c++ - C++ 标准中的哪一段验证了以下示例中使用的表达式 `sizeof(S::m + 42)`?

标签 c++ language-lawyer sizeof

这是 [expr.prim.id]/2 (2.3) 中的示例:

struct S {
    int m;
};
int i = sizeof(S::m); // OK
int j = sizeof(S::m + 42); // OK

我想知道标准 (N4713) 中的哪一段验证了上面使用的表达式 sizeof(S::m + 42)

最佳答案

正如@M.M 指出的,sizeof 的操作数可以是任何表达式 (8.5.2.3.1):

[...] The operand is either an expression, which is an unevaluated operand, or a parenthesized type-id.

8.5.2.3 中还提到了一些其他限制,但此处均不适用。

请注意,它提到它可以是一个未计算的操作数——这使得在这里使用非静态类成员S::m成为可能,参见(8.2 .3.1):

An unevaluated operand is not evaluated. [ Note: In an unevaluated operand, a non-static class member may be named ([expr.prim]) and naming of objects or functions does not, by itself, require that a definition be provided ([basic.def.odr]). An unevaluated operand is considered a full-expression. — end note ]

其中提到了您在问题 (8.4.4.2.3) 中提到的段落:

An id-expression that denotes a non-static data member or non-static member function of a class can only be used: [...] if that id-expression denotes a non-static data member and it appears in an unevaluated operand.

关于c++ - C++ 标准中的哪一段验证了以下示例中使用的表达式 `sizeof(S::m + 42)`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49523064/

相关文章:

c++ - 关于类型定义结构

c++ - 私有(private)新运营商是否有任何意想不到的副作用?

c++ - 功率(C++ - {模板})=功率(C++)?

c++ - 程序是否不使用无法绑定(bind)到引用参数的默认参数,是否合法?

c++ - 为什么 Doom 3 源代码中有 assert( sizeof( bool ) == 1 )?

c++ - 在编译时打印 sizeof(T)

c++ - VS2019 C++成员函数LNK2005错误

具有私有(private)复制构造函数的 C++11 std::is_convertible 行为

c++ - 为什么 std::array< T, 0 > 不为空?

c++ - sizeof(int)(或任何简单数据类型的任何 sizeof)在运行时是否有成本?