mercredi 11 décembre 2019

C++ rvalue reference requestion

I'm confusing with rvalue reference,
is_rvalue_reference<decltype(a)>::value indicate that the a variable is rvalue reference,
but can't passing it to hello(int &&) function.

#include <iostream>
#include <string>
#include <array>
using std::cout;
using std::endl;
using std::boolalpha;
using std::string;
using std::is_rvalue_reference;
using std::move;


void hello(int && z) {};


int main(void) {
    int && a = 20;

    // a is_xvalue: true
    cout << "a is_xvalue: " << boolalpha << is_rvalue_reference<decltype(a)>::value << endl;

    // error C2664:  
    // 'void hello(int &&)': cannot convert argument 1 from 'int' to 'int &&'
    hello(a);       

    // compile ok.
    hello(move(a));

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire