digitalmars.D.learn - Querying Function Template Restrictions
- =?UTF-8?B?Tm9yZGzDtnc=?= (21/21) May 09 2016 In what ways can I compile-time introspect the template
- Meta (4/25) May 09 2016 Unfortunately, you cannot introspect template constraints. The
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
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.
May 09 2016