Advertisement
Advanced C++, Type Deduction & auto

How does deduction interact with reference collapsing rules?

Short answer

Reference collapsing governs what happens when references to references form during deduction/aliasing: T& & -> T&, T& && -> T&, T&& & -> T&, and T&& && -> T&&. Only two rvalue references collapse to an rvalue reference; any lvalue reference collapses to lvalue.

Save C++ mock interview
Advertisement
AModel answer
Reference collapsing governs what happens when references to references form during deduction/aliasing: T& & -> T&, T& && -> T&, T&& & -> T&, and T&& && -> T&&. Only two rvalue references collapse to an rvalue reference; any lvalue reference collapses to lvalue. These rules are what make forwarding references bind correctly to both lvalues and rvalues.
// If T deduces to int& in T&&, then int& && collapses to int&
Real-world example
Reference collapsing is why a forwarding reference becomes an lvalue reference when passed an lvalue.
Likely follow-ups
  • What do the four collapsing rules produce? | Why do they matter for forwarding?
Related topics