By Adrian McCarthy

2010-02-12 17:48:47 8 Comments

Some C++ compilers permit anonymous unions and structs as an extension to standard C++. It's a bit of syntactic sugar that's occasionally very helpful.

What's the rationale that prevents this from being part of the standard? Is there a technical roadblock? A philosophical one? Or just not enough of a need to justify it?

Here's a sample of what I'm talking about:

struct vector3 {
  union {
    struct {
      float x;
      float y;
      float z;
    float v[3];

My compiler will accept this, but it warns that "nameless struct/union" is a non-standard extension to C++.


@bames53 2012-10-08 16:07:19

As others have pointed out anonymous unions are permitted in standard C++, but anonymous structs are not.

The reason for this is that C supports anonymous unions but not anonymous structs*, so C++ supports the former for compatibility but not the latter because it's not needed for compatibility.

Furthermore, there's not much use to anonymous structs in C++. The use you demonstrate, to have a struct containing three floats which can be referred to either by .v[i], or .x, .y, and .z, I believe results in undefined behavior in C++. C++ does not allow you to write to one member of a union, say .v[1], and then read from another member, say .y. Although code that does this is not uncommon it is not actually well defined.

C++'s facilities for user-defined types provide alternative solutions. For example:

struct vector3 {
  float v[3];
  float &operator[] (int i) { return v[i]; }
  float &x() { return v[0]; }
  float &y() { return v[1]; }
  float &z() { return v[2]; }

* C11 apparently adds anonymous structs, so a future revision to C++ may add them.

@Adrian McCarthy 2012-10-08 17:13:34

+1: My example relies on undefined behavior in C++--something I wasn't aware of back when I wrote the question.

@underscore_d 2016-06-09 14:29:25

"C++ does not allow you to write to one member of a union[...] and then read from another member" - unless said members are standard-layout objects and share a common initial sequence of members of their own, and you're writing/reading their members within said common initial sequence. That is allowed (i.e. defined).

@Nicol Bolas 2016-08-22 00:02:45

@underscore_d: Yes, if the types are standard layout with a common initial sequence. However, a struct can never alias with an array in this way, because the "common initial sequence" rules of C++ state that a common initial sequence can only be between structs. Arrays are not mentioned, so they cannot alias like this.

@underscore_d 2016-08-22 07:53:50

@NicolBolas Oh, haha - believe me - I have wished many times that arrays and other primitives were included in this allowance! But I've not thought much about the possible practical limitations on that, so probably it's not as simple as it currently seems. My comment was more general but might've risked implying by omission that I thought arrays were included in this, so thanks for adding that in.

@user743382 2017-09-08 23:45:54

"The reason for this is that C supports anonymous unions but not anonymous structs" -- No. Your footnote clarifies that you were talking about C99 or earlier here. The term "anonymous union" does not appear anywhere in the C99 standard. GCC claims in an diagnostic (with -std=c99 -pedantic options) that "ISO C99 doesn't support unnamed structs/unions". The standard doesn't mention anything about unnamed members other than unnamed bit-fields. I'm not entirely sure if struct-declarations are declarations, but if they are, anonymous unions are a constraint violation per 6.7p2, at best undefined.

@user743382 2017-09-09 12:22:05

Ah, figured out how it's supposed to work. struct-declarations aren't declarations, so 6.7p2 doesn't apply to them, but the syntax requires each struct-declaration to contain a struct-declarator. The only struct-declarator that is syntactically allowed to omit the declarator is an unnamed bit-field. In C99, anonymous unions are therefore a constraint violation outside of structures, and a syntax error as members of structures.

@Sergey.quixoticaxis.Ivanov 2018-05-26 23:36:08

+1 for the usage of C++ guarantees for built-in arrays and remark about C11. Coming from C# I still fail to understand why a lot of people dislike type& prop() access methods.

@bobobobo 2012-10-15 19:14:43

I'll say, you can clean up your vector3 declaration by just using a union

union vector3 {
  struct { float x, y, z; } ;
  float v[3] ;
} ;

Sure, anonymous structures was an MSVC extension. But ISO C11 permits it now, and gcc allows it, and so does Apple's llvm compiler.

Why in C11 and not C++11? I'm not sure, but practically speaking most (gcc++, MSVC++ and Apple's C++ compiler) C++ compilers support them.

@Adrian McCarthy 2012-10-15 19:34:45

+1 for updated information. The reason I had an outer struct was because the "real code" had methods as well.

@bobobobo 2012-10-15 22:27:49

The only things you can't do with a union are have static data members, or use inheritance.

@Adrian McCarthy 2012-10-17 20:10:04

Thanks. I never new a union could be used like a struct or class.

@irsis 2015-05-12 05:30:03

I know Sun studio didn't not support anonymous struct prior to C++11 by default. If you are writing cross platform code and compilers are not upgraded to C+11 then don't use anonymous struct.

@kennytm 2010-02-12 21:03:31

Your code

union {
  struct {
    float x;
    float y;
    float z;
  float v[3];

is like

union Foo {
   float v[3];

which is surely invalid (in C99 and before).

The reason is probably to simplify parsing (in C), because in that case you only need to check that the struct/union body has only "declarator statements" like

Type field;

That said, gcc and "other compilers" supports unnamed fields as an extension.

Edit: Anonymous structs are now officially supported in C11 (ยง6.7.2.1/13).

@Johannes Schaub - litb 2010-02-12 22:19:36

From a parsing perspective, i don't think that union { ... } is any different than struct { ... }. The former is valid, but the latter is not.

@Adrian McCarthy 2010-02-13 00:28:50

Given how absurdly difficult C++ is to parse in general, I doubt the standard committed disallowed unnamed structs and unions just to simplify parsing.

@kennytm 2010-02-13 05:32:38

@Adrian: I said C, not C++. C++ adopts C's syntax and extend it. Probably C++'s creators don't see a need to allow unnamed struct/union members so they don't mess with that part of syntax.

@bobobobo 2010-06-07 21:15:47

@Adrian, Good point there Adrian, I always didn't think "too hard to implement" would ever be a concern of Bjarne and crew

@bames53 2012-10-08 16:10:54

C and C++ both support unnamed unions, so the comment that union { ... }; is invalid is not correct.

@bames53 2012-10-08 16:15:52

@bobobobo difficulty of implementation affects many things in the C++ standard. For example, the places which say 'no diagnostic required' typically include that because detecting whatever error condition is being discussed is too difficult or impossible to implement in the general case.

@kennytm 2012-10-08 16:31:45

@bames53: C99 did not support unnamed struct fields. C11 does (edited the post to show that.) I don't think C++11 support unnamed struct field.

@bames53 2012-10-08 16:36:54

Correct, C++11 does not support anonymous structs, but does support anonymous unions. C99 is the same. C11 additionally supports anonymous structs. C11 does not add support for anonymous unions, because they were already supported in C99.

@JonM 2010-02-13 03:12:37

Based on the edit, the comments, and this MSDN article: Anonymous Structures, I'll hazard a guess - it fits poorly with the concept of encapsulation. I wouldn't expect a member of a class to mess with my class namespace beyond merely adding one member. Furthermore, changes to the anonymous structure can affect my class without permission.

@bames53 2012-10-08 16:24:07

Due to the way anonymous struct/unions are created (it's special, inline syntax that cannot be hidden except by a macro) you cannot be surprised that some member you're using is an anonymous member. So I don't think this reasoning makes any sense. The actual reason is that anonymous unions are supported in C++, for C compatibility only. C doesn't support anonymous structs (until C11) and so C++ doesn't either.

@David Thornley 2010-02-12 18:06:41

Unions can be anonymous; see the Standard, 9.5 paragraph 2.

What purpose do you see an anonymous struct or class as fulfilling? Before speculating why something isn't in the Standard, I'd like to have some idea why it should be, and I don't see a use for an anonymous struct.

@Dan 2010-02-12 18:03:35

Not sure what you mean. Section 9.5 of the C++ spec, clause 2:

A union of the form

union { member-specification } ;

is called an anonymous union; it defines an unnamed object of unnamed type.

You can do things like this too:

void foo()
  struct { // unnamed, is that what you mean by anonymous?
    int a;
    char b;
  } MyStructType; // this is more of a "C" style, but valid C++ nonetheless

  struct { // an anonymous struct, not even typedef'd
    double x;
    double y;
  } point = { 1.0, 3.4 };

Not always very useful... although sometimes useful in nasty macro definitions.

@Johannes Schaub - litb 2010-03-26 21:47:05

-1 because it's saying that it defines an anonymous struct. See comments above on the question - you are defining an unnamed struct, not an anonymous one.

Related Questions

Sponsored Content

1 Answered Questions

4 Answered Questions

[SOLVED] Aliasing struct and array the C++ way

1 Answered Questions

2 Answered Questions

[SOLVED] Why do both struct and class exist in C++?

2 Answered Questions

[SOLVED] Why does C++11 not support anonymous structs, while C11 does?

3 Answered Questions

[SOLVED] Are "anonymous structs" standard? And, really, what *are* they?

  • 2013-01-09 23:01:49
  • Lightness Races in Orbit
  • 27168 View
  • 54 Score
  • 3 Answer
  • Tags:   c++

5 Answered Questions

[SOLVED] Why does typedef struct produce a link failure

1 Answered Questions

7 Answered Questions

[SOLVED] Why does C have a distinction between -> and .?

  • 2009-11-28 21:46:02
  • bk.
  • 1930 View
  • 55 Score
  • 7 Answer
  • Tags:   c struct

Sponsored Content