Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 393
Arun Avatar asked Jul 26 '13 10:07

Arun


People also ask

Can we overload function with default argument?

No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either. You can only overload functions only on the basis of: Type of arguments. Number of arguments.

Which feature is used in function overloading and function with default parameter?

Which of the following feature is used in function overloading and function with default argument? Explanation: Both of the above types allows a function overloading which is the basic concept of Polymorphism.

What is function with default parameter explain with example?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.

How does function overloading work?

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading.


2 Answers

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.

like image 197
Luchian Grigore Avatar answered Sep 22 '22 21:09

Luchian Grigore


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;
}
like image 26
Spook Avatar answered Sep 21 '22 21:09

Spook