Fallback 'catch-all' template functions
Walter Bright via Digitalmars-d
digitalmars-d at puremagic.com
Sat Sep 3 02:19:19 PDT 2016
On 8/31/2016 10:37 PM, Manu via Digitalmars-d wrote:
> So, consider a set of overloads:
>
> void f(T)(T t) if(isSomething!T) {}
> void f(T)(T t) if(isSomethingElse!T) {}
> void f(T)(T t) {}
>
> I have a recurring problem where I need a fallback function like the
> bottom one, which should be used in lieu of a more precise match.
> This is obviously an ambiguous call, but this is a pattern that comes
> up an awful lot. How to do it in D?
import core.stdc.stdio;
void f(T:T)(T t) if(is(T == int)) { printf("case int\n"); }
void f(T:T)(T t) if(is(T == uint)) { printf("case uint\n"); }
void f(T)(T t) { printf("case default\n"); }
void main()
{
f(1);
f(1u);
f(1.0);
}
----
A bit odd, but far better than SFINAE.
More information about the Digitalmars-d
mailing list