Instantiation of nested structs should be allowed outside their parent scope

Stanislav Blinov stanislav.blinov at gmail.com
Tue Nov 9 15:44:13 UTC 2021


On Tuesday, 9 November 2021 at 15:39:48 UTC, Timon Gehr wrote:

>> template isNothrowCopyable(To, From)
>> if (is(immutable To == immutable From))
>> {
>>      enum bool isNothrowCopyable = is(typeof((ref scope From 
>> from) nothrow { union U { auto to=To.init; } U u = U(from); 
>> }));
>> }
>
> (I don't know if that solves your problem, as I don't actually 
> have an example where your implementation does not work.)

```d
template isNothrowCopyable(To, From = To)
if (is(immutable To == immutable From))
{
     enum bool isNothrowCopyable = is(typeof((ref scope From from) 
nothrow { union U { auto to=To.init; } U u = U(from); }));
}

unittest
{
     int dtors;
     struct NestedThatPasses
     {
         ~this() { ++dtors; }
     }

     struct NestedThatFails
     {
         this(return ref scope typeof(this)) nothrow {}
         ~this() { ++dtors; }
     }

     static assert(isNothrowCopyable!NestedThatPasses);
     static assert(isNothrowCopyable!NestedThatFails);
}
```


More information about the Digitalmars-d mailing list