c++ - UB是否将第一个联合成员重新解释为活动联合成员的类型?
问题描述
给定代码:
struct A { int i; };
struct B { std::string s; };
struct C
{
union
{
A a{};
B b;
};
};
以下访问是否定义良好:
void foo()
{
C c; // c.a is active
c.b = {}; // now c.b is active
auto& b = reinterpret_cast<B&>(c.a); // read c.b via access to c.a
// use b somehow
}
解决方案
根据“注释”部分中的cppreference,将指向联合的一个成员的指针重新解释为联合的另一个成员的类型的指针是完全合法的。
假设满足对齐要求,则 reinterpret_cast 不会更改指针的值,但处理指针可互转换对象的一些有限情况除外:
struct S1 { int a; } s1;
struct S2 { int a; private: int b; } s2; // not standard-layout
union U { int a; double b; } u = {0};
int arr[2];
int* p1 = reinterpret_cast<int*>(&s1); // value of p1 is "pointer to s1.a" because s1.a
// and s1 are pointer-interconvertible
int* p2 = reinterpret_cast<int*>(&s2); // value of p2 is unchanged by reinterpret_cast and
// is "pointer to s2".
int* p3 = reinterpret_cast<int*>(&u); // value of p3 is "pointer to u.a": u.a and u are
// pointer-interconvertible
double* p4 = reinterpret_cast<double*>(p3); // value of p4 is "pointer to u.b": u.a and u.b
// are pointer-interconvertible because both
// are pointer-interconvertible with u
int* p5 = reinterpret_cast<int*>(&arr); // value of p5 is unchanged by reinterpret_cast and
// is "pointer to arr"
推荐阅读
- docker - 在 Docker 容器中使用 https 运行 ASP.Net Core API
- python - 避免编写具有大量列的数据框
- unreal-engine4 - 虚幻引擎 4.26.2 无法为 Android 构建包
- html - 根据寡妇大小调整内容 div 的大小,其余元素将保持不变
- angular - Angular 12 formGroup 问题:“HTMLElement”类型缺少“FormGroup”类型的以下属性
- python - 如何解决此错误:OSError: [Errno 9] Bad file descriptor
- python - 在 Python 中抓取包含某些字符和名称的文本?
- firebase - 如何过滤firebase中的子集合(颤振)?
- javascript - 当我从 Nodejs 发布到在 Django 休息框架上运行的 API 时。它返回 200 ok 但在数据库中没有保存任何内容
- xamarin.forms - Image SizeChanged 事件仅以 xamarin 形式触发