TITLE: explicit keyword background PROBLEM: Claus Hirth The ANSI/ISO-Draft says quite a lot about where not to use the "explicit" keyword, but I did not find sth about what it means. Is it correct that explicit is only used to prevent ctors from being called implicitly for conversion purposes ? What happens if an implicit call to a ctor would need to be made, but this ctor is declared explicit ? Do I get a warning, or is it an error ? RESPONSE: clamage@Eng.Sun.COM (Steve Clamage), 9 Jan 96 It is a language rule violation, so you should get an error. Background: Implicit type conversions appear in many places in C++. For example, any numeric value may be converted to a value of any other numeric type. That conversion rule (inherited from C in this case) allows mixed-mode arithmetic expressions without writing casts. A constructor for class C having a single argument of type T acts as an implicit type conversion operator from T to C, whether you want it to or not. Example: class String { // expandable character string public: String(int size); // minimum initial size String(const char* value); // convert from char array ... }; void foo(const String& ID_number); ... foo(12345); // oops! should be foo("12345"); This example code will compile without complaint, but does the wrong thing: a temporary empty string of capacity 12,345 characters is passed to "foo". Because of this sort of problem, the usual recommendation was not to write one-argument constructors unless you want to allow the type conversion unconditionally. The conversion from char* to String is probably OK, but an implicit conversion from int is almost never going to be correct. Implicit type conversions also lead to ambiguity. Example: class Rational { ... Rational(int); ... }; void process(String); void process(Rational); ... process(123); // ambiguous: convert 123 to Rational or String? The workarounds for this problem were ugly, especially when the single argument constructor was natural for the class. The "explicit" keyword was added to solve this problem. If you change the String constructor declaration to explicit String(int size); // minimum initial size the code above will generate an error message at compile time: there is no implicit conversion from int to String. When you really want to use the constructor, you must say so explicitly: String s(12); // OK foo(String(12345)); // OK