// GetValue() is l-value now.
// GetValue() return a lvalue reference.
int&GetValue(){staticintvalue=10;// Must be a persistent location.
returnvalue;}
r-value
❌ Location / Address
✅ Value
1
2
3
inta=123;^^^r-value
1
2
3
4
5
// GetValue() is r-value
// It's return a temporary value 10
intGetValue(){return10;}
Some code snippet
Parameter that taking any value
Taking a value as parameter.
1
2
3
4
5
6
7
8
voidSetValue(intvalue){}intmain(){inti=10;SetValue(i);// OK, passing a l-value.
SetValue(10);// OK, passing a r-value.
}
l-value reference
Taking only a l-value reference as parameter.
1
2
3
4
5
6
7
8
voidSetValue(int&value){}intmain(){inti=10;SetValue(i);// OK, passing a l-value.
SetValue(10);// ERROR!! Can't passing a r-value.
}
const l-value reference
Passing l-value and termporary value.
1
2
3
4
5
6
7
8
voidSetValue(constint&value){}intmain(){inti=10;SetValue(i);// OK, passing a l-value.
SetValue(10);// OK, compiler will create a temp l-value.
}
r-value reference
Function only accept termporary value.
1
2
3
4
5
6
7
8
voidSetValue(constint&&value){}intmain(){inti=10;SetValue(i);// ERROR!! Can't bound l-value to r-value reference
SetValue(10);// OK, passing a r-value
}