Unions destructors and GC precision

Paulo Pinto pjmlp at progtools.org
Tue Aug 14 23:02:54 PDT 2012


On Wednesday, 15 August 2012 at 05:10:02 UTC, Jonathan M Davis 
wrote:
> On Wednesday, August 15, 2012 07:02:25 Simen Kjaeraas wrote:
>> On Tue, 14 Aug 2012 22:32:58 +0200, Andrei Alexandrescu
>> 
>> <SeeWebsiteForEmail at erdani.org> wrote:
>> > On 8/14/12 3:25 PM, bearophile wrote:
>> >> D2 doesn't give you that restriction, and when an union 
>> >> goes out of
>> > 
>> >> scope it calls the destructors of all its fields:
>> > That's pretty surprising. "Major bug" doesn't begin to 
>> > describe it.
>> > 
>> > Unions should call no constructors and no destructors.
>> 
>> That means the default case is unsafe. Should it also be an 
>> error
>> (or at least a warning) for a union containing types with 
>> destructors
>> or complex constructors not to have a defined 
>> constructor/destructor?
>
> I wouldn't expect unions to be considered @safe in the first 
> place. You're
> potentially reintrepreting one type as another with them. And I 
> would expect
> that anything in them is in the same boat that anything 
> initialized to void
> is. e.g.
>
> Type var = void;
>
> - Jonathan M Davis

I second this.

That is actually one of the reasons why most languages with GC, 
ban pointer
uses to unsafe sections, otherwise the GC would be very 
restricted in the ways it could work.

Same thing about unions, as you wouldn't know which 
pointer/reference is the active one without some kind of tagging.

--
Paulo



More information about the Digitalmars-d mailing list