This post represents my first encounter with rvalue references. The following are noteworthy:
1. Function 'increment' can be overload based on lvalue and rvalue reference. The main function calls 'increment' and based on the type of the operand, the appropriate overload is selected.
2. Rvalues prefer binding to Rvalue reference and Lvalues prefer binding to Lvalue reference. Note that postfix operator ++ returns an unnamed temporary and is an Rvalue
3. Lambda functions can be declared in namespace scope and can also be declared to accept Lvalue and Rvalue references, just like ordinary functions
4. However, to invoke the lamda function by name, we need to store the value of the lambda expression in a variable. Since it is very difficult to spell out the exact type of the lambda function, the 'auto' keyword is convenient, which enables the compiler to auto deduce the type of the variable based on the type of the initializer lambda expression
5. Using lambda functions however does not allow overloading, and the caller needs to use the appropriate lambda expression variable based on whether the argument is Lvalue or Rvalue e.g.
#include <iostream> void increment(int &x) { std::cout << "Lvalue reference" << std::endl; x = x + 1; } void increment(int &&x) { std::cout << "Rvalue reference" << std::endl; x = x + 1; } auto r1 = [](int &x) { std::cout << "Lvalue Lambda reference" << std::endl; x = x + 1; }; auto r2 = [](int &&x) { std::cout << "Rvalue Lambda reference" << std::endl; x = x + 1; }; int main() { int x = 2; increment(x); // calls Lvalue version increment(x++); // calls Rvalue version increment(++x); // calls Lvalue version increment(x + 1); // calls Rvalue version r1(x); // calls Lvalue version Lambda r2(x++); // calls Rvalue version Lambda r1(++x); // calls Lvalue version Lambda r2(x + 1); // calls Lvalue version Lambda std::cout << x << std::endl; }
The output of the code shown is
Lvalue reference
Rvalue reference
Lvalue reference
Rvalue reference
Lvalue Lambda reference
Rvalue Lambda reference
Lvalue Lambda reference
Rvalue Lambda reference
10
No comments:
Post a Comment