【发布时间】:2018-02-11 17:20:57
【问题描述】:
我不是在问decltype((x)),我知道它是如何工作的。
根据 N4687 草案第 10.1.7.2 节
4 For an expression e, the type denoted by decltype(e) is defined as follows:
...
(4.2) — otherwise, if e is an unparenthesized id-expression or an unparenthesized class
member access (8.2.5), decltype(e) is the type of the entity named by e. If
there is no such entity, or if e names a set of overloaded functions, the
program is ill-formed;
...
还有例子
struct A { double x; };
const A* a = new A();
decltype(a->x) x3; // type is double
我的问题是,a->x 是 const double,但为什么 x3 是 double? const 去哪儿了?
顺便说一句,decltype(e) is the type of the entity named by e 到底是什么意思?
【问题讨论】:
-
a->x 是 const double 吗?您刚刚将其声明为 double x 而不是 const double
-
我知道
x被声明为双精度。但是a->x是e,e是const double,所以x是const double,不是吗? -
提交错误报告
-
我不认为
const对象的成员本身就是const,除非在类定义中声明const。只是禁止修改它们。 -
@user2357112 - 它们是常量。限定符递归地应用于聚合的成员。这种情况只是简单地指定为其他行为(尽管不是很清楚)。
标签: c++ c++11 language-lawyer decltype