Querying Function Template Restrictions

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 9 10:00:22 PDT 2016


On Monday, 9 May 2016 at 11:57:11 UTC, Nordlöw wrote:
> In what ways can I compile-time introspect the template 
> restrictions of a specific function overload set in D?
>
> In other words if I have, for instance,
>
> T f(T)(T x) if (isFloat!T) {}
> T f(T)(T x) if (isInteger!T) {}
>
> T g(T)(T x) if (isFloat!T && isInteger!T) {}
>
> can I somehow at compile-time query that
>
> - f() is a function having *two* overloads where the first 
> takes an integer and the second takes a float?
> - g() is a function having *one* overload taking either an 
> integer or a float?
>
> Or is it only via trial-and-error calls such as
>
> __traits(compiles, { f(1); } ); // ok
> __traits(compiles, { f(1.2); } ); // ok
> __traits(compiles, { f("1"); } ); // not ok
> __traits(compiles, { f('1'); } ); // not ok
>
> __traits(compiles, { g(1); } ); // ok
> __traits(compiles, { g(1.2); } ); // ok
> __traits(compiles, { g("1"); } ); // not ok
> __traits(compiles, { g('1'); } ); // not ok
>
> I can get that information?

Unfortunately, you cannot introspect template constraints. The 
only thing you can do is check if calling it with given arguments 
compiles or not.




More information about the Digitalmars-d-learn mailing list