4 ms·
Question: it is ok to initialize a string_view from a string literal, right? e.g. std::string_view sv{ "lvalue" }; or auto sv{ "lvalue"sv };
by smitherfield 10y ago
Question: it is ok to initialize a string_view from a string literal, right?
e.g.
std::string_view sv{ "lvalue" };
or
auto sv{ "lvalue"sv };
- Jeaye 10y agoYeah. String literals are lvalues and they live for the lifetime of the program. Both of those snippets would be fine.
- thecpphorse 10y agoYes, but never use {} initialization with auto.
- clishem 10y agoNever give advice without telling why, because it's very likely that people will forget it if they don't understand your advice.
- thecpphorse 10y agoOk. auto i{42}; // std::initializer_list<int>
- david-given 10y agoBut why is that bad?
- lmitchell 10y agoBecause `i` isn't an int, it's an initializer_list, and you almost never want to actually create an initializer_list instead of an int? Seems pretty understandable to me.
- thecpphorse 10y agoIt's unexpected. Nobody ever* wanted to assign an initializer list to a named variable. [*] pretty much
- hellofunk 10y agoNo longer applies with c++17