【发布时间】:2014-07-29 15:59:42
【问题描述】:
我想保留任何其他检查 -Wpedantic 但会丢失有关未命名结构的警告 error: ISO C++ prohibits anonymous structs [-Wpedantic]。
我希望能够做到以下几点:
union
{
struct
{
float x, y, z, w;
};
struct
{
float r, g, b, a;
};
float v[4];
};
到目前为止我发现了什么
我正在使用 C++11 并使用 -std=c++11 标志进行编译。我有 read that C11 supports this feature,但我没有看到 C++11 支持它的任何提及。
我听说过-fms-extensions:
In this SO question about C for which it is the accepted answer
In the GCC documentation for the flag's use when compiling C++ which doesn't give very many details
我尝试了该标志,但它似乎没有任何效果(无论-fms-extensions 和-Wpedantic 之间的顺序排列如何)。
编辑 - 更多细节
感谢 cmets,我发现了以下内容:
Details about why unnamed classes/structs are not fully conformant with the standard
A post that claims my example code relies on undefined behavior
我仍然想知道是否有一种方法可以启用这个 gcc 扩展(我知道的所有编译器都有)可以禁用警告。还是-Wpedantic 全部或全部?
【问题讨论】:
-
这在 C++ 中是非法的。见stackoverflow.com/q/13138605/774499。
-
@DavidHammen:显然。尽管如此,它仍然有效,并且有很多代码以这种方式使用联合来执行某种类型转换(这也是非法的)。
-
@VioletGiraffe:工会是不是有点不同,因为这样的声明确实在程序中引入了一个或多个名称?联合成员的范围与类成员不同。
-
@LightnessRacesinOrbit,@VioletGiraffe:据我所知,允许使用未命名的工会。我不确定标准对它们的描述,但它们至少在
-Wpedantic下编译得很好。 cppreference has a section on "anonymous unions" -
迂腐模式是迂腐的。当您使用任何类型的非标准功能时,编译器都会抱怨。可能会有人在 SO 上回答您的问题,或者您甚至可以自己解决问题,但请考虑丢弃那部分代码并用标准 C++11 编写它。您当然不想从所有这些 GCC 扩展中陷入兼容性地狱。