Sparse Aggregate Assignment/Initialization (RAII)

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 8 02:31:00 PDT 2014


On Monday, 7 July 2014 at 21:49:22 UTC, Justin Whear wrote:
> On Mon, 07 Jul 2014 21:34:05 +0000, Nordlöw wrote:
>
>> However using this function through UFCS
>> 
>>      auto cx = new C().set!"x"(11);
>> 
>> fails as
>> 
>> algorithm_ex.d(1257,17): Error: template algorithm_ex.set 
>> cannot deduce
>> function from argument types !("x")(C, int), candidates are:
>> algorithm_ex.d(1242,7):        algorithm_ex.set(string member, 
>> T,
>> U)(ref T a, in U value) if (isAggregateType!T && hasMember!(T,
>> member))
>> 
>> Instead I have to use
>> 
>>      auto c = new C(); set!"x"(c, 11);
>> 
>> which is not as elegant.
>> 
>> Why doesn't UCFS work here and is there a solution using DMD 
>> git master?
>
> You need to use `auto ref` to have this work with both classes 
> and
> structs.  A working version of your code here: auto dx = 
> D().set!"x"(11);
> 	assert(dx.x == 11);

To elaborate:
`new C()` is an r-value, and references can only be taken from 
l-values.


More information about the Digitalmars-d-learn mailing list