C++11 declval:什么是 "unevaluated context"?

C++11 declval: what is "unevaluated context"?

cppreference web site 引入 declval :

Note that because no definition exists for declval, it can only be used in unevaluated contexts

我查了ISO C++11标准,没有"unevaluated context"的词组。这是编译原理中的概念,还是别的?

谢谢。

来自标准 [第 5 节,#7]

In some contexts, unevaluated operands appear (5.2.8, 5.3.3, 5.3.7, 7.1.6.2). An unevaluated operand is not evaluated. [ Note: In an unevaluated operand, a non-static class member may be named (5.1) and naming of objects or functions does not, by itself, require that a definition be provided (3.2). — end note ]

使用的上下文是:

  1. 类型识别[第 5.2.8 节]
  2. Sizeof 运算符[第 5.3.3 节]
  3. noexcept [第 5.3.7 节]
  4. 类型说明符 例如 decltype(declval<T>()) [第 7.1.6.2 节]

所以,在外行人看来(据我了解)它不能用作像 a + b;.

这样的独立表达式

根据下面马萨的评论:

In those above mentioned contexts, the expression that denotes the unevaluated operand is unfolded so that the type of its result (and, in the case of sizeof, the size of that type) can be determined, but the evaluation of the expression is never incorporated to the final program.

Example: typeof(std::cout << "1\n") &x = std::cout; will never generate the code to print "1\n" to the standard output.