Choosing an Approach for the Template Lowering of _d_arrayctor

Stanislav Blinov stanislav.blinov at gmail.com
Thu Nov 25 19:58:11 UTC 2021


On Thursday, 25 November 2021 at 18:50:28 UTC, Teodor Dutu wrote:
> On Thursday, 25 November 2021 at 16:16:11 UTC, Stanislav Blinov 
> wrote:
>> What can be done then? If I understand correctly, the union 
>> approach was arrived at due to void initialization, as in case 
>> of exception array of garbage data is getting destructed. But 
>> then, couldn't one just write a T.init into the whole array if 
>> an exception is thrown, and not rely on a union at all?..
>
> Actually, I used void initialisation because `T.init` couldn't 
> be used when T is a nested struct, because of the context 
> pointer, which is unavailable in the scope of `_d_arrayctor`.

I know, I was the one that suggested void initialization :D

> On Thursday, 25 November 2021 at 16:16:11 UTC, Stanislav Blinov 
> wrote:
>> 
>>>```
>>>    foreach (ref it; result)
>>>        emplaceInitializer(it); // this is nothrow
>>>```
>> ...pff, yeah, of course, and then we run into a segfault if 
>> it's an array of nested
>> structs, which was the problem "solved" with void 
>> initialization in the first place.
>> Not fun >:-E
>
> I think you've already touched on what I said above, here. It's 
> just that there wouldn't be a seg fault, but a compiler error, 
> because of the aforementioned context pointer.

I'm just not expressing myself clearly :) This won't be a compile 
error. The `emplaceInitializer` above would plop a null context 
pointer into `it`, which, if the destructor actually accesses 
context, naturally would lead to a segfault in __ArrayDtor.

This can be avoided though, if you leech the context pointer from 
the elements of `from` when emplacing the initializer.


More information about the Digitalmars-d mailing list