Multiple ChoiceBeginner 45s
string_view dangling from temporary
Why is `std::string_view sv = std::string("hi") + std::string("there");` dangerous?
Options
- 1sv views a temporary std::string destroyed at the end of the full expression, leaving a dangling view.
- 2It fails to compile, because a string_view cannot bind to an rvalue temporary expression.
- 3It always deep-copies the concatenated data into the view, so the result is actually safe.
- 4The string_view extends the temporary's lifetime, keeping the underlying buffer alive.
Think you can answer under pressure?
Get matched and answer live questions in 3-minute 1v1 battles. Elo ranking, badge tiers and bragging rights on the line.