Why should global anonymous unions be declared static?

C ++ 0x draft

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

Why?


Update -

Based on Bart van Ingen Schenau and lothar respones, the best explanation so far may be the following:

If the same global anonymous union occurs in two translation units (for example, through a header file), then how can the rule of one definition be implemented? Are these two definitions the same and combined? Or are two definitions considered different? If they are regarded as the same thing, then the compiler, apparently, does the β€œmagic”, otherwise it is not done for other objects. If they are regarded as the same thing, then the compiler does this without the explicit consent of the programmer ... therefore, I assume that explicit consent forcibly requires that it be declared as static.

+6
c ++ c ++ 11 anonymous-types unions
source share
5 answers

My assumption is that if it were allowed to define the union in a non-stationary way, this could violate ODR (one rule of definition)

+1
source share

Suppose that anonymous joins should not 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 these two unions the same object, or should they be different objects?

I think that in order to avoid irrefutable questions like this, it was decided that anonymous unions with the namespace should be declared static.

+4
source share

My best guess:

If it were non-stationary, another code could refer to it. But what could another code name? This is anonymous. Therefore, it is necessary to limit anonymous union to some local area; therefore, it must be declared static.

But this is just a hunch. Language designers get the design as they want. Sometimes their choice is arbitrary, simply because some choice has to be made.

+1
source share

$ 9.5 / 5- Combining Form Combination {Member Specification}; called an anonymous union; it defines an unnamed object of an unnamed type .

I assume that it should be static so that the object can be initialized according to the rule of global static objects. If it is not static and the object does not have a name, then how to initialize it?

EDIT2:

About rethinking ...

Members of anonymous associations have an internal connection. In addition, by default, global names have an external link if they do not have an internal link. If the name of the anonymous union has an external connection, members of the anonymous union cannot have an internal connection. Therefore, anonymous unions are declared using a β€œstatic” storage class specifier, so the anonymous name itself has an internal relationship.

0
source share

There was never an excuse for a static requirement, and it should be removed. The compiler does and must process several elements in the union as several separate global variables that have the same address. In practical terms, this means that the compiler allows you to apply multiple types to the same address. Because the scope of a global anonymous union is a global scope, the rules for naming elements in anonymous associations must be (and are) the same as the rules for naming global variables. that is, the names of anonymous connections must be unique. Regarding the initialization of the union, there is no difference between the initialization of the union and a simple variable. Another point about static joins is that the value and type of join are time-dependent. Please 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 is to use the same address for different types, dynamically, at different times. This is why static joins are wrong, and some compilers simply ignore it.

-one
source share

All Articles