View mode: basic / threaded / horizontal-split · Log in · Help
September 15, 2011
detecting function pointer types
I tried writing a template function that detects function pointer types but it
does not work for function marked wiht any extern qualifier.
I tried around but couldn't find a way to make it work.
Any suggestions on this?

template isFunctionPointer(T){
	pragma(msg,T);
	static if(is(T VAR == RET function(ARGS),RET,ARGS...)){
		enum bool isFunctionPointer = true;
	}
	else {
		enum bool isFunctionPointer = false;
	}
}

alias void function() func1;
extern(C) alias void function() func2;
extern(Windows) alias void function() func3;
extern(C++) alias void function() func4;

pragma(msg,isFunctionPointer!(func1)); //true
pragma(msg,isFunctionPointer!(func2)); //false
pragma(msg,isFunctionPointer!(func3)); //false
pragma(msg,isFunctionPointer!(func4)); //false

void main(string[] args){
}

Kind Regards
Benjamin Thaut
September 15, 2011
Re: detecting function pointer types
On 9/15/11 7:59 PM, Benjamin Thaut wrote:
> I tried writing a template function that detects function pointer types but it
> does not work for function marked wiht any extern qualifier.
> I tried around but couldn't find a way to make it work.

---
template isFunctionPointer(T) {
  enum isFunctionPointer = false;
}

template isFunctionPointer(T : U*, U) if (is(U == function)) {
  enum isFunctionPointer = true;
}
---

Could also be implemented using static ifs, if you prefer that.

David
Top | Discussion index | About this forum | D home