All challenges
Multiple ChoiceHard 45s

Dangling reference from operator[]

auto&& r = vec[0]; vec.clear(); vec.shrink_to_fit(); use(r); What is the problem?
Options
  1. 1It is always safe because r binds to a copy that outlives the vector
  2. 2r may dangle after the vector releases storage, giving undefined behavior
  3. 3auto&& forces a copy, binding r to a fresh element that survives clear
  4. 4It only violates the standard if vec was const and its storage survives

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