Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ disallow anonymous structs?

Tags:

c++

struct

unions

Some C++ compilers permit anonymous unions and structs as an extension to standard C++. It's a bit of syntactic sugar that's occasionally very helpful.

What's the rationale that prevents this from being part of the standard? Is there a technical roadblock? A philosophical one? Or just not enough of a need to justify it?

Here's a sample of what I'm talking about:

struct vector3 {   union {     struct {       float x;       float y;       float z;     };     float v[3];   }; }; 

My compiler will accept this, but it warns that "nameless struct/union" is a non-standard extension to C++.

like image 657
Adrian McCarthy Avatar asked Feb 12 '10 17:02

Adrian McCarthy


People also ask

What is an anonymous struct in C?

Anonymous structsA 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.

Can a structure be considered without a name?

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.

What is struct anonymous?

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.

What is the difference between union and anonymous union?

An anonymous union is a union without a name. It cannot be followed by a declarator. An anonymous union is not a type; it defines an unnamed object. The member names of an anonymous union must be distinct from other names within the scope in which the union is declared.


2 Answers

As others have pointed out anonymous unions are permitted in standard C++, but anonymous structs are not.

The reason for this is that C supports anonymous unions but not anonymous structs*, so C++ supports the former for compatibility but not the latter because it's not needed for compatibility.

Furthermore, there's not much use to anonymous structs in C++. The use you demonstrate, to have a struct containing three floats which can be referred to either by .v[i], or .x, .y, and .z, I believe results in undefined behavior in C++. C++ does not allow you to write to one member of a union, say .v[1], and then read from another member, say .y. Although code that does this is not uncommon it is not actually well defined.

C++'s facilities for user-defined types provide alternative solutions. For example:

struct vector3 {   float v[3];   float &operator[] (int i) { return v[i]; }   float &x() { return v[0]; }   float &y() { return v[1]; }   float &z() { return v[2]; } }; 

* C11 apparently adds anonymous structs, so a future revision to C++ may add them.

like image 135
bames53 Avatar answered Oct 15 '22 06:10

bames53


I'll say, you can clean up your vector3 declaration by just using a union

union vector3 {   struct { float x, y, z; } ;   float v[3] ; } ; 

Sure, anonymous structures was an MSVC extension. But ISO C11 permits it now, and gcc allows it, and so does Apple's llvm compiler.

Why in C11 and not C++11? I'm not sure, but practically speaking most (gcc++, MSVC++ and Apple's C++ compiler) C++ compilers support them.

like image 23
bobobobo Avatar answered Oct 15 '22 07:10

bobobobo