【发布时间】:2011-09-26 09:19:00
【问题描述】:
是否可以与 const 成员建立匿名联合?我有以下内容:
struct Bar {
union {
struct { const int x, y; };
const int xy[2];
};
Bar() : x(1), y(2) {}
};
使用 G++ 4.5 我得到错误:
error: uninitialized member ‘Bar::<anonymous union>::xy’ with ‘const’ type ‘const int [2]’
【问题讨论】:
-
struct { const int x, y; };不是有效的 C++。你是在问你是否可以用 GCC 做点什么? -
为什么无效?是否需要带有初始化列表的构造函数?
-
GCC -pedantic 开关似乎很有用。 “ISO C++ 禁止匿名结构”它说。如果我删除上面的
consts,我会收到同样的警告。
标签: c++ constructor constants unions