Assume a deleted constructor is added to std::string_view
as following:
// Cannot construct a string_view from a temporary string
string_view(string const &&) = delete;
With this change, the following code would fail at compile time:
// You can't do this anymore
std::string_view sv = std::string{"A"} + std::string{"B"};
However this wouldn’t:
// Oops
std::string_view sv = (std::string const &)(std::string{"A"} + std::string{"B"});
What can be done in string
to forbid the result of the concatenation to be converted into a string const &
(which doesn’t extend the lifetime of the result, and thus the string_view
is dangling).
8