Instantiation of nested structs should be allowed outside their parent scope

Timon Gehr timon.gehr at gmx.ch
Tue Nov 9 15:55:14 UTC 2021


On 09.11.21 16:44, Stanislav Blinov wrote:
> 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);
> }
> ```

It indeed seems ridiculous that you can't copy a nested struct using its 
copy constructor. I guess one solution would be to use the frame pointer 
of the instance you are about to copy to call the copy constructor.

I don't think calling it with `null` is a great idea.


More information about the Digitalmars-d mailing list