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:

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:

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?

Community
  • 1
  • 1
Peter Clark
  • 2,863
  • 3
  • 23
  • 37
  • That's illegal in C++. See http://stackoverflow.com/q/13138605/774499 . – David Hammen Jun 09 '14 at 07:12
  • 1
    @DavidHammen: obviously. Still, it works, and there's A LOT of code out there that uses unions in this way to perform some sort of type casting (which is illegal as well). – Violet Giraffe Jun 09 '14 at 07:14
  • @VioletGiraffe: Isn't it a bit different for unions, because such a declaration _does_ introduce one or more names into the program? Union members are scoped differently from class members. – Lightness Races in Orbit Jun 09 '14 at 07:18
  • @LightnessRacesinOrbit, @VioletGiraffe: From what I've found unnamed unions are allowed. I'm not sure what the standard says about them but they compile fine under `-Wpedantic` at least. [cppreference has a section on "anonymous unions"](http://en.cppreference.com/w/cpp/language/union) – Peter Clark Jun 09 '14 at 07:24
  • @PeterClark: [Quite](http://stackoverflow.com/a/14248127/560648). – Lightness Races in Orbit Jun 09 '14 at 07:26
  • @LightnessRacesinOrbit I meant unions of unnamed structs. – Violet Giraffe Jun 09 '14 at 07:27
  • @DavidHammen Is there any method to enable the GCC extension then (thus disabling this warning) or is it all or nothing with `-Wpedantic`? – Peter Clark Jun 09 '14 at 07:35
  • 2
    Pedantic mode is pedantic. When you use a non-standard feature of any kind, compiler complains. Probably, there will be someone on SO who answers your question, or you even manage to do it yourself, but consider throwing out that part of code and writing it in standard C++11. You certainly don't want to get into compatibility hell from all of those GCC extensions. – polkovnikov.ph Jun 09 '14 at 09:17

1 Answers1

6

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 :)

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • 1
    Since it looks like there's no command line method, this seems like the way to go. I agree about staying away from non-standard behaviour in most situations, but this particular extension provides some very handy syntax in certain applications [(such as a vector math library)](http://www.reedbeta.com/blog/2013/12/28/on-vector-math-libraries/) and all the compilers I use support it. – Peter Clark Jun 09 '14 at 11:59