c ++ 11 decltype(e)是e命名的实体的类型

Cae*_*sar 10 c++ decltype language-lawyer c++11


我不是问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;
        ...
Run Code Online (Sandbox Code Playgroud)

例如

struct A { double x; };
const A* a = new A();
decltype(a->x) x3; // type is double
Run Code Online (Sandbox Code Playgroud)

我的问题是,
a->xconst double,但为什么x3是double?在什么地方const去了?
BTW,究竟是什么decltype(e) is the type of the entity named by e意思?

n. *_* m. 6

这方面的标准似乎含糊不清.

一个实体是一个值,对象,参考,功能,枚举,类型,类的成员,位字段,模板,模板特,命名空间或参数包.

表达a->x,可以说,仅举构件 xstruct A,其类型double.也可以说相同的表达式命名具有类型的对象const double.这两件事都是实体.规范性文本并未明确表明预期的解释是第一个,它只能从示例中推断出来.

  • 有人说,CWG引入"实体"的唯一原因是因为"thingy"被认为不适合ISO标准. (4认同)
  • @TC他们还删除了原子衰变脚注.永远不要原谅,永远不要忘记. (2认同)
  • 这里有一个微妙的区别.成员子对象的*类型*不受其完整对象的cv限定条件的影响.当然,很难证明是否定的,但是可以从"const对象"的定义推断为"const T`类型的对象或者这种对象的不可变子对象",在[\ [basic. type.qualifier \]/1](https://timsong-cpp.github.io/cppwp/basic.type.qualifier#1.1)(同样适用于`volatile`).如果对整个对象的cv-qualification影响其子对象的类型,则定义的第二部分将是完全多余的. (2认同)

T.C*_*.C. 1

由类成员访问表达式命名的“实体”就是该类成员,在本例中为A::x

的类型A::xdouble.