g++ C++0x enum class Compiler Warnings

夙愿已清 提交于 2019-12-08 16:45:35

问题


I've been refactoring my horrible mess of C++ type-safe psuedo-enums to the new C++0x type-safe enums because they're way more readable. Anyway, I use them in exported classes, so I explicitly mark them to be exported:

enum class  __attribute__((visibility("default"))) MyEnum : unsigned int
{
    One = 1,
    Two = 2
};

Compiling this with g++ yields the following warning:

type attributes ignored after type is already defined

This seems very strange, since, as far as I know, that warning is meant to prevent actual mistakes like:

class __attribute__((visibility("default"))) MyClass { };
class __attribute__((visibility("hidden")))  MyClass;

Of course, I'm clearly not doing that, since I have only marked the visibility attributes at the definition of the enum class and I'm not re-defining or declaring it anywhere else (I can duplicate this error with a single file).

Ultimately, I can't make this bit of code actually cause a problem, save for the fact that, if I change a value and re-compile the consumer without re-compiling the shared library, the consumer passes the new values and the shared library has no idea what to do with them (although I wouldn't expect that to work in the first place).

Am I being way too pedantic? Can this be safely ignored? I suspect so, but at the same time, having this error prevents me from compiling with Werror, which makes me uncomfortable. I would really like to see this problem go away.


回答1:


You can pass the -Wno-attributes flag to turn the warning off.

(It's probably a bug in gcc?)




回答2:


It works for me with g++ 4.8.2 the following way:

enum class MyEnum : unsigned int
__attribute__((visibility("default")))
{
    One = 1,
    Two = 2
};

(change the position of the attribute declaration)



来源:https://stackoverflow.com/questions/2463113/g-c0x-enum-class-compiler-warnings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!