Troubles creating templated inout objects

Timoses timosesu at gmail.com
Thu Jul 12 14:17:49 UTC 2018


On Thursday, 12 July 2018 at 12:22:34 UTC, Steven Schveighoffer 
wrote:
> On 7/11/18 8:55 AM, Timoses wrote:
>>      class TestA(T : T[])
>>      {
>>          Test!T[] arr;
>> 
>>                  // ERROR: Can't initialize inout variable in 
>> a for loop...
>>          this(inout(T[]) arr) inout
>>          {
>>              // 1: Nope
>>              foreach (mem; arr)
>>                  this.arr ~= test(mem);
>> 
>>              // 2: Nope
>>              //Test!T[] a;
>>              //foreach (mem; arr)
>>              //   a ~= test(mem);
>>
>
> On the right track, but inside inout (or const or immutable) 
> constructors, the members can only be initialized once. So you 
> have to initialize a local, and then set the member once.
>
> The issue is, your input is *also* inout (a necessary 
> condition), so you didn't declare a properly:
>
> inout(Test!T)[] a;
> foreach (mem; arr) a ~= test(mem);
> this.arr = a;
>
> -Steve

Aw, thanks! This is much nicer than casting...


More information about the Digitalmars-d-learn mailing list