I have a function which accepts a std::string&:
void f(std::string& s) { ... }
I have a const char* which should be the input parameter for that function. This works:
const char* s1 = "test";
std::string s2{s};
f(s2);
This doesn't:
const char* s1 = "test";
f({s1});
Why isn't this possible? The funny thing is that CLion IDE is not complaining, but the compiler is:
no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::basic_string<char>&’
s1is completely unneeded given your example:std::string s2 = "test";const char*is not astd::stringso for it to even work at all a new temporary string would need to be created to pass to the function. But the function modifies the string you pass in!! So it won't accept a temporary. Non-const reference won't bind to temporaries.