这是 [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/