gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:24:30 32 4
gpt4 key购买 nike

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.

关于C++11 declval : what is "unevaluated context"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37934918/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com