11

C++ 0x draft

9.5.6 Anonymous unions declared in a named namespace or in the global namespace shall be declared static.

Why is this?

cigien
  • 57,834
  • 11
  • 73
  • 112
Vatsan
  • 1,163
  • 9
  • 15

5 Answers5

9

Suppose anonymous unions were not required to be declared static, and the compiler encounters these two translation-units (after preprocessing):

File1:

union {
  int  a;
  char b;
};

// Further contents referring to a and b

File2:

union {
  int  a;
  char b;
};

// Further (different) contents referring to a and b

Are those two unions one an the same object, or are they supposed to be different objects?

I think that, in order to avoid unanswerable questions like this, it has been decided that namespace-scope anonymous unions have to be declared static.

Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
2

My best guess:

If it were non-static, it could be referenced by other code. But what would other code call it? It is anonymous. Hence, the need to restrict an anonymous union to some local scope; hence, it shall be declared static.

But its just a guess. Language Designers get to design things the way they want. Sometimes their choices are arbitrary, just because some choice must be made.

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • I don't see how what it is called in a different part of the code could be the problem - because it is not called anything. Rather, only the members are referenced directly, and they all have names. It's the union itself that's anon, not its members. – Vatsan Nov 02 '10 at 03:11
  • I guess I'm trying to get insights on why this feature is designed the way it is. BTW this behavior is not new to C++0x (even though I've quoted the C++0x draft) - it has been this way for a while now. – Vatsan Nov 02 '10 at 03:14
2

My guess is that if it were allowed to define the union in a non static way it may violate the ODR (one definition rule)

lothar
  • 19,853
  • 5
  • 45
  • 59
0

$9.5/5- A union of the form union { member-specification } ; is called an anonymous union; it defines an unnamed object of unnamed type.

My guess that it should be static so that the object can be initialized as per the rule of global static objects. If it is not static and the object does not have a name, then how does one initialize it?

EDIT2:

On rethinking...

Members of anonymous unions have internal linkage. Further by default global names have external linkage unless they have internal linkage. If the name of the anonymous union has external linkage, it is not possible for the members of the anonymous union to have internal linkage. Therefore anonymous unions are declared with 'static' storage class specifier so that the anonymous name itself has internal linkage.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
  • damn!. I don't know how to get rid of that double 'defines' – Chubsdad Nov 02 '10 at 03:56
  • Making it static will provide it with 'zero initialization'. But why is default (zero) initialization needed at all? It's members can be referred to by their names, so the first time a member of the anon-union is assigned a value, the anon-union will be initialized. What am I missing? – Vatsan Nov 02 '10 at 04:09
  • @Vatsan Madhavan: assignment is different from initialization – Chubsdad Nov 02 '10 at 04:12
  • You are right. But..what's wrong with an uninitialized global anonymous union? – Vatsan Nov 02 '10 at 04:49
-1

There was never a justification for the static requirement and it should be removed. The compiler does, and should, treat the multiple elements in the union as multiple individual global variables that share the same address. In practicality it means that the compiler allow multiple types to be applied to the same address. Since the scope of a global anonymous union is the global scope, the rules for naming elements in anonymous unions should be (and are) the same as the rules for naming global variables. i.e. anonymous union elements names must be unique. As for the initialization of union - there is no difference between the initialization of a union and of a simple variable. Another point about static unions -- the value and type of a union are time-dependent. Note that only one value at a time can occupy a union regardless of the number of elements in it. The reason for declaring a union to start with is to allow the same address to be used for different types, dynamically, at different times. This is why a static unions is a misnomer and some compilers simply ignore it.

Mik
  • 1