c++ - 什么是 C++ 中未评估的上下文?

标签 c++ c++11

经常想到的一个例子是:

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 a std::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. The sizeof 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/

相关文章:

c++ - 尾随返回类型的名称查找和类型简化规则是什么?

c++ - 迭代 vector 对并使用 std::copy 打印结果

c++ - clang 和 gcc 中的 Constexpr 复合赋值运算符

c++ - 如何重构容器以直接使用谓词方法?

c++ - 将 char 地址分配给 int 指针,然后写入 int 大小的内存块

c++ - 我可以使用另一种方式从 C++ 获取时间吗?

c++ - 自动和自动&和常量

c++ - C 中信号量的使用?

c++ - 使用 GCC 编译 C++ 时如何包含 C11 头文件?

c++ - WINAPI SQLite C 无法正确地将数据插入数据库