c++anonymous-classanonymous-struct

Is there really an Anonymous class/struct in C++?


I'm confused by many websites: People there refer to a class/struct as Anonymous when it has no name for example:

struct{
   int x = 0;
}a;

I think the example above creates an Unnamed struct but not an Anonymous struct. I think an Anonymous struct/class doesn't have a name nor a declarator after the close-curly brace that ends the class body and before the semi-colon that ends the class definition:

class { // Anonymous class
   int x_ = 0;
}; // no delcarator here

And of course the standard refuses such declaration above because it is ill-formed.

In this example above I've declared an unnamed (but not an anonymous) union and this is similar to the class/structs above.

So are my thoughts correct? if not please guide me. Thank you!


Solution

  • In the terminology of the C++ standard (N4659), only unions can be "anonymous". Neither the phrase "anonymous class" nor "anonymous struct" appear anywhere in the standard. In fact, the word "anonymous" itself appears only 44 times in the standard: 42 times followed by the word "union", and twice on its own underneath the "union" sublist of the Index.