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.
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 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.
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.
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.
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.
In addition to other answers, I guess, that the following rules from C++ standard applies:
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: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.
- 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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With