谈一谈c++中的unevaluated operand

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ssss1223ss/article/details/60466357

什么是unevaluated operand

我们先看2段外文:

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. An unevaluated operand is considered a full-expression. [ 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 ]
+ 5.2.8 covers typeid
+ 5.3.3 covers sizeof
+ 5.3.7 covers noexcep
+ 7.1.6.2 covers simple type specifiers such as auto and decltype and POD types like int, char, double etc.

The operands of the four operators typeid, sizeof, noexcept, and decltype (since C++11) are expressions that are not evaluated (unless they are polymorphic glvalues and are the operands of typeid), since these operators only query the compile-time properties of their operands. Thus, std::size_t n = sizeof(std::cout << 42); does not perform console output.
The unevaluated operands are considered to be full expressions even though they are syntactically operands in a larger expression (for example, this means that sizeof(T()) requires an accessible T::~T)

可以这样理解:
1. unevaluated operand是不被评估的,它是一个完整的表达式。c++中4个操作符typeid,sizeof, noexcept,decltype对应的表达式是不需要被评估的。
2. unevaluated operand仅仅是查询编译期间的相关属性,并不会生产运行期的代码。
3. unevaluated operand不会去完全编译表达式,即不会完全检查编译的错误,但是参数值和需要推导返回值的地方会执行编译检查。

猜你喜欢

转载自blog.csdn.net/ssss1223ss/article/details/60466357