Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ compiling C++11 using -Wpedantic option: Is there an option to disable only the warning about unnamed structs?

Tags:

c++

gcc

c++11

I want to keep any other checks -Wpedantic does but lose the warning about unnamed structs error: ISO C++ prohibits anonymous structs [-Wpedantic].

I want to be able to do the following:

union
{
  struct
  {
    float x, y, z, w;
  };
  struct
  {
    float r, g, b, a;
  };

  float v[4];
};

What I've found so far

I'm using C++11 and compiling with the -std=c++11 flag. I've read that C11 supports this feature, but I haven't seen any mention of it being supported in C++11.

I've come across mention of -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

I tried the flag and it doesn't appear to have any effect (no matter the permutation of ordering between -fms-extensions and -Wpedantic).

EDIT - More details

Thanks to the comments I've found the following:

  • Details about why unnamed classes/structs are not fully conformant with the standard

  • A post that claims my example code relies on undefined behavior

I'd still like to know if there is a method of enabling this gcc extension (which all compilers I know of have) that will disable the warning. Or is -Wpedantic all or nothing?

like image 202
Peter Clark Avatar asked Jun 09 '14 06:06

Peter Clark


People also ask

How do I turn off warnings in GCC?

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. This warning is enabled by -Wall .

Which GCC option should be used to do only compilation?

To produce only the compiled code (without any linking), use the -C option.

What is fPIC option in GCC?

fPIC option in GCC enables the address of shared libraries to be relative so that the executable is independent of the position of libraries. This enables one to share built library which has dependencies on other shared libraries. fPIC stands for "force Position Independent Code".

How do I disable compiler optimization?

Use the command-line option -O0 (-[capital o][zero]) to disable optimization, and -S to get assembly file. Look here to see more gcc command-line options.


1 Answers

You can disable -Wpedantic temporarily, for example if you have old code in some include file:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#include "old_header.hpp"
#pragma GCC diagnostic pop

Of course you could also do this on every occasion where you are using an anonymous struct to limit the scope where pedantic is disabled, but when you are doing that you could as well just go ahead and fix the code itself :)

like image 134
Daniel Frey Avatar answered Oct 20 '22 21:10

Daniel Frey