Checking for Callabilty of either f(x) or x.f

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 1 15:04:37 PDT 2014


On Friday, 1 August 2014 at 17:17:57 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
> 	if (is(typeof(f(x))) || is(typeof(x.f)))

Here's my try:

template isCallableWith(alias fun, T)
{
     enum bool isCallable = is(typeof(fun(T.init))) || 
is(typeof(T.init.fun));
}
unittest {
     auto sqr(T)(T x) { return x*x; }
     auto xf = isCallableWith!(sqr, int);
}

but errs as

traits_ex.d(182,15): Error: cannot infer type from template 
instance isCallableWith!(sqr, int)

Could someone, please, explain how to f and x should be passed in 
a template here?

A complete implementation of, say isCallableWith, would be nice. 
:)


More information about the Digitalmars-d-learn mailing list