Can I pass a function by parameter?

Jakob Ovrum via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 7 14:42:30 PDT 2014


On Sunday, 7 September 2014 at 21:31:11 UTC, AsmMan wrote:
> 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?

In D, the address-of operator has to be used to get a function 
pointer or delegate from a function or member function. This is 
unlike C and C++, where the function is implicitly convertible to 
its function-pointer type.

This difference in rules may be because D has functions that can 
be called without parentheses:

---
int foo() { return 42; }

// Note: `bar` is an overload set.
void bar(void function() a) {}
void bar(int a) {}

void main()
{
     assert(foo() == 42);

     // Nullary functions can also be called without parentheses.
     assert(foo == 42);

     bar(foo); // If function pointers worked like in C, which 
overload should be called?
}
---


More information about the Digitalmars-d mailing list