12

The following sample code compiles just fine in Visual C++:

class Test {
private:
    struct {
        struct {
            int privateData;
        };
    };
};

int main(int, char **)
{
    Test test;
    test.privateData = 0;
    return 0;
}

But why? I'd expect a compiler error because the privateData member should be inaccessible to the function main, since it's supposed to be private like its container's container. I know that nameless structs are not part of official C++, but this design is asinine.

By the way I've also tried to change private into protected and struct into union: it looks like the compiler refuses to honor access modifiers on anonymous structs and unions that are nested inside another anonymous struct or union.

Can someone explain this feature?

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
  • 3
    This sounds like the bug that was fixed in _VS2005sp1_, which version are you using? Keep in mind that _anonymous structs_ are not a standard _C++_ feature... – K-ballo Jan 06 '13 at 22:51
  • 2
    @K-ballo gcc also compiles this... – Luchian Grigore Jan 06 '13 at 22:52
  • 1
    BTW, the intellisense does complain but it nevertheless compiles – SomeWittyUsername Jan 06 '13 at 22:55
  • @Luchian Grigore: That doesn't make them any more standard... – K-ballo Jan 06 '13 at 22:55
  • @K-ballo I never said that, just pointing out a fact... – Luchian Grigore Jan 06 '13 at 22:56
  • 1
    Clang 4.0 does not compile this with an error "error: 'privateData' is a private member of 'Test'" –  Jan 09 '13 at 22:02
  • private applies to the name of a struct (and in no way to the struct itself or its content, really just the name). For an unnamed struct, it is not completely obvious what happens. – Marc Glisse Jan 09 '13 at 22:03
  • 1
    It works correctly if you put `private:` in the first anonymous struct and then nest however many anonymous structs beneath it. The answer seems to be: "it's a bug." – JCooper Jan 09 '13 at 22:13
  • Also interesting is the fact that the MSDN entry on anonymous structures gives code that can't compile (at least in 2008): http://msdn.microsoft.com/en-us/library/z2cx9y4f%28v=vs.90%29.aspx – JCooper Jan 09 '13 at 22:25
  • 1
    `test.privateData` shouldn't even exist! Where is an instance of those un-named structs? – Lightness Races in Orbit Jan 09 '13 at 22:49
  • 1
    @K-ballo: Unnamed structs are not standard...?! Then explain `[C++11: 9/1]: [..] A class-specifier whose class-head omits the class-head-name defines an unnamed class.` – Lightness Races in Orbit Jan 09 '13 at 22:50
  • @LightnessRacesinOrbit: Oh, so are they standard now in _C++11_? That's a change that slipped under my radar... – K-ballo Jan 09 '13 at 22:52
  • @K-ballo: The `identifier` is optional in C++03 too, AFAICT (though the same explicit wording is not present, 9.4.2/5 and 3.5/4 both make direct reference to "unnamed classes"). I've never heard of this being non-standard. – Lightness Races in Orbit Jan 09 '13 at 22:54
  • @LightnessRacesinOrbit: Check http://msdn.microsoft.com/en-us/library/z2cx9y4f.aspx I'm guessing it needs to be updated now? Or are we talking about different things? Note this is not _just_ about a struct with no name – K-ballo Jan 09 '13 at 22:56
  • @K-ballo: It seems to me that this page needed to be updated fifteen years ago. Let's not use MSDN as an authoritative source on what is and isn't standard C++... – Lightness Races in Orbit Jan 09 '13 at 22:57
  • @LightnessRacesinOrbit: Ok then, do the _unnamed classes_ in the standard (both 03 and 11) have its members in the enclosing struct or class as _anonymous classes_? Note anonymous != unnamed – K-ballo Jan 09 '13 at 22:58
  • @LightnessRacesinOrbit: Now a quick check of the _C++11_ standard seems to suggest that while _anonymous unions_ are still allowed, _anonymous structs_ are still not... You seem to be confusing _unnamed_ with _anonymous_... Think _anonymous namespace_... – K-ballo Jan 09 '13 at 23:00
  • @K-ballo: I don't see any distinction between unnamed and anonymous. I started [a new question](http://stackoverflow.com/questions/14248044/are-anonymous-structs-standard) to address this. – Lightness Races in Orbit Jan 09 '13 at 23:02
  • For the record -- that other Q&A concludes that I was wrong :) – Lightness Races in Orbit Jan 09 '13 at 23:13
  • @JCooper when I put `private:` in the first anonymous struct VS 2012 complains because of a syntax error in the struct definition, not because of the access to a private member. – GOTO 0 Jan 10 '13 at 06:51

1 Answers1

6

Yes, it is a bug. Microsoft acknowledged it is, the feedback report is here.

Right now the bug is in "will not fix" status and it is unclear when (if ever) it will be addressed. There is a somewhat odd workaround for it, the IntelliSense parser built into Visual Studio, written by the Edison Design Group, does complain about it. You get the red squiggles and the message:

Error: member "Test.privateData" (declared at line 10) is inaccessible

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 2
    Are you suggesting that there are hordes of developers displaced in time who _rely_ on access specifiers not properly cascading through two levels of anonymous structs? – Lightness Races in Orbit Jan 10 '13 at 02:04
  • @LightnessRacesinOrbit My understanding is there aren't hordes, but a pack that like to use microsoft's "workarounds", and for these people they do not fix this and similar issues. Instead, they mark it with their code parser – BЈовић Jan 10 '13 at 07:22
  • @BЈовић: It just seems to me like an _incredibly_ narrow edge case that wouldn't be worth such considerations, _even_ when there's also arguably little benefit in fixing it... – Lightness Races in Orbit Jan 10 '13 at 15:12
  • @LightnessRacesinOrbit Good. We agree :) – BЈовић Jan 10 '13 at 15:54
  • @LightnessRacesinOrbit like Luchian Grigore notes in a comment to the question, this "bug" can be successfully reproduced in gcc. In my understanding, this is by no means a matter of chance and compatibility requirements could likely be a good reason in support of the current design. – GOTO 0 Jan 10 '13 at 22:32
  • It is a bit remarkable that Microsoft agrees that GCC has a bug. I should have stuck with my original answer but got too much flak about it. Tends to happen when Lightness throws around his weight. – Hans Passant Jan 10 '13 at 22:41
  • @Hans: I didn't give you any "flak". I asked a follow-up question. Let's leave the personal attacks aside, okay? – Lightness Races in Orbit Jan 11 '13 at 00:09
  • @ft1: Okay that seems like a reasonable interpretation – Lightness Races in Orbit Jan 11 '13 at 00:10