"sizeof expr"用的是静态还是动态类型?
Static or dynamic type is used for "sizeof expr"?
expr
的静态类型还是动态类型用于 sizeof expr
?
请引用 C++17 标准。
参考sizeof
。
具体来说:
When applied to an expression, sizeof
does not evaluate the expression, and even if the expression designates a polymorphic object, the result is the size of the static type of the expression.
这是以下引自 C++ 标准的可读性更强的版本 ([expr.sizeof]) :
The operand is either an expression, which is an unevaluated operand (Clause 8), or a parenthesized type-id.
The result of applying sizeof
to a base class subobject is the size of the base class type.
The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand.
这里type是一个术语,表达式的类型定义在[expr]的每个子句中,你可以看到它总是引用静态类型。
例如,考虑
struct Base {};
struct Derived : Base {};
Base *pb = new Derived;
auto s = sizeof(*pb);
在这个例子中,根据[expr.sizeof]/1,sizeof(*pb)
的结果是*pb
的类型。要查看 *pb
的类型是什么,我们转向 [expr.unary.op]/1:
... If the type of the expression is “pointer to T”, the type of the result is “T”.
然后我们转向[expr.prim.id.unqual]/2:
The result is the entity denoted by the identifier. ... Otherwise, the type of the expression is the type of the result.
pb
表示的实体是指向Base
的指针类型的对象,表达式pb
也是,所以*pb
的类型是Base
,因此 sizeof(*pb)
的结果是 Base
.
的大小
expr
的静态类型还是动态类型用于 sizeof expr
?
请引用 C++17 标准。
参考sizeof
。
具体来说:
When applied to an expression,
sizeof
does not evaluate the expression, and even if the expression designates a polymorphic object, the result is the size of the static type of the expression.
这是以下引自 C++ 标准的可读性更强的版本 ([expr.sizeof]) :
The operand is either an expression, which is an unevaluated operand (Clause 8), or a parenthesized type-id.
The result of applying
sizeof
to a base class subobject is the size of the base class type.
The sizeof operator yields the number of bytes occupied by a non-potentially-overlapping object of the type of its operand.
这里type是一个术语,表达式的类型定义在[expr]的每个子句中,你可以看到它总是引用静态类型。
例如,考虑
struct Base {};
struct Derived : Base {};
Base *pb = new Derived;
auto s = sizeof(*pb);
在这个例子中,根据[expr.sizeof]/1,sizeof(*pb)
的结果是*pb
的类型。要查看 *pb
的类型是什么,我们转向 [expr.unary.op]/1:
... If the type of the expression is “pointer to T”, the type of the result is “T”.
然后我们转向[expr.prim.id.unqual]/2:
The result is the entity denoted by the identifier. ... Otherwise, the type of the expression is the type of the result.
pb
表示的实体是指向Base
的指针类型的对象,表达式pb
也是,所以*pb
的类型是Base
,因此 sizeof(*pb)
的结果是 Base
.