Multiple ChoiceHard 45s
Pass-by-value then move idiom
Why is taking a parameter by value and then moving from it (`void f(std::string s){ v.push_back(std::move(s)); }`) often optimal?
Options
- 1It always avoids one copy for every argument type, because elision into the parameter is guaranteed to happen for all callers.
- 2It prevents the need for a move constructor on std::string, because the parameter itself is copied exactly once before use.
- 3For rvalue arguments it moves once; for lvalue arguments it copies once then moves — no worse than by-const-ref plus one copy.
- 4It allows the caller's lvalue to be moved from silently, so no copy occurs at any call site that passes one.
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.