C++ Type and Value Category for Expression and Variable

前端 未结 3 538
死守一世寂寞
死守一世寂寞 2021-02-06 01:31

From this link, it says that

Objects, references, functions including function template specializations, and expressions have a property called type

<
3条回答
  •  逝去的感伤
    2021-02-06 02:19

    Does rf_int have a reference type or not?

    The entity (or variable) with the name rf_int has type int&& (a reference type) because of the way it is declared, but the expression rf_int has type int (a non-reference type) per [expr]/5:

    If an expression initially has the type “reference to T” ([dcl.ref], [dcl.init.ref]), the type is adjusted to T prior to any further analysis. The expression designates the object or function denoted by the reference, and the expression is an lvalue or an xvalue, depending on the expression. [ Note: Before the lifetime of the reference has started or after it has ended, the behavior is undefined (see [basic.life]).  — end note ]


    Do we have to provide context when talking about the type of a name, be it a variable or an expression?

    Yes, we do. rf_int can be said to have different types depending on whether it refers to the entity or the expression.


    More specifically, when a variable name is used in function call:

    SomeFunc(rf_int);
    

    Is rf_int now considered an expression (thus it is an lvalue with type int), or a variable (thus it is an lvalue with type rvalue reference to int)?

    It is considered an expression, which is an lvalue of type int. (Note that value category is a property of expressions. It is not correct to say a variable is an lvalue.)

提交回复
热议问题