#include using namespace std ; //This function determines if something //is lvalue or rvalue template constexpr bool is_lvalue(T&&) { return std::is_lvalue_reference{}; } //LVALUE REFERENCE void function1LValueRef( int& a1 ) { cout << "function1:" << a1 << endl ; } //RVALUE REFERENCE void function2RValueRef( int&& a1 ) { //Since a1 is a name it is a lvalue at this point inside the //function but the function can only take rvalues. cout << "Is a1 a lvalue: " << is_lvalue( a1 ) << endl ; //a1 = 25 ; } int main() { int x1 = 10 ; int y1 = 30 ; //Need to initialize references int&& x2R = 20 ; int&& x3R = 30 ; int& x3L = x1 ; //compiler error //A rvalue reference must be initialized //int&& x7 ; //Can only initialize it with a temporary object // int&& x7 = x1 ; //can reassign values to r reference //x2R is not pointing to x3R but rather it's //value is being updated. x2R = x3R ; cout << "x2R:" << x2R << endl ; x2R = 35 ; cout << "x2R:" << x2R << endl ; //Can initialize l-value reference with r-value //reference. Anything that has a name is l-value. int& x6R = x2R ; //cannot initialize lvalue //reference with a constant/rvalue //At the point of definition //int& x8 = 25 ; //can do //const int& x1 = 20 ; //Can only initialize rvalue reference with // rvalues //int&& x7 = x3L ; function1LValueRef( x1 ) ; function2RValueRef( 20 ) ; //Allowed // x2R = x1 ; x2R = 40 ; //reassignment to a r value reference cout << "x2R:" << x2R << endl ; //Not a reassignment of the lvalue reference. //changes the value of x1 and x3L "points" to x1 . x3L = y1 ; //x2R's value is being set to x1 x1 = x2R ; cout << "x1:" << x1 << endl ; //function 2 is r reference //Compiler error r-value reference expects a constant // function2RValueRef( x1 ) ; //ok move converts a l value to a r value function2RValueRef( move(x1) ) ; //x2R is a name so it's a l value at this point function1LValueRef( x2R ) ; cout << "Is x2R a lvalue: " << is_lvalue( x2R ) << endl ; cout << "Is 12 a lvalue: " << is_lvalue( 12 ) << endl ; function2RValueRef( move(x2R) ) ; cout << "x2R value: " << x2R << endl ; //compiler error even though x2R is rvalue !!! //But it has a name so now is lvalue !! // function2RValueRef( x2R ) ; function2RValueRef( 21 ) ; int x19 = 5 ; cout << x19 << " " << move(x19) << endl ; return 0 ; }