Function Overload Rules

Take a look at this code (for example):

#include <iostream> void print(unsigned int value); void print(float value); int main() { print('a'); print(0); print(3.14159); return 0; } 

I get the following error:

'print (char)' is ambiguous

What is the real problem? I realized that more than one function is suitable for any call from the main (for the print function). But what actually are the rules in function overloading (How can I find out what corresponds to the one between which types exist, etc.)

How can I find out that more than one function corresponds to a call? (another example: it's unclear how to understand that both functions correspond to a call)

 #include <iostream> void print(unsigned int value); void print(int value); int main() { print(2.5); } 
+5
source share
2 answers

Errors you get due to a missing print version:

 print('a'); // calling print(int) print(0); // calling print(int) print(3.14159); // calling print(double) not float 

As you can see, print(int) and print(double) missing, and print(char) missing, you get these errors. When print(char) , for example, is not provided, looking for a compiler for print(int) in this way, it passes the passed value to an integer.

To solve this problem, you either add a version of the overloaded print , or explicitly pass the value passed to:

1- Print Overload:

 void print(int value){ cout << "int: " << value << endl;} void print(unsigned int value){ cout << "ui: " << value << endl;} void print(float value){cout << "float: " << value << endl;} void print(char c){ cout << "Char: " << c << endl;} void print(double value){cout << "double: " << value << endl;} 

2- Casting:

 print((unsigned int)'a'); // or (float) print((unsigned int)0); // or (float) print(3.14159f); // or (float)3.14159 or (unsigned int)3.14159 
  • Keep in mind that print(3.14159); - call print(double); no float to do this for float: print(3.14159f);
+1
source

When you work with function overloading, there is a lot of work that the compiler does behind the scenes to perform function overloading. Details of what needs to be listed here, however I can provide some links that may be useful, and try to describe some of the most important parts.


There are 3 possible outcomes:

  • Found a match. A call is allowed for a specific congestion.
  • No matches found. Arguments cannot be matched with any overload.
  • An ambiguous match was found. Arguments correspond to more than one overload.

The main order of execution by the compiler is as follows:

  • Find exact match from parameter list
  • Trying to find a match through promotion
  • trying to find a match through standard conversion
  • Trying to find a match through custom conversion

So, how is it determined whether a call is ambiguous?

Since each overload must have unique parameters, and because all standard transformations and all user transformations are considered equal; if a function call matches more than one valid candidate for a definition definition through a standard or custom transformation, then the result will be mixed.


From your example:

 void print( unsigned int value ); void print( float value ); print( 'a' ); print( 0 ); print( 3.14159 ); 

In the case of print( 'a' ); C ++ cannot find an exact match. First try pushing 'a' to int , but there is no print(int) function that is declared-defined. Then it will try to use the standard conversion, it can convert 'a' to unsigned int and floating point value. And since all standard conversions are considered equal, this leads to ambiguity.


To eliminate this ambiguity, you can either simply declare-define use the necessary print( type ); versions print( type ); , or explicitly specify the type on one of the provided types of overload parameters.

+3
source

Source: https://habr.com/ru/post/1275847/


All Articles