询问C++标准N4713中验证sizeof(S::m +42)的条款
sizeof(S::m + 42) in C++ Standard N4713 To confirm that sizeof(S::m + 42) is legal in N4713 (the draft corresponding to C++17), we can break down the reasoning using key clauses from the standard:
1. The sizeof operand is an unevaluated operand
First, per §8.3.3 (The sizeof operator) paragraph 1:
The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand. The operand is either an expression, which is an unevaluated operand (Clause 8), or a parenthesized type-id.
This means the expression inside sizeof is not evaluated—we only care about its type, not its runtime value. This is critical because non-static data members like S::m have special rules in unevaluated contexts.
2. Referencing non-static data members in unevaluated operands is allowed
For the id-expression S::m (a non-static data member), §8.1.4.2 (Id expressions for class members) paragraph 2 explicitly permits this in unevaluated operands:
If the id-expression denotes a non-static data member or non-static member function of a class C, the id-expression is only valid:
(2.1) — as part of a class member access (8.2.5) in which the object expression refers to an object of type C or a class derived from C, or
(2.2) — if it appears in an unevaluated operand.
Since S::m is inside the sizeof operand (an unevaluated context), this reference is valid.
3. The addition expression S::m + 42 is well-formed
The type of S::m is int (from the struct definition struct S { int m; };). Adding the integer literal 42 (also type int) is a valid arithmetic operation per §8.5.6 (Additive operators), which allows addition of arithmetic types. The resulting expression has type int, which is a valid type for sizeof to operate on.
Putting it all together
All components of sizeof(S::m +42) are valid under N4713: the unevaluated context allows referencing S::m, the addition is a valid arithmetic expression, and sizeof accepts the resulting int type. Thus, the expression is legal.
内容的提问来源于stack exchange,提问作者Alexander

