answersLogoWhite

0


Best Answer
***Function Overloading and Ambiguity ****

We know that compiler will decide which function to be invoked among the overloaded functions. When the compiler could not choose a function between two or more overloaded functions, the situation is called as an ambiguity in function overloading.

When the program contains ambiguity, the compiler will not compile the program.

The main cause of ambiguity in the program

· Type conversion

· Functions with default arguments

· Functions with pass by reference

Type conversion

#include

using namespace std;

#include

void fun(int i)

{ cout << i;}

void fun(float f)

{ cout << f;}

int main()

{

fun(10);

fun(10.2);

return 0;

}

In this example type conversion creates ambiguity. fun(10) will refer the first function.

We think the value 10.2 is floating point value. So fun(10.2) will refer the second function. But fun(10.2) will refer which function? It could not refer any function. Because 10.2 will be treated as double data type by the compiler. (In c++ all the floating point values are converted by the compiler as double data type.) This is type conversion of float to double. So now compiler could not search the function fun with double data type.

So the compiler will give error message call of overloaded 'fun(double)' is ambiguous.

This example clearly rises the ambiguity in the program because of the type conversion.

Functions with default arguments

#include

using namespace std;

#include

void fun(int i)

{ cout << i;}

void fun(int i, int j=8)

{ cout << i << j;}

int main()

{

fun(10);

fun(10.2f);

return 0;

}

This program will give error message that overloaded 'fun(int)' is ambiguous. Because the fun(int i, int j =8) is a function with default arguments. It can be called in two ways. They are

(i) function with one argument.

fun(2) now the value of i is 2 and j is 8 (8 is the default value).

(ii) function with two arguments fun(5,6). So i is 5 and j is 6.

fun(int i) is a function with one argument. It should be invoked with one argument. So

when we call a function with 1 argument the compiler could not select among the two functions fun(int i) and fun(int i, int j=8).

This example clearly illustrates the ambiguity in the program because of function with default arguments.

Function with reference parameter

#include

using namespace std;

#include

void fun(int i)

{ cout << i;}

void fun(int &i)

{cout <

int main()

{

fun(10);

return 0;

}

This program will show the error message call of overloaded 'fun(int)' is ambiguous.

First fun(int) takes one integer argument and second function fun(int &) takes a reference parameter. In this case, compilers do not know which function is intended by the user when it is called. Because there is no syntactical difference in calling the function between the function with single argument and function with single reference parameter.

So compiler could not decide which function should be invoked when it is called. So this leads ambiguity in the program.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

You create ambiguity when two functions share the same signature or the signatures are covariant. For instance: void foo (unsigned int) has the same signature as void foo (size_t), because size_t is implemented as an alias (a typedef) for unsigned int. Such ambiguities can be avoided by placing the functions in separate namespaces. However, you need to be mindful that polluting the global namespace by importing both names will re-introduce the ambiguity. A better solution is to give the functions different names.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Ambiguity in function overloading can only exist at compile time. Ambiguity occurs when two or more functions share the same signature or prototype. That is, the number and type of arguments (including any use of the const keyword) are intrinsically the same. Ambiguity can also occur when calling a function overload with types that do not match any of the existing signatures. You must either provide a suitable implementation or cast the types to suit an appropriate existing signature.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the situations where overloading function can be an ambiguity?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How operator overloading differ from function overloading?

Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed. Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.


What is function overloading in c language of computer?

There is no such thing as function overloading in C; that is a feature of C++. Function overloading allows us to provide two or more implementations of the same function. Typically, we use function overloading so that the same function can cater for different types. For instance, we might provide one implementation that is optimised to handle an integer argument while another is optimised to handle a real argument. We can also use function overloading to provide a common implementation of a function which can then be invoked by overloads that handle the low-level type conversions.


What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.


When to create a template function?

A template function is used when you want to write some kind of function that can be applied to different data types. It is a form of overloading, but you don't have to actually write all of the overloaded variants.


Rules for function overloading?

It's a way by which you use define the same function for different input types. For example, think about the the operator "+" which in java works for adding integers, floating point numbers and even string concatenation. The way such functionality is achieved is by overloading.

Related questions

How operator overloading differ from function overloading?

Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed. Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.


define function overloading?

Defining several functions with the same name with unique list of parameters is called as function overloading.


Is it possible to do operator overloading in c?

No. Operator and/or function overloading is only a C++ thing.


What is function overloading in c language of computer?

There is no such thing as function overloading in C; that is a feature of C++. Function overloading allows us to provide two or more implementations of the same function. Typically, we use function overloading so that the same function can cater for different types. For instance, we might provide one implementation that is optimised to handle an integer argument while another is optimised to handle a real argument. We can also use function overloading to provide a common implementation of a function which can then be invoked by overloads that handle the low-level type conversions.


How the compiler compiles overloaded method?

method overloading occurs when we use two functions or more with the same name.In function overloading compiler detect which method is call actually throw the parameters passed to the methods.In function overloading parameters of functions are different.


What is an operator overloading in C?

one function but multiple behaviours depending on the parameters


What is the c plus plus program to calculate the area of a circle using function overloading?

Function overloading is used when you want to re-use the same function name with different argument types or a different number of arguments. Calculating the area of a circle isn't the sort of function that requires overloading since the only argument you need is the radius. double area_of_circle (const double radius) { const double pi=4*atan(1); return pi*radius*radius; }


What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.


Why do you think Welty does not clarify this ambiguity?

Welty may have chosen not to clarify the ambiguity in order to allow readers to form their own interpretations and engage more actively with the story. By leaving the ambiguity unresolved, she may have intended to create a sense of mystery and provoke deeper thought and discussion among readers. This ambiguity could also serve to reflect the complexities and uncertainties of real-life situations.


When to create a template function?

A template function is used when you want to write some kind of function that can be applied to different data types. It is a form of overloading, but you don't have to actually write all of the overloaded variants.


Rules for function overloading?

It's a way by which you use define the same function for different input types. For example, think about the the operator "+" which in java works for adding integers, floating point numbers and even string concatenation. The way such functionality is achieved is by overloading.


Why do you use of operator overloading in c?

Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...