May 09, 2016 Querying Function Template Restrictions | ||||
---|---|---|---|---|
| ||||
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? |
May 09, 2016 Re: Querying Function Template Restrictions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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.
|
Copyright © 1999-2021 by the D Language Foundation