std::string_view v = s.substr(i);,
std::string by value, i.e. it returns a temporary
std::string could be converted to
std::string_view implicitly, which is constructed from
std::string::data(), i.e. the pointer to the underlying array of the temporary
std::string. After the full expression, the temporary
std::string is destroyed and the pointer held by
std::string_view becomes dangling pointer.
It is the programmer’s responsibility to ensure that the resulting string view does not outlive the string.
std::string get_string(); int f(std::string_view sv); int x = f(get_string()); // OK std::string_view sv = get_string(); // Bad: holds a dangling pointer
CLICK HERE to find out more related problems solutions.