How to specialize templates for l-value and non-l-value arguments?

Roman D. Boiko rb at d-coding.com
Thu Jun 14 09:38:04 PDT 2012


On Thursday, 14 June 2012 at 16:29:31 UTC, Timon Gehr wrote:
> On 06/14/2012 06:20 PM, Roman D. Boiko wrote:
>> On Thursday, 14 June 2012 at 16:08:54 UTC, Roman D. Boiko 
>> wrote:
>>> On Thursday, 14 June 2012 at 16:05:52 UTC, Timon Gehr wrote:
>>>> You can overload based on 'ref'.
>>>>
>>>> auto just(ref immutable(T) data) { return Maybe!T(&data); }
>>>> auto just(immutable(T) data) { return Maybe!T([data].ptr); }
>>> Great! Thanks
>> Hmm... seems to fail for immutable:
>>
>> immutable a = 2.0f;
>> #line 121
>> auto y0 = Maybe!float.just(a);
>> assert(!y0.empty);
>> assert(y0.front == a);
>>
>> Compiler output:
>> fds/persistent.d(121): Error: 2 is not an lvalue
>> dmd: glue.c:1114: virtual unsigned int Type::totym(): 
>> Assertion `0' failed.
>> Aborted (core dumped)
>> make: *** [build/client] Error 134
>>
>
> You have found a compiler bug.

Looks like :) So far I decided to remove ref overload. If I want 
to avoid copying, I can use maybeCreate(Immutable(T*) payload).


More information about the Digitalmars-d-learn mailing list