Table of Contents
ToggleIn this article we will look into concept of function overloading and how can a call to an overloaded function be ambiguous.
What is Function Overloading?
Function Overloading means two or more functions having same name, but differ in the number or order of arguments or data types of arguments. Function overloading is the process of defining same function name to carry out similar types of activities with various data items.
Function Overloading is also known as Compile time polymorphism.
For Example:
// How can a call to an overloaded function be ambiguous int foo() { } int foo(int a) { } float foo(double a) { } int foo(int a, double b) { }
In the above example test functions vary in either number of arguments or type of arguments it takes. This is function overloading.
// How can a call to an overloaded function be ambiguous int foo(int a) { } float foo(int a) { }
Note: The above example is not function overloading as both function only varies on return type.
How Does Function Overloading works?
The complier first search for Exact match i.e. (Function name and Parameter).
If a exact match is not found
Compiler tries type promotion which includes
A char, unsigned char or short can be promoted to an int. For example void foo(int) can be a match for foo('a')
A float can only be promoted to a double.
A bool can be promoted to an int (FALSE counts as 0, TRUE as 1).
If still match is not found
Compiler tries to find a match through the standard conversion which includes
Conversions between integral types, apart from the ones counted as promotions. Remember that bool and char are integral types as well as int, short and long.
Conversions between floating types: double, float and long double, except for float to double which counts as a promotion.
Conversions between floating and integral types
Conversions of integral, floating and pointer types to bool (zero or NULL is FALSE, anything else is TRUE)
Conversion of an integer zero to the NULL pointer
If still no match found error is raised
How can a call to an overloaded function be ambiguous?
So, how can a call to an overloaded function be ambiguous. In Function overloading, sometimes a situation can occur when the compiler is unable to choose between two correctly overloaded functions. This situation is said to be ambiguous.
Ambiguous statements are error-generating statements and the programs containing ambiguity will not compile. Automatic type conversions are the main cause of ambiguity. Let’s understand using examples.
Ambiguity Due To Type Conversion
Example 1:
// Example 1: How can a call to an overloaded function be ambiguous #include <iostream> using namespace std; // Overloaded Function with float type parameter void foo(float a) { cout << "Overloaded Function with float paramter" } // Overloaded Function with double type parameter void foo(double a) { cout << "Overloaded Function with double parameter"; } int main() { // Calling overloaded function with char type paramter foo('c'); return 0; }
In the above example there is no exact type match, the compiler therefore looks for the closest match. The closest match for foo(‘c’) will be foo(int a) which is also not present, foo(double d) and foo(float f) which will cause ambiguity as both are valid conversions. This confusion causes ambiguity in calling overloaded functions and thus causes compile time error.
Example 2:
// Example 2: How can a call to an overloaded function be ambiguous #include <iostream> using namespace std; // Overloaded function with int type parameter void foo(int a) { cout << "int type parameter function"; } // Overloaded function with long type parameter void foo(long a) { cout << "long type parameter function"; } int main() { //Calling function with float type test(10.5f); return 0; }
In the above example function call will look for float function for exact match and since it is not present float is only promoted to double, but there is no function definition with double or float type of parameter. This confusion causes ambiguity in calling overloaded functions and thus causes compile time error.
Ambiguity Due To Function With Default Arguments
// Example 3: How can a call to an overloaded function be ambiguous #include <iostream> using namespace std; // Overloaded Function with one parameter void foo(int a) { cout << a; } // Overloaded Function with two parameter // where one is default parameter void foo(int a, int b = 10) { cout<<a<<b; } int main() { // Call overloaded function with two paramters foo(100, 200); // Call overloaded function with one paramters foo(100); return 0; }
Here, in the unambiguous first call statement foo(100, 200) two arguments are specified, therefore there is no ambiguity and exact match is found by compiler. In the second call however, foo(100) the compiler gets confused about whether to call the first function foo() with one argument or to call the second foo() function with two arguments out of which one is the default. This causes the program to throw an error.
Conclusion
Two or more functions are said to be overloaded if they differ in any of the following-
- The number of arguments.
- Order of arguments.
- Type of arguments.
And so to answer how can a call to an overloaded function be ambiguous, is when there might be two or more functions with equally appropriate signatures and complier is not able to decide which function to call as more than function resolves to appropriate signatures either via exact match or type conversion.
Got a question or just want to chat? Drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!