Check for presence of function

Adam D. Ruppe destructionator at gmail.com
Sun Mar 23 06:03:06 PDT 2014


On Sunday, 23 March 2014 at 12:53:39 UTC, Dicebot wrote:
> template isSomething(T)
> {
>     enum isSomething =
> 		is(typeof(__traits(getMember, T, "doSomething")) == function)
> 	 && is(typeof(&__traits(getMember, T, "doSomething")) : void 
> function(Unrelated*));
> }

That won't necessarily work in the presence of overloaded 
functions since getMember only gets one of them.

You could though do this:

template isSomething(T) {
     bool checker() {
         foreach(overload; __traits(getOverloads, T, 
"doSomething"))
                 static if(is(typeof(overload) == void 
function(Unrelated*)))
                        return true;
         return false;
     }
     enum isSomething = checker();
}

getOverloads returns an empty tuple for non-functions so you 
don't even have to check that ahead of time.


More information about the Digitalmars-d-learn mailing list