1
void f();
void f(int);
void f(int, int);
void f(double, double = 3.14);
f(5.6);  // calls void f(double, double) and not f(int) or f(), for that matter. Why?

I read that the compiler checks the number of parameters before checking the type of parameters. Then why aren't all the functions having different number of parameters getting eliminated?

Ishit Mehta
  • 89
  • 1
  • 2
  • 6
  • Parameters for which no argument has been provided (at the call site) and which have default arguments are *ignored* for overload resolution [over.match.viable]/2. – dyp Nov 13 '13 at 22:23

2 Answers2

2

It does call void f(double, double = 3.14);, because of the default value for the second argument; one double provided, one required -> match. Otherwise, void f(int); would be selected. So its the number of mandatory parameters that matters.

Further info:

Community
  • 1
  • 1
Sam
  • 7,778
  • 1
  • 23
  • 49
  • ADL? You mean overload resolution? This has nothing to do with name lookup. – dyp Nov 13 '13 at 22:22
  • @DyP: Thank you. As far as I know, it depends on the actual context, whether ADL is used additionally to a name lookup. It may be applied to gather other candidates for overload resolution. Mentioning ADL in conjunction with the OP's code snippet was misleading, so I removed that part. – Sam Nov 13 '13 at 22:54
  • "whether ADL is used additionally to a name lookup" ADL is a *form* of name lookup; it's always used when the function name is unqualified (qualified: `my_class_or_namespace::f`; unqualified: `f`). Btw you included the link to cppreference twice ;) – dyp Nov 14 '13 at 15:45
1

You have defined the second value from the function:

void f(double, double = 3.14);

So the call

f(5.6);

is like

f(5.6, 3.14);

use explicit type converting, to call the other function, like:

f((int)5.6);
pointhi
  • 303
  • 1
  • 5
  • 13