1 int var; 2 static assert(!isFunctionPointer!(SymbolType!var)); 3 static assert(!isFunctionPointer!(PropertyType!var)); 4 static assert(!isFunctionPointer!(typeof(&var))); 5 6 static void func() {} 7 static assert(!isFunctionPointer!(SymbolType!func)); 8 static assert( isFunctionPointer!(typeof(&func))); 9 10 void funcWithContext() { ++var; } 11 static assert(!isFunctionPointer!(SymbolType!funcWithContext)); 12 static assert(!isFunctionPointer!(typeof(&funcWithContext))); 13 14 int function() funcPtr; 15 static assert( isFunctionPointer!(SymbolType!funcPtr)); 16 static assert( isFunctionPointer!(PropertyType!funcPtr)); 17 static assert(!isFunctionPointer!(typeof(&funcPtr))); 18 19 int delegate() del; 20 static assert(!isFunctionPointer!(SymbolType!del)); 21 static assert(!isFunctionPointer!(PropertyType!del)); 22 static assert(!isFunctionPointer!(typeof(&del))); 23 24 // It's possible to get the pointers to the function and the context from a 25 // delegate in @system code. 26 static assert( isFunctionPointer!(typeof(SymbolType!del.funcptr))); 27 static assert( is(typeof(SymbolType!del.ptr) == void*)); 28 29 @property static int prop() { return 0; } 30 static assert(!isFunctionPointer!(SymbolType!prop)); 31 static assert(!isFunctionPointer!(PropertyType!prop)); 32 static assert( isFunctionPointer!(typeof(&prop))); 33 34 @property int propWithContext() { return var; } 35 static assert(!isFunctionPointer!(SymbolType!propWithContext)); 36 static assert(!isFunctionPointer!(PropertyType!propWithContext)); 37 static assert(!isFunctionPointer!(typeof(&propWithContext))); 38 39 static int function() propFuncPtr() @property { return null; } 40 static assert(!isFunctionPointer!(SymbolType!propFuncPtr)); 41 static assert( isFunctionPointer!(PropertyType!propFuncPtr)); 42 static assert( isFunctionPointer!(typeof(&propFuncPtr))); 43 44 static int delegate() propDel() @property { return null; } 45 static assert(!isFunctionPointer!(SymbolType!propDel)); 46 static assert(!isFunctionPointer!(PropertyType!propDel)); 47 static assert( isFunctionPointer!(typeof(&propDel))); 48 49 static struct S 50 { 51 void foo() {} 52 } 53 static assert(!isFunctionPointer!(SymbolType!(S.foo))); 54 static assert( isFunctionPointer!(typeof(&S.foo))); 55 static assert(!isFunctionPointer!(typeof(&S.init.foo))); 56 57 struct HasContext 58 { 59 void foo() { ++var; } 60 } 61 static assert(!isFunctionPointer!(SymbolType!(S.foo))); 62 static assert( isFunctionPointer!(typeof(&S.foo))); 63 static assert(!isFunctionPointer!(typeof(&S.init.foo)));
Evaluates to $(K_TRUE) if the given type is a function pointer (and to $(K_FALSE) otherwise).
Note that this does not include implicit conversions or enum types. The type itself must be a function pointer.
Whether taking the address of a function results in a function pointer or a delegate depends on whether the result includes a context pointer.
Taking the address of a free function or a $(K_STATIC) function gives a function pointer. Taking the address of a non-$(K_STATIC) nested function gives a delegate. And the potentially confusing one is non-$(K_STATIC) member functions, because whether taking their address results in a function pointer or a delegate depends on whether the address is taken via the type or via an instance.
See the documentation for SymbolType and PropertyType to see the quirks involving $(K_PROPERTY) and why either SymbolType or PropertyType should be used rather than $(K_TYPEOF) to get the type of a symbol.