In C++11 I'm somewhat confused about the difference between the types T
and reference to T
as they apply to expressions that name variables. Specifically consider:
int main()
{
int x = 42;
int& y = x;
x; // (1)
y; // (2)
}
What is the type of the expression x
in (1) in the above? Is it int
or lvalue reference to int
? (Its value category is clearly an lvalue
, but this is separate from its type)
Likewise what is the type of the expression y
at (2) in the above? Is it int
or lvalue reference to int
?
It says in 5.1.1.8:
The type of [an identifier primary expression] is the type of the identifier. The result is the entity denoted by the identifier. The result is an lvalue if the entity is a function, variable, or data member and a prvalue otherwise.
The bit you're missing is this (§5/5):
If an expression initially has the type “reference to
T
” (8.3.2, 8.5.3), the type is adjusted toT
prior to any further analysis.
So although the identifier y
has type int&
, the expression y
has type int
. An expression never has reference type, so the type of both of your expressions is int
.
The expression denotes an lvalue of type int
, in both cases. An expression cannot be a reference , although you can bind the result of an expression with an lvalue or rvalue reference.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.