108

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? Feb 12, 2010 at 18:18
  • 88
    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. 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.
    – greyfade
    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 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. Jan 10, 2013 at 18:07

7 Answers 7

60

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.

7
  • 3
    "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). Jun 9, 2016 at 14:29
  • 6
    @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. Aug 22, 2016 at 0:02
  • @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. Aug 22, 2016 at 7:53
  • "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
    Sep 8, 2017 at 23:45
  • 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.
    – user743382
    Sep 9, 2017 at 12:22
25

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
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
  • 15
    -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. Mar 26, 2010 at 21:47
1

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.

0
1

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
  • 1
    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.
    – bames53
    Oct 8, 2012 at 16:24
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
  • 5
    From a parsing perspective, i don't think that union { ... } is any different than struct { ... }. The former is valid, but the latter is not. Feb 12, 2010 at 22:19
  • 3
    Given how absurdly difficult C++ is to parse in general, I doubt the standard committed disallowed unnamed structs and unions just to simplify parsing. Feb 13, 2010 at 0:28
  • @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.
    – kennytm
    Feb 13, 2010 at 5:32
  • 1
    @Adrian, Good point there Adrian, I always didn't think "too hard to implement" would ever be a concern of Bjarne and crew
    – bobobobo
    Jun 7, 2010 at 21:15
  • C and C++ both support unnamed unions, so the comment that union { ... }; is invalid is not correct.
    – bames53
    Oct 8, 2012 at 16:10
-2

Edit: I put up a non-answer since I didn't realize the difference between an "anonymous struct" and an "unnamed struct". Rather than delete this answer, I will just leave it up, but my response here is not correct.

Original response follows:


I don't see it mentioned in any of the answers here, I guess since they're mostly written from before the "modern C++" era, but since I found my way here via a google search on "C++ anonymous struct", I'll just put this answer down here:

I'm able to do the following:

struct /* no typename */
{
    int i=2;
} g_some_object{};

int main()
{
    return g_some_object.i;
}

I noticed this behavior is actually leveraged in some of the examples for C++20 coroutines on cppreference, particularly for demonstrating a task awaiter.

If at any point in the past this behavior was not allowed, then that's no longer the case--we can definitely do this now.

1
  • 1
    You are using an unnamed struct. These have always been legal; they're just a structure that doesn't have a type name, but they have an instance which is of that type. The question is about anonymous structs which have no type name and no instance created -- which is what is in the question's example. Jul 1, 2022 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.