C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources]

Herb Sutter, Andrei Alexandrescu

نسخه متنی -صفحه : 521/ 218
نمايش فراداده

Exceptions

When used sparingly and with care, implicit conversions can make calling code short and intuitive. The standard

std::string defines an implicit constructor that takes a

const char* . This works fine because the designers took some precautions:Item 29). This avoids the creation of hidden temporary variables.

Even so, there can still be some weirdness with overloaded functions:

void Display( int ); void Display( std::string ); Display( NULL );

// calls Display(int)

This result might be surprising. (Incidentally, if it did call

Display( std::string ) , the code would have exhibited undefined behavior because it's illegal to construct a

std::string from a null pointer, but its constructor isn't required to check for the null.)