117

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++.

6
  • 4
    Clearly there's some confusion about what you mean. Could you please provide an example of code that only compiles due to a compiler extension? Commented Feb 12, 2010 at 18:18
  • 98
    Notice that there are two concepts, that sound similar, but are vastly different: unnamed structs and anonymous structs. The first is this one, which C++ supports: struct { int i; } a; a.i = 0; (the type has no name). The second is this one, which C++ does not support: struct { int i; }; i = 0; (the type has no name, and it escapes into the surrounding scope). C++, however, does support both unnamed and anonymous unions. Commented Feb 12, 2010 at 18:39
  • This looks like the rather interesting VMMLib vector library. I believe the problem is that the union contains an unnamed struct, but I'm unsure. Commented Feb 12, 2010 at 21:05
  • 1
    FWIW It's "anonmyous", not "unnamed", and unions are supported as litb says. stackoverflow.com/q/14248044/560648 Commented Jan 10, 2013 at 1:42
  • 1
    @AdrianMcCarthy: That's fine (FSVO "fine"; pesky compiler being cryptic), but precisely "unnamed" is an unrelated, standard concept. Commented Jan 10, 2013 at 18:07

6 Answers 6

67

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.

Sign up to request clarification or add additional context in comments.

7 Comments

"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).
@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.
@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.
"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.
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.
|
27

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.

4 Comments

The only things you can't do with a union are have static data members, or use inheritance.
Thanks. I never new a union could be used like a struct or class.
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.
If ISO permits this, why doesn't it work with GCC? stackoverflow.com/questions/72137275/…
3

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()
{
  typedef
  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.

1 Comment

-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.
3

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.

1 Comment

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.
2

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.

Comments

0

Your code

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

is like

union Foo {
   int;
   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).

8 Comments

From a parsing perspective, i don't think that union { ... } is any different than struct { ... }. The former is valid, but the latter is not.
Given how absurdly difficult C++ is to parse in general, I doubt the standard committed disallowed unnamed structs and unions just to simplify parsing.
@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.
@Adrian, Good point there Adrian, I always didn't think "too hard to implement" would ever be a concern of Bjarne and crew
C and C++ both support unnamed unions, so the comment that union { ... }; is invalid is not correct.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.