Union redux

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 1 19:32:58 PDT 2015


On 6/1/15 7:18 PM, deadalnix wrote:
> On Tuesday, 2 June 2015 at 00:11:09 UTC, Andrei Alexandrescu wrote:
>> On 6/1/15 3:43 PM, deadalnix wrote:
>>> On Monday, 1 June 2015 at 19:43:33 UTC, Andrei Alexandrescu wrote:
>>>> Do not define constructors. They ostensibly imply commitment to
>>>> remembering to call the appropriate destructor. A union can only be
>>>> default constructed (with its init value as described) and has no
>>>> destructor.
>>>>
>>>
>>> This is implied in C++ because of manual memory management. I don't
>>> think it is in D.
>>
>> It is because destructors exist in both languages.
>>
>
> It is common in D to have a struct with a constructor and no destructor.

Probably there's a misunderstanding then. I'm saying the right way to go 
is unions have no destructors at all.

>>>>> 5/ union currently disallow members with postblit and/or destructor
>>>>> . It
>>>>> seems that this was needed in C++ as per Andrei's comments. It
>>>>> seems to
>>>>> me that, because of manual memory management, C++ would have a lot
>>>>> more
>>>>> struct with copy constructor and/or destructor than in D, so I'm not
>>>>> sure if this require change in spec. Andrei, can you give more details
>>>>> on the C++ situation here ?
>>>>
>>>> That rule has hurt C++ everywhere, and the community credits Lois
>>>> Goldthwaite for pushing the current rule, which allows types with
>>>> cdtors in unions. Let's go the same way, unions are all but useless
>>>> otherwise.
>>>>
>>>
>>> Without knowing the rationale in C++, it is difficult to assert that
>>> this is also valid in D. You seems to be knowledgeable of the subject,
>>> so surely you can enlight us on that one.
>>
>> I intentionally gave enough information to allow easy further
>> research. http://goo.gl/19Nol5 lists N2412 and N2248 as top hits,
>> their motivation sections are relevant.
>>
>
> The abstract of the 2 above mentioned link only discuss the
> *constructor* case. It was never question of struct with constructor not
> being able to be in union.

Then again sorry I am misunderstanding something. I'm saying the right 
way to go is unions accept all types.

> You attitude is only weakening your point.

I'm not making a point. There is no debate. You said you want to work on 
unions, that would be great. But this is a cut and dried area. There's 
no room for creativity here. If you want to be creative and design new 
programming language facilities, do something else. For unions do what 
needs be done.


Thanks,

Andrei



More information about the Digitalmars-d mailing list