开发者

Why are global anonymous unions required to be declared as static?

开发者 https://www.devze.com 2023-01-22 19:37 出处:网络
C++ 0x draft 9.5.6 Anonymous unions declared in a named namespace or in the global namespace shall be decl开发者_如何学Cared static.

C++ 0x draft

9.5.6 Anonymous unions declared in a named namespace or in the global namespace shall be decl开发者_如何学Cared static.

Why is this?


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.


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)


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.


$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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消