Fallback 'catch-all' template functions

Meta via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 1 10:55:39 PDT 2016


On Thursday, 1 September 2016 at 17:49:13 UTC, Timon Gehr wrote:
> On 01.09.2016 19:21, Meta wrote:
>> ...
>>
>> I just thought of this, but cannot test if it works. If it 
>> does, maybe
>> it would be a suitable solution?
>>
>> void f(T)(T t) if(isSomething!T) {}
>> void f(T)(T t) if(isSomethingElse!T) {}
>> //Taken if no other "overload" of f will intantiate with the 
>> given T
>> void f(T)(T t) if(!__traits(compiles, alias _ = .f!T)) {}
>
> It shouldn't work, but DMD currently seems to allow it. (If you 
> fix the syntax error.) I would expect it to break later.
>
>
> The following causes an ICE (DMD segfaults).
>
> import std.stdio;
>
> int f(T)(T t) if(!__traits(compiles,.f!T)) {
>     return 0;
> }
> int f(T)(T t) if(!__traits(compiles,.f!T)) {
>     return 1;
> }
>
> void main(){
>     writeln(f(2));
> }

The idea is that there'd only be one such "fallback" template, so 
that you cannot get into a situation such as this. I'm guessing 
the ICE is due to a recursive dependency between the two f 
templates?


More information about the Digitalmars-d mailing list