使用 GCC 4.9.3 静态常量匿名联合给出 "uninitialized const" 错误

Static const anonymous union gives "uninitialized const" error using GCC 4.9.3

编译时:

const static union {
    float cMaskF;
    int cMask = -1;
};

x86_64-w64-mingw32-gcc-5.2.0 成功,而 i686-pc-cygwin-gcc-4.9.24.9.3 都给出以下错误:

uninitialized const 'Uphil::Math::{anonymous}::' [-fpermissive]

不过,下面也会报错,可以理解...

const static union {
    float cMaskF = 1.0f;
    int cMask = -1;
};

multiple fields in union 'Uphil::Math::{anonymous}::' initialized

initializations for multiple members of 'Uphil::Math::{anonymous}::'

那么有没有办法让 const static 匿名联合编译一致?这是旧版本中已修复的错误,还是我不希望可移植的 "non-standard" 代码?无论如何,这似乎是一个有用的结构。

这都是在C++11下编译的。即使使用 -pedantic.

,v5.2.0 也会在没有警告的情况下成功

据我所知,这似乎是允许 const 匿名联合的 gcc 扩展。这适用于更高版本的 gcc,对于 example with gcc 5.2. It would seem the only way to get it working for older versions would be to use the -fpermissive 标志,它降低了对不一致代码的警告,但这可能是不可取的。

另一方面,如果我们使用 clang 尝试此代码,如果我们使用 -pedantic 标志 (see it live),它会提供以下警告:

warning: anonymous union cannot be 'const' [-Wpedantic]

我们可以找到一份 gcc 错误报告:type-specifier const in declaration of anonymous union 声称由于 7.1.6.1 [dcl.type.cv] 格式不正确其中说:

If a cv-qualifier appears in a decl-specifier-seq, the init-declarator-list of the declaration shall not be empty.

据我所知这是正确的。根据 9.5 [class.union]:

A union of the form

union { member-specification } ;

is called an anonymous union; it defines an unnamed object of unnamed type.