Arun
Arun

Reputation: 2373

How Function Overloading is working with default parameter

Consider the following functions definition

void fn(int a, int b)
{
  //...
  //...
}

void fn(int a, int b, int c = 0)
{
  //...
  //...
}

In the main function I am calling a fn with 2 arguments:

int main()
{
  fn(10, 15);
  return 0;
}

So would like to know how compiler handles this situation.

Upvotes: 5

Views: 1543

Answers (4)

Spook
Spook

Reputation: 25927

In addition to other answers, I guess, that the following rules from C++ standard applies:

13.3 Overload resolution

Each of these contexts defines the set of candidate functions and the list of arguments in its own unique way. But, once the candidate functions and argument lists have been identified, the selection of the best function is the same in all cases:
  • First, a subset of the candidate functions (those that have the proper number of arguments and meet certain other conditions) is selected to form a set of viable functions (13.3.2).
  • Then the best viable function is selected based on the implicit conversion sequences (13.3.3.1) needed to match each argument to the corresponding parameter of each viable function.
3. If a best viable function exists and is unique, overload resolution succeeds and produces it as the result. Otherwise overload resolution fails and the invocation is ill-formed. When overload resolution succeeds, and the best viable function is not accessible (Clause 11) in the context in which it is used, the program is ill-formed.

By the way, there actually is a way to call specific overload:

#include <stdio.h>

int f(int a, int b)
{
    printf("f - version 1\n");
    return 0;
}

int f(int a, int b, int c = 10)
{
    printf("f - version 2\n");
    return 0;
}

int main(int argc, char * argv[])
{
    int (* fn1)(int, int);
    fn1 = f;

    fn1(5, 10);

    return 0;
}

Upvotes: 3

nullptr
nullptr

Reputation: 11058

This is not a valid C++ code, the call is ambiguous. Here is diagnostics from GCC:

In function ‘int main()’:
error: call of overloaded ‘fn(int, int)’ is ambiguous
note: candidates are: void fn(int, int)
note:                 void fn(int, int, int)

Although some compilers can treat the situation in some way (e.g. always use the second definition in case of ambiguity instead of aborting).

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258548

The compiler can't know, and thus throws an error:

prog.cpp: In function ‘int main()’: prog.cpp:15:12: error: call of
  overloaded ‘fn(int, int)’ is ambiguous prog.cpp:15:12: note:
  candidates are: prog.cpp:1:6: note: void fn(int, int) prog.cpp:7:6:
  note: void fn(int, int, int)

The error doesn't happen on declaration, but on call, when it's actually resolved.

Upvotes: 9

TheDarkKnight
TheDarkKnight

Reputation: 27611

While you can declare them, if you call one with fn(10,15), you're likely to get an error stating that it's an ambiguous call as the compiler doesn't know which you want to use. Though this may dependent on the compiler you're using.

Upvotes: 1

Related Questions