toDelegate

fnauto toDelegate(F)(auto ref F fp) if (isCallable!(F))

Convert a callable to a delegate with the same parameter list and return type, avoiding heap allocations and use of auxiliary storage.

Parameters

fpa function pointer or an aggregate type with opCall defined.

Returns

A delegate with the context pointer pointing to nothing.

Example: ---- void doStuff() { writeln("Hello, world."); }

void runDelegate(void delegate() myDelegate) { myDelegate(); }

auto delegateToPass = toDelegate(&doStuff); runDelegate(delegateToPass); // Calls doStuff, prints "Hello, world." ----

BUGS:

  • Ignores C-style / D-style variadic arguments.