C ++ 11表达式中的变量类型? [英] C++11 type of variable in expression?
问题描述
T
和引用T
因为它们适用于命名变量的表达式。具体考虑: int main()
{
int x = 42;
int& y = x;
x; //(1)
y; //(2)
}
表达式的类型是什么? x
在上面的(1)中?是 int
还是 lvalue引用int
? (它的值类别显然是 lvalue
,但它与其类型是分开的)
的表达式 y
在上面的(2)?是 int
还是 lvalue引用int
?
它在5.1.1.8中说:
标识符的类型是标识符的类型。
结果是由标识符表示的实体。
你缺少的是这个(§5/ 5):
如果一个表达式最初的类型是引用
T
(8.3.2,8.5.3),在进行任何进一步调整之前,将类型调整为T
分析。
因此虽然标识符 y
有类型 int&
,表达式 y
的类型为 int
。表达式从不具有引用类型,因此两个表达式的类型都是 int
。
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
.
这篇关于C ++ 11表达式中的变量类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!