All challenges
Multiple ChoiceBeginner 45s

string_view dangling from temporary

Why is `std::string_view sv = std::string("hi") + std::string("there");` dangerous?
Options
  1. 1sv views a temporary std::string destroyed at the end of the full expression, leaving a dangling view.
  2. 2It fails to compile, because a string_view cannot bind to an rvalue temporary expression.
  3. 3It always deep-copies the concatenated data into the view, so the result is actually safe.
  4. 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.

Play this in a 3-minute battle