MSDN reckons that anonymous structs are non-standard in C++:
A Microsoft C extension allows you to declare a structure variable within another structure without giving it a name. These nested structures are called anonymous structures. C++ does not allow anonymous structures.
You can access the members of an anonymous structure as if they were members in the containing structure.
@K-ballo agrees.
I'm told that this feature isn't necessarily the same as just creating an unnamed struct but I can't see a distinction in terms of standard wording.
C++11 says:
[C++11: 9/1]
: [..] A class-specifier whose class-head omits the class-head-name defines an unnamed class.
and provides an entire grammatical construction for a type definition missing a name.
C++03 lacks this explicit wording, but similarly indicates that the identifier
in a type definition is optional, and makes reference to "unnamed classes" in 9.4.2/5
and 3.5/4
.
An anonymous struct declaration is a declaration that declares neither a tag for the struct, nor an object or typedef name. Anonymous structs are not allowed in C++. The -features=extensions option allows the use of an anonymous struct declaration, but only as member of a union.
A structure is a class defined with the struct keyword. Its members and base classes are public by default. In practice, structs are typically reserved for data without functions.
The C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.
Anonymous unions/structures are also known as unnamed unions/structures as they don't have names. Since there is no names, direct objects(or variables) of them are not created and we use them in nested structure or unions. Definition is just like that of a normal union just without a name or tag.
All the standard text refers to creating an "unnamed struct":
struct { int hi; int bye; };
Just a nice friendly type, with no accessible name.
In a standard way, it could be instantiated as a member like this:
struct Foo { struct { int hi; int bye; } bar; }; int main() { Foo f; f.bar.hi = 3; }
But an "anonymous struct" is subtly different — it's the combination of an "unnamed struct" and the fact that you magically get members out of it in the parent object:
struct Foo { struct { int hi; int bye; }; // <--- no member name! }; int main() { Foo f; f.hi = 3; }
Converse to intuition†, this does not merely create an unnamed struct that's nested witin Foo
, but also automatically gives you an "anonymous member" of sorts which makes the members accessible within the parent object.
It is this functionality that is non-standard. GCC does support it, and so does Visual C++. Windows API headers make use of this feature by default, but you can specify that you don't want it by adding #define NONAMELESSUNION
before including the Windows header files.
Compare with the standard functionality of "anonymous unions" which do a similar thing:
struct Foo { union { int hi; int bye; }; // <--- no member name! }; int main() { Foo f; f.hi = 3; }
† It appears that, though the term "unnamed" refers to the type (i.e. "the class" or "the struct") itself, the term "anonymous" refers instead to the actual instantiated member (using an older meaning of "the struct" that's closer to "an object of some struct
y type"). This was likely the root of your initial confusion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With