Confusion about rvalue reference: what is its type?

Xavier

I have a really simple func definition here:

void testRvalue(int&& r)
{
    printf("rvalue ref is called\n");
    testRvalue(r); // this line gives "no known conversion from 'int' to 'int &&' for 1st argument"
}

I mean... inside this function, what is the type of r? Isn't it int&& (the parameter is int&& r so how come r is not of type int&&?) If so why can't I pass this r to this func itself, which takes a int&& type as parameter?

What is the type of r exactly? int&& or int? If int, why???

bolov

A very good read is Value categories.

Yes, the type of the variable r is indeed int&&. However what matters here is the expression and:

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. Each expression has some non-reference type, and each expression belongs to exactly one of the three primary value categories: prvalue, xvalue, and lvalue.

The expression r is an lvalue:

lvalue

The following expressions are lvalue expressions:

  • the name of a variable [...], regardless of type. Even if the variable's type is rvalue reference, the expression consisting of its name is an lvalue expression;

Rvalue references can bind to prvalues or xvalue but not to lvalues so if you want to bind an rvalue reference to r you need to convert r to an xvalue. This is done with std::move which despite its name is just an cast.

You can easily reason about this like so: if it has a name then it's an lvalue (even if the type of that id is rvalue reference). You can't bind rvalue references (which in principle should bind to temporary objects) to something that has a name. Something that has a name can be reused. You need std::move to enable move from that lvalue.

Regarding the message which says "no known conversion from 'int'". As shown above the type of the expression r is int, however a more appropriate diagnostic message would have been something along the lines: "rvalue reference cannot bind to lvalue".

And indeed newer clang and gcc give better messages:

gcc

error: cannot bind rvalue reference of type 'int&&' to lvalue of type 'int'

clang

candidate function not viable: expects an rvalue for 1st argument

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Lvalue to rvalue reference binding

来自分类Dev

书中“ rvalue”和“ rvalue reference”之间的混淆

来自分类Dev

Confusion about bsr and lzcnt

来自分类Dev

confusion about character array initialisation

来自分类Dev

带有rvalue引用参数的模板函数中的std :: is_rvalue_reference

来自分类Dev

Why can you indirectly bind an rvalue to an lvalue reference but not directly?

来自分类Dev

std :: move()和std :: add_rvalue_reference()之间的区别

来自分类Dev

What can be said about the value of fundamental type members of moved from object during move construction?

来自分类Dev

How to use unscoped enumerator as if its type is its underlying type

来自分类Dev

模板是否应该使非Rvalue-reference构造函数/赋值仅移动不同类型的参数?

来自分类Dev

Some confusion about listening and open ports, difference between listening, open and blocked

来自分类Dev

What about using a nvarchar as a foreign key?

来自分类Dev

What is the type of quasiquote that matched type parameters?

来自分类Dev

What is the difference between type conversion and type assertion?

来自分类Dev

Populating a list based on its element type when passed to a method

来自分类Dev

Java: Creating Reference Type from Abstract class and interface

来自分类Dev

What (if any) guidelines exist about text/link colour difference?

来自分类Dev

Structure Confusion

来自分类Dev

nullptr rvalue如何

来自分类Dev

What is the maximum length for keyword type in elasticsearch?

来自分类Dev

what object type/instance to use for synchronization

来自分类Dev

what is the correct way to add type definition for this module

来自分类Dev

Storing rvalue references: should this work?

来自分类Dev

std :: bind和rvalue参考

来自分类Dev

std :: thread和rvalue参考

来自分类Dev

What is happening when require("http").Server() is evaluated with an Express app as its argument?

来自分类Dev

Java: What's the most efficient way to read relatively large txt files and store its data?

来自分类Dev

实现类似字符串的类时出现“ no type named'const_reference”错误

来自分类Dev

Codeigniter .htaccess to remove index.php but what about sub-directory access?

Related 相关文章

  1. 1

    Lvalue to rvalue reference binding

  2. 2

    书中“ rvalue”和“ rvalue reference”之间的混淆

  3. 3

    Confusion about bsr and lzcnt

  4. 4

    confusion about character array initialisation

  5. 5

    带有rvalue引用参数的模板函数中的std :: is_rvalue_reference

  6. 6

    Why can you indirectly bind an rvalue to an lvalue reference but not directly?

  7. 7

    std :: move()和std :: add_rvalue_reference()之间的区别

  8. 8

    What can be said about the value of fundamental type members of moved from object during move construction?

  9. 9

    How to use unscoped enumerator as if its type is its underlying type

  10. 10

    模板是否应该使非Rvalue-reference构造函数/赋值仅移动不同类型的参数?

  11. 11

    Some confusion about listening and open ports, difference between listening, open and blocked

  12. 12

    What about using a nvarchar as a foreign key?

  13. 13

    What is the type of quasiquote that matched type parameters?

  14. 14

    What is the difference between type conversion and type assertion?

  15. 15

    Populating a list based on its element type when passed to a method

  16. 16

    Java: Creating Reference Type from Abstract class and interface

  17. 17

    What (if any) guidelines exist about text/link colour difference?

  18. 18

    Structure Confusion

  19. 19

    nullptr rvalue如何

  20. 20

    What is the maximum length for keyword type in elasticsearch?

  21. 21

    what object type/instance to use for synchronization

  22. 22

    what is the correct way to add type definition for this module

  23. 23

    Storing rvalue references: should this work?

  24. 24

    std :: bind和rvalue参考

  25. 25

    std :: thread和rvalue参考

  26. 26

    What is happening when require("http").Server() is evaluated with an Express app as its argument?

  27. 27

    Java: What's the most efficient way to read relatively large txt files and store its data?

  28. 28

    实现类似字符串的类时出现“ no type named'const_reference”错误

  29. 29

    Codeigniter .htaccess to remove index.php but what about sub-directory access?

热门标签

归档