lundi 24 octobre 2016

c++11 - initialize std::string from char* directly with {} constructor

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>&’

Aucun commentaire:

Enregistrer un commentaire