Search⌘ K
AI Features

Problem With The Initial Example

Explore the challenges of using std string_view with temporary objects in C++17. Learn how temporary strings can lead to dangling references and understand the importance of managing string lifetimes correctly to avoid common pitfalls when working with string_view.

We'll cover the following...

The intro section showed you an example of:

C++
std::string_view StartFromWord(std::string_view str, std::string_view word)
{
return str.substr(str.find(word)); // substr creates only a new view
}

​The code doesn’t have any issues with non-null-terminated strings - as all the functions are from the string_view API.

However, how ...