【发布时间】:2018-06-12 00:23:07
【问题描述】:
我可以将T 和包装好的T 放在union 中,然后随意检查它们吗?
union Example {
T value;
struct Wrapped {
T wrapped;
} wrapper;
};
// for simplicity T = int
Example ex;
ex.value = 12;
cout << ex.wrapper.wrapped; // ?
C++11 标准仅保证对通用初始序列进行保存检查,但value 不是struct。我猜答案是否,因为wrapped types aren't even guaranteed to be memory compatible to their unwrapped counterpart 和accessing inactive members is only well-defined on common initial sequences。
【问题讨论】:
标签: c++ struct types language-lawyer unions