Understanding rvalue references

后端 未结 5 887
清歌不尽
清歌不尽 2020-12-15 19:03

I think there\'s something I\'m not quite understanding about rvalue references. Why does the following fail to compile (VS2012) with the error \'foo\' : cannot conver

5条回答
  •  长情又很酷
    2020-12-15 19:32

    rvalue and lvalue are categories of expressions.

    rvalue reference and lvalue reference are categories of references.

    Inside a declaration, T x&& = , the variable x has type T&&, and it can be bound to an expression (the ) which is an rvalue expression. Thus, T&& has been named rvalue reference type, because it refers to an rvalue expression.

    Inside a declaration, T x& = , the variable x has type T&, and it can be bound to an expression (the ) which is an lvalue expression (++). Thus, T& has been named lvalue reference type, because it can refer to an lvalue expression.

    It is important then, in C++, to make a difference between the naming of an entity, that appears inside a declaration, and when this name appears inside an expression.

    When a name appears inside an expression as in foo(x), the name x alone is an expression, called an id-expression. By definition, and id-expression is always an lvalue expression and an lvalue expressions can not be bound to an rvalue reference.

提交回复
热议问题