[EMCpp]Item-28 Understand Reference Collapsing
· ☕ 3 min read
Reference collapsing occurs in four contexts: template instantiation, auto type generation, creation and use of typedefs and alias declarations, and decltype.
Reference collapsing occurs in four contexts: template instantiation, auto type generation, creation and use of typedefs and alias declarations, and decltype.
Universal reference parameters often have efficiency advantages, but they typically have usability disadvantages.
Overloading on universal references almost always leads to the universal reference overload being called more frequently than expected.
But don’t apply std::move
or std::forward
to local objects if they would otherwise be eligible for the return value optimization.
If a function template parameter has type T&&
for a deduced type T
, or if an object is declared using auto&&
, the parameter or object is a universal reference.