The great inapplicable attribute debate

Don nospam at nospam.com
Tue Apr 14 05:24:04 PDT 2009


Stewart Gordon wrote:
>> Unions are normally aligned to the maximum alignment for any member. 
>> Now consider:
>>
>> union U {
>>     align(1) void* p;
>>     ubyte[size_t.sizeof] bytes;
>> }
>> union U2 {
>>     align(4) ubyte[12] data;
>>     char[12] str;
>> }
>> What are U.alignof and U2.alignof?
>>
>> According to that piece of the spec, those align() attributes should 
>> be useless.
>> However, according to DMD[1] U.alignof is 1, and without the attribute 
>> it's 4.
>> U2 on the other hand is align(1) regardless of attribute.
> <snip>
> 
> Sounds like a bug.

Definitely.
> 
> Surely, align isn't applicable to unions at all.  IINM the members of a 
> union, by design, start at the same offset.

Not so, the alignment of each member should be respected. Most 
obviously, a union U consisting of a single member x should have 
U.alignof == x.alignof.


   An anonymous struct within
> a union, or an anonymous union within a struct, might have alignment - 
> in either case, it would be in relation to the struct.

> 
> Stewart.



More information about the Digitalmars-d mailing list