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

11,415

Solution 1

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.

Solution 2

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.

Share:
11,415

Related videos on Youtube

Jeff Walden
Author by

Jeff Walden

I hack on Firefox -- most particularly the JS engine, but I've hacked on layout, DOM, networking, and other code too. (If you're using a WebKit-based browser, I've hacked out a patch or two for them. If you're using Opera, well, it's difficult to contribute to a program that's not open source. If you're using IE, I pity the fool.) The picture's me at the end of a southbound Appalachian Trail thru-hike -- most awesome thing I've ever done in my life. I've also thru-hiked the John Muir Trail (excellent scenery and hiking, too short), done the Coast to Coast Walk across England (an enjoyable way to visit England, but not backpacking), and expect to thru-hike other trails (definitely Pacific Crest Trail, maybe Long Trail, likely others). In other crazy outdoor activities, I enjoy biking. My biggest trip crossed the US, 37 days/~3875mi (not enough time, but also barely enough). Loved it all, could have used more time (externally-imposed deadline forced the pace). I think I prefer backpacking to cycling -- more remote, less like normal life.

Updated on March 18, 2020

Comments

  • Jeff Walden
    Jeff Walden about 4 years

    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?

    • GManNickG
      GManNickG over 12 years
      I disagree with the vote that this question is not constructive. Softer questions are fine too.
    • bobobobo
      bobobobo over 11 years
      Practically speaking most C++11 compilers support anonymous structs as well. I've used them in both MSVC++ (since, ever) and with Apple's llvm C++11 compiler.
    • Jamin Grey
      Jamin Grey over 9 years
      MinGW supports anonymous structs/unions as well.
  • Jeff Walden
    Jeff Walden over 12 years
    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.
  • Johannes Schaub - litb
    Johannes Schaub - litb over 12 years
    "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.
  • J. C. Salomon
    J. C. Salomon over 12 years
    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++.
  • Deduplicator
    Deduplicator over 9 years
    "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.
  • Tim Čas
    Tim Čas over 8 years
    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
    Tim Čas over 8 years
    @Deduplicator: Also, anonymous unions are supported (even in C++98), despite (optionally) having constructors.
  • 6502
    6502 over 8 years
    I find the "little magical" bit amusing. To me looks more like "This is not C. We decide. neener-neener."
  • Dwayne Robinson
    Dwayne Robinson over 8 years
    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). stackoverflow.com/questions/11016220/…