In this code:
#include <string>
#include <iostream>
void my_function(const char* value)
{
std::cout << "char * " << (value ? value : "nullptr");
}
void my_function(const std::string &value)
{
std::cout << "string";
}
int main()
{
my_function({});
}
https://godbolt.org/z/56bxzjM1M
The const char *
overload is chosen, passing a nullptr
pointer. Can somebody explain why? I assume this is something weird with initializer lists? (By the way, this happens with my_function(string value)
too, so it isn't the reference).
This caused a crash in our code because somebody added a const char *
overload, causing the rare caller who passed {}
to send in a nullptr
.