相关疑难解决方法(0)

与const和nonconst成员的联合?

这似乎是未定义的行为

union A {
  int const x;
  float y;
};

A a = { 0 };
a.y = 1;
Run Code Online (Sandbox Code Playgroud)

规范说

在存储位置创建一个具有静态,线程或自动存储持续时间的const对象占用的新对象,或者在此生命周期结束之前这样的const对象占用的存储位置处导致未定义的行为.

但是没有编译器警告我,因为它很容易诊断错误.我是否误解了措辞?

c++ const undefined-behavior unions

21
推荐指数
2
解决办法
2305
查看次数

标签 统计

c++ ×1

const ×1

undefined-behavior ×1

unions ×1