0

In C++, if I overload a function like the following

void foo(int bar);
void foo(int bar, float baz = 0);

And then I call it

foo(1);

Which foo would be called?

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67

2 Answers2

0

I tried, and it won't compile as David Haim said in his comment to my question.

-2

Why would you even need the first? Seems redundant. The first one will be hit. This is a duplicate. Check here for answer.

Community
  • 1
  • 1
Always Learning
  • 312
  • 1
  • 10
  • Why would I need the first, you mean? – Yassine Imounachen Nov 12 '15 at 17:00
  • Yes, I meant the first - edited for clarity. The second would cover both cases. But still, a dupe question. See my link for more explanation: "If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters." – Always Learning Nov 12 '15 at 19:07
  • 1
    The "answer" you pointed to is about C# and OP asked about C++ which has different rules of overload resolution. – Paul Nov 12 '15 at 19:16
  • It is still considered ambiguous. From [learnCPP.com](http://www.learncpp.com/cpp-tutorial/77-default-parameters/) "However, it is important to note that default parameters do NOT count towards the parameters that make the function unique. Consequently, the following is not allowed: void printValues(int x); void printValues(int x, int y=20); If the caller were to call printValues(10), the compiler would not be able to disambiguate whether the user wanted printValues(int) or printValues(int, 20) with the default value." – Always Learning Nov 12 '15 at 19:26