c++c++14c++17visual-c++-2017

Why do char{} and char() work as a temporary variable for a char* argument?


In Visual C++ 2017 (with /std:c++14 or with /std:c++17), the following code works:

void TakePtr(char*); // const or not

int main()
{ 
     TakePtr(char{});
     TakePtr(char());
}

I don't understand why it works.

Apparently, the following would also work (as expected):

void TakeChar(char);

   TakeChar(char{});
   TakeChar(char());

How does the compiler deduce (or convert) the type char to char*, when char{} or char() is used as an argument?

Now, if I have both char and char* overloads, it works without any error/warning about ambiguity:

void TakePtr(char*);
void TakePtr(char);

    TakePtr(char{});  // Chooses 'char'
    TakePtr(char());  // Chooses 'char'

Why is the compiler okay with char{} for TakePtr(char*)? And why doesn't it give a warning/error when choosing the better version? Such behavior is bound to break existing code.

For sure, the compiler isn't happy with:

void TakePtr(char*);

    char c{};
    TakePtr(c);

Solution

  • This is simply MSVC being behind: the rule in C++03 was that any constant expression of integer type and value 0 was a null pointer constant and could thus be converted to char*. Certainly char() qualifies—and char{} means the same thing, although it never overlapped with the rule.