Reputation: 376
Simple program:
void f(const std::string& s);
void f(const char* p);
f({});
Why clang calls f((const char*)nullptr)
? I've expected compiler warning about ambiguous call.
Upvotes: 4
Views: 209
Reputation: 158459
This is covered in the draft C++11 standard section 13.3.3.1.5
[over.ics.list] which says:
Otherwise, if the parameter type is not a class:
[...]
if the initializer list has no elements, the implicit conversion sequence is the identity conversion. [ Example:
void f(int); f( { } ); // OK: identity conversion
—end example ]
and so an identity conversion would be better than a constructor call.
The reason why we get a nullptr is because it is value initializing the pointer. From section 8.5.4
[dcl.init.list]:
List-initialization of an object or reference of type T is defined as follows:
[...]
Otherwise, if the initializer list has no elements, the object is value-initialized. [ Example:
int** pp {}; // initialized to null pointer
—end example ]
Upvotes: 3