Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Type and Value Category for Expression and Variable

Tags:

c++

From this link, it says that

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

So given the following:

int &&rf_int = 10;

I can say that variable rf_int is of compound type rvalue reference to int.

But when talking about value category, it specifically says that

Each expression has some non-reference type

and

Each C++ expression (an operator with its operands, a literal, a variable name, etc.)

Based on the above two statement, rf_int can be treated as an expression and expression has non-reference type.

Now I am really confused. Does rf_int have a reference type or not? Do we have to provide context when talking about the type of a name, be it a variable or an 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)?

EDIT: A comment here got me wonder about this issue.

like image 286
Rich Avatar asked Apr 01 '16 06:04

Rich


1 Answers

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.)

like image 160
L. F. Avatar answered Sep 23 '22 07:09

L. F.