You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

询问C++标准N4713中验证sizeof(S::m +42)的条款

Legality of 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:34:16