Explicit Conversion Functions
You're already familiar with the use of explicit in constructors that take a single argument. Unless declared explicit, a constructor C(arg) operates as implicit conversion functions that converts arg to C. C++0x extends the use of explicit to conversion functions as well. When you declare a conversion function as explicit, implicit conversions are permitted only in direct initialization expressions:
class BuggyString {
//now explicit
explicit operator char*();
explicit operator const char* () const;
};
const char * ps(str);//OK, direct initialization
ps=str; //Error, explicit cast required
ps=(const char *) str; //OK, C-style cast
ps=static_cast<const char*>(str); //OK, new style cast
Safe and Sound
The use of explicitguarantees that conversion functions do not surprise you with undesirable conversions. Instead, every conversion operation requires an explicit permission from you in the form of at cast notation (except in the case of direct initializations). Explicit conversion functions thus offer a safe and uniform method for conversions.