fre*_*low 50 c++ type-inference decltype c++11
我不明白FCD第148页上的例子的最后一行(§7.6.1.2/ 4):
const int&& foo();
int i;
struct A { double x; };
const A* a = new A();
decltype(foo()) x1 = i; // type is const int&&
decltype(i) x2; // type is int
decltype(a->x) x3; // type is double
decltype((a->x)) x4 = x3; // type is const double&
Run Code Online (Sandbox Code Playgroud)
为什么括号在这里有所作为?它不应该只是double
像上面那样吗?
Cub*_*bbi 40
就在这个例子之上,它说
- 如果e是未表示的id-expression或类成员访问(5.2.5),则decltype(e)是e命名的实体的类型.
- 如果e是左值,则decltype(e)是T&,其中T是e的类型;
我认为decltype(a->x)
是"类成员访问" decltype((a->x))
的一个例子,是左值的一个例子.
Jam*_*lis 18
decltype(a->x)
Run Code Online (Sandbox Code Playgroud)
这为您提供了成员变量的类型A::x
,即double
.
decltype((a->x))
Run Code Online (Sandbox Code Playgroud)
这为您提供了表达式的类型(a->x)
,它是一个左值表达式(因此它是一个const引用 - a
为a const A*
).