"ubyte[size] store = void" in std.variant
Steven Schveighoffer
schveiguy at yahoo.com
Tue Jun 15 08:24:48 PDT 2010
On Tue, 15 Jun 2010 11:18:07 -0400, Graham Fawcett <fawcett at uwindsor.ca>
wrote:
> On Tue, 15 Jun 2010 10:29:56 -0400, Steven Schveighoffer wrote:
>
>> On Tue, 15 Jun 2010 09:04:23 -0400, Graham Fawcett <fawcett at uwindsor.ca>
>> wrote:
>>
>>> Hi folks,
>>>
>>> The following statement appears in std.variant:
>>>
>>> 190 union
>>> 191 {
>>> 192 ubyte[size] store = void;
>>> 193 // conservatively mark the region as pointers 194
>>> static if (size >= (void*).sizeof) 195 void* p[size /
>>> (void*).sizeof]; 196 }
>>>
>>> The '= void' on line 192 sometimes leads to 'Error: void initializer
>>> has no value' errors in application code. For example, this fails to
>>> compile on DMD 2.047:
>>>
>>> foreach (int v; map! "a.get!int" (variantArray(1,2,3)))
>>> writeln(v);
>>>
>>> Changing line 192 to 'ubyte[size] store;' resolves the issue.
>>>
>>> My question is: what is the point of the '= void' initializer here?
>>> Would std.variant be broken if '= void' were removed?
>>
>> = void means don't initialize the data. Otherwise, the compiler/runtime
>> will fill in the data will all 0s. However, I'm not sure how that works
>> with a union, since you may have conflicting requirements for
>> initialization.
>
> I'm not sure if it's in the spec, but a quick test results in a
> compiler error if I declare a union with overlapping initializers.
>
> Simplifying the 'std.variant' case, I get the same 'void initializer
> has no value' error like this:
>
> struct foo {
> ubyte[] store = void;
> }
> foo z = foo();
>
> Is this a compiler bug?
Note, you can only use = void on a value type, not a dynamic array. The
variant union member is a static array, not a dynamic one.
But actually, now that I think about it, =void is generally used in a
function, not in a type definition. I'm not sure = void should be allowed
in that context. So maybe the compiler is right to complain...
-Steve
More information about the Digitalmars-d
mailing list