Several sources given the C++ reference collapsing rules as follows:
A& & becomes A&
A& && becomes A&
A&& & becomes A&
A&& && becomes A&&
(e.g. http://thbecker.net/articles/rvalue_references/section_08.html )
I can give the following example for A&&& becomes A&
template <class T> void f1(T&& param) {
// T t = 5; // does not compile because T is T&
param++; // param collapses from int&&&& to int&&
}
void demo()
{
int x = 8;
int &y = x;
f1(y); // OK T will still be int&, param will go to int&&& -> collapses to int&
cout << y; // prints 9
}
I would like something similar for A&& && becomes A&&, however when I call with an RValue , T is deduced to int, and so this does not show what I would like to.
template <class T> void f1(T&& param) {
T t = 5; // compiles since T is int
T t2 = t; // would not compile if T was int&&
t2++;
cout << t; // prints 5 since t2 was not a reference
}
void demo()
{
f1(8); // OK T deduced to int, param will go to int&& , no collapsing
}
Can someone help me show a similar example where T is deduced to T&& and the parameter is collapsed from T&&&& to T&&?
After help from commenters and other answer posters, I've come up with this demo: