use*_*370 5 c++ constructor const unions
是否可以与const成员建立匿名联盟?我有以下内容:
struct Bar {
union {
struct { const int x, y; };
const int xy[2];
};
Bar() : x(1), y(2) {}
};
Run Code Online (Sandbox Code Playgroud)
使用G ++ 4.5,我收到错误:
error: uninitialized member ‘Bar::<anonymous union>::xy’ with ‘const’ type ‘const int [2]’
Run Code Online (Sandbox Code Playgroud)
这是 GCC 中的一个问题,已在 4.6 版本中修复。您的代码现在工作正常。
它仍然依赖于 GCC 扩展,因为它使用匿名结构,但现在大多数编译器都支持它们。另外,以下代码现在可以正确构建-pedantic:
struct Bar {
union {
const int x;
const int y;
};
Bar() : x(1) {}
};
Run Code Online (Sandbox Code Playgroud)
该代码也被 Clang 和 Visual Studio 2010 接受(但在 2008 中失败)。