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?

1
  • 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. Commented Nov 13, 2013 at 22:23

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

3 Comments

ADL? You mean overload resolution? This has nothing to do with name lookup.
@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.
"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 ;)
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.