www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Querying Function Template Restrictions

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
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
parent Meta <jared771 gmail.com> writes:
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