Monday, July 10, 2017

C++11: Rvalue References: Move

A move() template function tells the compiler to move from source instead of first copying from source. The source is changed to a "cheap default", i.e., an object that is inexpensive to create and delete. Here is an example:

int main()
    int   i  = 3;
    int & ri = i;
    int   j  = 4;
    int & rj = j;
    rj       = std::move(ri);
    std::cout << rj << std::endl;
    return 0;
// Output: 3
The move() template function is equivalent to a static_cast to an rvalue reference type.

No comments:

Post a Comment