What to do about default function arguments

Jonathan M Davis jmdavisProg at gmx.com
Thu Apr 26 10:25:31 PDT 2012


On Thursday, April 26, 2012 13:54:47 bearophile wrote:
> The simplest solution is to the breaking change of disallowing
> default arguments for function pointers and delegates. This also
> means disallowing talking the pointer/delegate of function with
> default arguments.

No it doesn't. You could definitely have a pointer to function with default 
arguments. It's just that the pointer doesn't use the default arguments at 
all. The default arguments get inserted at the call site if you explicitly 
call a function and don't pass arguments for those parameters. The function's 
signature is unaffected by the presence of default arguments, so the pointer 
should be unaffected.

What should be disallowed is giving default arguments to function pointers and 
delegate literals. So, stuff like

void main()
{
 int foo(int a = 1)
 {
 return a;
 }
}

should work just fine. The default argument will only get used if foo is called 
directly. However, stuff like

void main()
{
 auto foo = (int a = 1) { return a;};
}

wouldn't work, because you can't call the function except through its pointer. 
As you can never call the function directly, its default argument would never 
be used, and there would be no point to having it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list