Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ rvalue reference requestion

I'm confusing with rvalue reference,
is_rvalue_reference<decltype(a)>::value indicates that the a variable is rvalue reference,
but can't pass 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;
}
like image 472
火狐狸 Avatar asked Dec 18 '22 14:12

火狐狸


2 Answers

While a may seem like a r-value it is not.

a is itself an l-value that contains an r-value reference.

You can take the address of a, but you can't take the address of move(a).

Please take a look at this article to really understand value categories specifically geared towards your question.

like image 74
Moshe Rabaev Avatar answered Jan 01 '23 22:01

Moshe Rabaev


Types and value categories are two independent things.

Each C++ expression (an operator with its operands, a literal, a variable name, etc.) is characterized by two independent properties: a type and a value category.

The type of a is int&&, i.e. rvalue-reference. But as a named variable, a is an lvalue and can't be bound to rvalue-reference. It seems confusing but try to consider them separately.

(emphasis mine)

The following expressions are lvalue expressions:

  • the name of a variable, a function, a template parameter object (since C++20), or a data member, regardless of type, such as std::cin or std::endl. Even if the variable's type is rvalue reference, the expression consisting of its name is an lvalue expression;
like image 38
songyuanyao Avatar answered Jan 01 '23 22:01

songyuanyao