isDelegate

Evaluates to $(K_TRUE) ifthe given type is a delegate (and to $(K_FALSE) otherwise).

This is equivalent to is(T == delegate), so most code shouldn't use it. It's intended to be used in conjunction with templates that take a template predicate - such as those in phobos.sys.meta.

Note that this does not include implicit conversions or enum types. The type itself must be a delegate.

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.

enum isDelegate (
T
)

Examples

1 int var;
2 static assert(!isDelegate!(SymbolType!var));
3 static assert(!isDelegate!(PropertyType!var));
4 static assert(!isDelegate!(typeof(&var)));
5 static assert(!is(SymbolType!var == delegate));
6 static assert(!is(PropertType!var == delegate));
7 static assert(!is(typeof(&var) == delegate));
8 
9 static void func() {}
10 static assert(!isDelegate!(SymbolType!func));
11 static assert(!isDelegate!(typeof(&func)));
12 static assert(!is(SymbolType!func == delegate));
13 static assert(!is(typeof(&func) == delegate));
14 
15 void funcWithContext() { ++var; }
16 static assert(!isDelegate!(SymbolType!funcWithContext));
17 static assert( isDelegate!(typeof(&funcWithContext)));
18 static assert(!is(SymbolType!funcWithContext == delegate));
19 static assert( is(typeof(&funcWithContext) == delegate));
20 
21 int function() funcPtr;
22 static assert(!isDelegate!(SymbolType!funcPtr));
23 static assert(!isDelegate!(PropertyType!funcPtr));
24 static assert(!isDelegate!(typeof(&funcPtr)));
25 static assert(!is(SymbolType!funcPtr == delegate));
26 static assert(!is(PropertyType!funcPtr == delegate));
27 static assert(!is(typeof(&funcPtr) == delegate));
28 
29 int delegate() del;
30 static assert( isDelegate!(SymbolType!del));
31 static assert( isDelegate!(PropertyType!del));
32 static assert(!isDelegate!(typeof(&del)));
33 static assert( is(SymbolType!del == delegate));
34 static assert( is(PropertyType!del == delegate));
35 static assert(!is(typeof(&del) == delegate));
36 
37 @property static int prop() { return 0; }
38 static assert(!isDelegate!(SymbolType!prop));
39 static assert(!isDelegate!(PropertyType!prop));
40 static assert(!isDelegate!(typeof(&prop)));
41 static assert(!is(SymbolType!prop == delegate));
42 static assert(!is(PropertyType!prop == delegate));
43 static assert(!is(typeof(&prop) == delegate));
44 
45 @property int propWithContext() { return var; }
46 static assert(!isDelegate!(SymbolType!propWithContext));
47 static assert(!isDelegate!(PropertyType!propWithContext));
48 static assert( isDelegate!(typeof(&propWithContext)));
49 static assert(!is(SymbolType!propWithContext == delegate));
50 static assert(!is(PropertyType!propWithContext == delegate));
51 static assert( is(typeof(&propWithContext) == delegate));
52 
53 static int function() propFuncPtr() @property { return null; }
54 static assert(!isDelegate!(SymbolType!propFuncPtr));
55 static assert(!isDelegate!(PropertyType!propFuncPtr));
56 static assert(!isDelegate!(typeof(&propFuncPtr)));
57 static assert(!is(SymbolType!propFuncPtr == delegate));
58 static assert(!is(PropertyType!propFuncPtr == delegate));
59 static assert(!is(typeof(&propFuncPtr) == delegate));
60 
61 static int delegate() propDel() @property { return null; }
62 static assert(!isDelegate!(SymbolType!propDel));
63 static assert( isDelegate!(PropertyType!propDel));
64 static assert(!isDelegate!(typeof(&propDel)));
65 static assert(!is(SymbolType!propDel == delegate));
66 static assert( is(PropertyType!propDel == delegate));
67 static assert(!is(typeof(&propDel) == delegate));
68 
69 static struct S
70 {
71     void foo() {}
72 }
73 static assert(!isDelegate!(SymbolType!(S.foo)));
74 static assert(!isDelegate!(typeof(&S.foo)));
75 static assert( isDelegate!(typeof(&S.init.foo)));
76 static assert(!is(SymbolType!(S.foo) == delegate));
77 static assert(!is(typeof(&S.foo) == delegate));
78 static assert( is(typeof(&S.init.foo) == delegate));
79 
80 struct HasContext
81 {
82     void foo() { ++var; }
83 }
84 static assert(!isDelegate!(SymbolType!(S.foo)));
85 static assert(!isDelegate!(typeof(&S.foo)));
86 static assert( isDelegate!(typeof(&S.init.foo)));
87 static assert(!is(SymbolType!(S.foo) == delegate));
88 static assert(!is(typeof(&S.foo) == delegate));
89 static assert( is(typeof(&S.init.foo) == delegate));

See Also

Meta