According to the C++23 standard, what is the output of this program?
#include <iostream>
#include <string>
void f(const std::string &) { std::cout << 1; }
void f(const void *) { std::cout << 2; }
int main() {
f("foo");
const char *bar = "bar";
f(bar);
}What is the type of a string literal? Does the compiler prefer standard or user defined conversions?
22
A string literal is not a std::string, but a const char[] . If the compiler was to choose f(const std::string&), it would have to go through a user defined conversion and create a temporary std::string. Instead, it prefers f(const void*), which requires no user defined conversion.