Can I pass a function by parameter?
AsmMan via Digitalmars-d
digitalmars-d at puremagic.com
Sun Sep 7 14:31:10 PDT 2014
On Sunday, 7 September 2014 at 21:23:26 UTC, Jakob Ovrum wrote:
> On Sunday, 7 September 2014 at 21:02:16 UTC, John Colvin wrote:
>> bool g(int n) { ... }
>> f(arr, &g);
>
> This will fail if `&g` is a function pointer, such as when `g`
> is declared at module-level scope. In that case, it has to be
> explicitly converted to a delegate:
>
> ---
> import std.functional : toDelegate;
>
> int f(in int[] arr, bool delegate(int) func);
> bool g(int n) { ... }
> f(arr, toDelegate(&g));
> ---
>
> Alternatively, make `f` receive a function pointer instead of a
> delegate:
>
> ---
> int f(in int[] arr, bool function(int) func);
> bool g(int n) { ... }
> f(arr, &g);
> ---
Thank you too. Btw, why the & operator in this syntax? I used to
think ref keyword sort of C's T** and & operator is neeeded.. or
is it because f can be a function called without pass any
parameter?
More information about the Digitalmars-d
mailing list