经常想到的一个例子是:
sizeof
表达式,它不计算表达式,而是通过静态类型确定大小。例如:
int func();
sizeof(func());
这是我的思维局限,所以如果还有其他未评估的上下文,那么它们是什么?
最佳答案
幸运的是,该标准有一个方便的列表(§ 5 [expr] ¶ 8):
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.
让我们详细看看这些。
我将在我的示例中使用以下声明。声明的函数从未在任何地方定义,因此如果对它们的调用出现在 求值 上下文中,则程序格式错误,我们将收到链接时错误。但是,在未评估的上下文中调用它们是可以的。
int foo(); // never defined anywhere
struct widget
{
virtual ~widget();
static widget& get_instance(); // never defined anywhere
};
typeid
§ 5.2.8 [expr.typeid] ¶ 3:
When
typeid
is applied to an expression other than a glvalue of a polymorphic class type, the result refers to astd::type_info
object representing the static type of the expression. Lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) conversions are not applied to the expression. If the type of the expression is a class type, the class shall be completely-defined. The expression is an unevaluated operand (Clause 5).
注意多态类(具有至少一个 virtual
成员的 class
的强调异常(exception)。
所以没关系
typeid( foo() )
并为 int
生成一个 std::type_info
对象,而 this
typeid( widget::get_instance() )
不是并且可能会产生链接时错误。它必须评估操作数,因为动态类型是通过在运行时查找 vptr
来确定的。
大小
§ 5.3.3 [expr.sizeof] ¶ 1:
The
sizeof
operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. Thesizeof
operator shall not be applied to an expression that has function or incomplete type, to an enumeration type whose underlying type is not fixed before all its enumerators have been declared, to the parenthesized name of such types, or to a glvalue that designates a bit-field.
以下
sizeof( foo() )
非常好,相当于 sizeof(int)
.
sizeof( widget::get_instance() )
也是允许的。但是请注意,它等同于 sizeof(widget)
,因此在多态 return
类型上可能不是很有用。
noexcept
§ 5.3.7 [expr.unary.noexcept] ¶ 1:
The
noexcept
operator determines whether the evaluation of its operand, which is an unevaluated operand (Clause 5), can throw an exception (15.1).
表达式
noexcept( foo() )
是有效的,计算结果为 false
。
这是一个更现实的例子,也是有效的。
void bar() noexcept(noexcept( widget::get_instance() ));
请注意,只有内部的 noexcept
是运算符,而外部是说明符。
decltype
§ 7.1.6.2 [dcl.type.simple] ¶ 4.4:
The operand of the
decltype
specifier is an unevaluated operand (Clause 5).
声明
decltype( foo() ) n = 42;
声明一个int
类型的变量n
并用值42初始化它。
auto baz() -> decltype( widget::get_instance() );
声明一个不带参数的函数baz
,并且return
是一个widget&
。
仅此而已(从 C++14 开始)。
关于c++ - 什么是 C++ 中未评估的上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35088599/