Choosing an Approach for the Template Lowering of _d_arrayctor

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


On Thursday, 25 November 2021 at 15:10:10 UTC, Paul Backus wrote:
> On Thursday, 25 November 2021 at 14:59:54 UTC, Stanislav Blinov 
> wrote:
>>
>> If you take the first parameter by reference, that shouldn't 
>> happen. I.e. in
>>
>> ```d
>> void _d_arrayctor(A,B)(return ref A to, scope B from)
>> if (is(A : X[]) && is (B : Y[]) && 
>> is(immutable(typeof(A.init[0])) == 
>> immutable(typeof(B.init[0])))) { /* ... */ }
>> ```
>>
>> `A` would be a static array, taken by reference.
>
> Unfortunately it is impossible to implement `_d_arrayctor` with 
> this signature without invoking undefined behavior. See the 
> discussion in this thread for details: 
> https://forum.dlang.org/thread/simesvkancmscrtsciwq@forum.dlang.org

Yikes. But then this goes the same for the "hack" version of the 
OP, does it not?..

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?..

```d
A _d_arrayctor(A,B)(B from)
if (appropriateContraintGoesHere)
{
     Unqual!(typeof(A.init[0]))[A.length] result = void;
     version (_D_BetterC) {}
     else scope(failure)
         foreach (ref it; result)
             emplaceInitializer(it); // this is nothrow
     copyEmplaceRange(from, result[]);
     return result;
}
```

though there's the question of calling appropriate copy ctors, 
"thanks" to all the qualifier combinations.


More information about the Digitalmars-d mailing list