47

C11 supports anonymous structures, like so:

struct Foo
{
    struct
    {
        size_t x, y;
    };
};
struct Foo f;
f.x = 17;
f.y = 42;

Basically, the members of such a struct are treated as if they were members of the enclosing struct or union (recursively, if the enclosing structure was itself anonymous).

What was the rationale for C++11 not also including anonymous structures? They're only uncommonly useful (mostly inside unions, to eliminate the typing of an identifier for the struct), certainly. But they seem an obvious enough addition to the specification (and one already implemented by many compilers) that surely they must have been discussed, at the very least to preserve compatibility with the C11 standard. So why weren't they added?

Xeo
  • 129,499
  • 52
  • 291
  • 397
Jeff Walden
  • 7,008
  • 2
  • 38
  • 55

2 Answers2

47

Little effort has been made to maintain compatibility between C++ and C as the two languages evolve. Notice that variable length stack arrays have been in C since 1999, but weren't included in C++11. While they generally don't introduce things that contradict one another, the C++ committee isn't exactly bending over backwards to make sure that C++11 is compatible with versions of C beyond C89.

Furthermore, this feature would be quite complex in C++, because a struct is nothing more than a class. And an anonymous struct/class should have all of the features of a regular struct/class, yes? Otherwise, what's the point of having it?

What would it mean to construct a nameless struct? How would you define the constructor? Something as simple as:

struct Foo
{
    struct
    {
        size_t &x;
    };
};

is simply not possible because the inner struct has no constructor. And there's no way to specify one. A struct cannot construct the members of another struct within it.

For something like this:

struct Foo
{
    size_t outer;
    struct
    {
        void SomeFunc();
        size_t x;
    };
};

What this pointer does SomeFunc get? What would the type of this be, the nameless and unnamed type? How would you even define SomeFunc outside of the struct? The name of SomeFunc can't be Foo::SomeFunc, because SomeFunc lives in an inner scope.

It's just too complex for C++ to deal with. And certainly not worthwhile enough to bother with adding that complexity for.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 18
    This makes reasonable sense, although I can imagine restrictions which would make it entirely sensible: POD, no methods (inherited or otherwise), and fully public, I think, solve the issues you raise. It also seems to me that anonymous unions already introduce many of these issues, so the problems don't seem "too complex" to me, except in the sense of too little drive to solve them. – Jeff Walden Dec 24 '11 at 04:16
  • 15
    "What would it mean to construct a nameless struct? How would you define the constructor?". The same thing can be asked for a nameless union. Unions can have constructors too. – Johannes Schaub - litb Dec 24 '11 at 14:09
  • Additionally, most of what you get in C11 from anonymous structs you can also get from inheritance. Anonymous unions would be more useful, but as Nicol points out the whole feature is a poor fit for C++. – J. C. Salomon Jan 31 '12 at 17:15
  • 6
    "How would you define the constructor?" You don't, the inner anonymous structs members are members of the enclosing class, which might initialize them like any other member. – Deduplicator Nov 13 '14 at 21:01
  • @Deduplicator: Also, anonymous unions *are* supported (even in C++98), despite (optionally) having constructors. – Tim Čas Sep 30 '15 at 18:03
4

To play devil's advocate - class and struct declarations are used often to wrap class-specific type declarations.

typedef struct {

} name;

therefore should be allowable.

Therefore

struct {

} 

should be as well.

However, if we consider this as just a declaration within a class' internal namespace, there would be no way to access the inside of the struct.

Because struct != namespace in C, C can make up rules like accessing an anonymous struct through the surrounding struct.

For C++ to allow this it would need to special case this situation, which would complicate name resolution.

Of course, playing devil's devil's advocate - C actually did this. It added an extra level to name resolution - if you can't find the name in a stuct check the struct's anonymous members. Which is a little magical, in a way that I can see C++ committee members finding annoying.

It also raises questions - if an anonymous struct can be accessed through its parent class, what about anonymous structs in a namespace.

Of course, if you really want to know, just ask Stroustrup - he responds to emails.

legends2k
  • 31,634
  • 25
  • 118
  • 222
  • This can't be the right reason --- anonymous unions *are* supported (even in C++98!), but anonymous structs are not? If this were the reason, then anonymous unions should not be supported either, since they share the same namespace as structs. – Tim Čas Sep 30 '15 at 18:02
  • I find the "little magical" bit amusing. To me looks more like "This is not C. We decide. neener-neener." – 6502 Jan 09 '16 at 07:50
  • C++ already supports anonymous unions and anonymous namespaces where the contents spill out, as if they were declared inline at the outer scope. Anonymous structs complete that trio (particularly useful in graphics programming). In another aspect, C++11 even goes beyond C and supports inline namespaces (even when named, not just anonymous). http://stackoverflow.com/questions/11016220/what-are-inline-namespaces-for – Dwayne Robinson Feb 10 '16 at 05:22