假设我们有两种类型,它们具有相同的表示形式(相同的成员变量和基类,顺序相同)。它们之间的 reinterpret_cast
是否有效(即不是 UB)?例如。从 Mary
到 Ashley&
的 reinterpret_cast
有效吗?如果这两种类型是多态的怎么办?
struct Mary {
int m1;
char m2;
};
struct Ashley {
int a1;
char a2;
};
int TryTwins ()
{
Mary mary = {};
Ashley& ashley = reinterpret_cast<Ashley&> (mary);
ashley.a1 = 1;
ashley.a2 = 2;
return mary.m1 + mary.m2;
}
如果我们知道源类型以目标类型的成员变量开头,那么我们将对象的开头转换为另一种类型会怎样?例如。这是有效的(即不是 UB)吗?
struct Locomotive {
int engine;
char pantograph;
};
struct Train {
int engine;
char pantograph;
int* wagon1;
int** wagon2;
int*** wagon3;
};
int TryTrain ()
{
Train train = {};
Locomotive& loc = reinterpret_cast<Locomotive&> (train);
loc.engine = 1;
loc.pantograph = 2;
return train.engine + train.pantograph;
}
请注意,所有主要编译器都将这些视为有效的强制转换 ( live demo )。问题是,C++ 语言是否允许这样做。
最佳答案
A glvalue expression of type
T1
can be cast to the type “reference toT2
” if an expression of type “pointer toT1
” can be explicitly converted to the type “pointer toT2
” using areinterpret_cast
. The result refers to the same object as the source glvalue, but with the specified type. [...]
Mary
和 Ashley
是对象类型,因此指向它们的指针可以相互转换。现在,我们使用 Ashley
类型的左值来访问底层 Mary
对象。
If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
the dynamic type of the object,
a cv-qualified version of the dynamic type of the object,
a type similar to the dynamic type of the object,
a type that is the signed or unsigned type corresponding to the dynamic type of the object,
a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,
an aggregate or union type that includes one of the aforementioned types among its elements or non-static data members (including, recursively, an element or non-static data member of a subaggregate or contained union),
a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
a
char
,unsigned char
, orstd::byte
type.
这些都不涵盖有问题的情况。 (“类似”谈论简历资格。)因此,未定义的行为。
关于c++ - 你可以在具有相同表示的类型之间进行reinterpret_cast吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58595488/