example2_rvalue_references.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>

void process(int&& x) {
    std::cout << "Rvalue reference: " << x << std::endl;
}

int main() {
    process(10);       // 10 is an rvalue, bound to int&&

    int a = 20;
    // process(a);     // Error: 'a' is an lvalue, cannot bind to int&&

    process(std::move(a));  // std::move(a) casts 'a' to an rvalue

    return 0;
}
Back to rvalue