C ++ 11 Unified Function Initialization and Overloading

Simple program:

void f(const std::string& s);
void f(const char* p);
f({});

Why does clang call f((const char*)nullptr)? I was expecting a compiler warning about an ambiguous call.

+4
source share
1 answer

This is described in the standard section of the C ++ 11 project 13.3.3.1.5[over.ics.list], which states:

Otherwise, if the parameter type is not a class:

[...]

  • if there are no elements in the initializer list, the implicit conversion sequence is an identity transformation. [Example:

    void f(int);
    f( { } ); // OK: identity conversion
    

-end example]

and therefore identity conversion would be better than calling the constructor.

, nullptr, , , . 8.5.4 [dcl.init.list]:

- T :

[...]

  • , , . [:

    int** pp {}; // initialized to null pointer
    

-end ]

+3

Source: https://habr.com/ru/post/1614156/


All Articles