Why does std :: move take an rvalue value as an argument?

According to cppreference.com , it movehas a signature

template< class T >
typename std::remove_reference<T>::type&& move( T&& t ) noexcept;

Why do I need an rvalue link T&& tas my ad?

Also when I tried the following code

void foo(int&& bar) {
    cout << "baz" << endl;
}

int main(){
    int a;
    foo(a);
}

I got an error from the compiler "rvalue link cannot be attached to lvalue"

What's happening? I'm so confused.

+4
source share
1 answer

This is not an rvalue link, but a forwarding link ; which could preserve a category of argument values. This means that it std::movecan accept both an lvalue and an rvalue and unconditionally convert them to an rvalue.

- , , std:: forward. :

1) , rvalue cv- :

2) auto && , , .

, int&& rvalue; , T&& T, T, .

+8

Source: https://habr.com/ru/post/1690914/


All Articles