Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 468
Travis Gockel Avatar asked Mar 17 '10 14:03

Travis Gockel


People also ask

How do I stop a GCC warning?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused.

What are compiler warnings?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.

How do you stop warnings in Cmake?

Use -Wno-dev to suppress it. You can disable the warning like this when you are configuring your build.

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.


2 Answers

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

(It's probably a bug in gcc?)

like image 171
kennytm Avatar answered Sep 23 '22 01:09

kennytm


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)

like image 20
erenon Avatar answered Sep 22 '22 01:09

erenon