How is the C++ compiler's performance affected by defining several functions with the same name but different types?

ShanZhengYang

I ran across this example in Stroustrup's "A Tour of C++" (2014). If you define functions with the exact same name but different argument types, the compiler will select the most appropriate function for each call:

void print(int);      // takes an integer argument
void print(double);   // takes a floating-point argument 
void print(string);   // takes a string argument

void user() 
{
    print(42);                  // calls print(int) 
    print(9.65);                // calls print(double) 
    print("D is for digital");  // calls print(string)
}

(1) Doesn't naming functions with the same name like this result in a (perhaps minor) performance hit?

(2) How exactly does the compiler "select" the most appropriate function, given the input? What is going on behind the scenes here?

Jerry Coffin

Function overloading (at least normally) has no effect on execution speed. Consider if you wrote these functions as:

void print_int(int);         // takes an integer argument
void print_double(double);   // takes a floating-point argument 
void print_string(string);   // takes a string argument

...and then printed things out by choosing one of them based on what you wanted to print. That's pretty much what the compiler does: it takes the number and type of parameters, and encodes them into a "mangled" name. When you make a function call, the compiler (at compile time) looks through the available functions, chooses one, and creates a call to that function. The code to call the function is identical to code for calling a function that hasn't been overloaded.

Selecting the best function is a non-trivial exercise, to put it mildly. It takes place in a few phases. The first is to find a scope at which there's something defined with the name you've used. The second is to look through everything at that scope with that name to create a list of overloads.

The next step is to eliminate overloads that couldn't possibly work at all--e.g., overloads that simply can't accept the number of arguments you've passed. If that leaves only one function, overload resolution is done. If there's more than one, we get to the final (and trickiest) part of overload resolution.

The compiler starts with a list of possible implicit conversions for each type, and a ranking for each. The "best" is identity conversion (i.e., no conversion at all, such as if you're passing an int and the function expects an int). Slightly worse (but still fairly good) is something like adding a const. Eventually, you get to things like truncating a double to an int.

The compiler then goes through arguments one at a time, and looks at the conversion necessary to get from that argument to the type for the formal parameter. To qualify as the "best" overload and be chosen for use, at least one argument must have a "better" conversion than the conversion for any other overload, and none of the arguments can have a conversion that's worse than the overload that would be needed for any other overload.

If there is no such thing (e.g., you have only two viable functions, and each has one parameter that has a better conversion, and one argument with a worse conversion) the call is ambiguous, so the code can't compile.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How is the C++ compiler's performance affected by defining several functions with the same name but different types?

From Dev

C#: Multiple functions with the same name and different signature, but the compiler calls the wrong one. How to fix it?

From Dev

multiple functions with same name but different argument types as template parameter

From Dev

How can different functions have the same name?

From Dev

How to declare several stylable attributes with the same name for different tags?

From Dev

XSD different types same name

From Dev

How to deal with functions with same name in C

From Dev

How to call the same functions to several buttons c#

From Dev

Functions with same name, different origin

From Dev

How to handle different data types with same attribute name with Gson?

From Dev

Java Templates, how to use two classes with the same name and different types

From Dev

C++ template functions with the same name but different return type

From Dev

Implement 2 functions with the same type and name but different parameters in C

From Dev

Defining different function with same name but different argument type in OpenCL 1.1

From Dev

How to combine functions to get several types of information

From Dev

Interface with members with same name and different return types

From Dev

Gson - Same field name, different types

From Dev

XSD multiple elements with same name but different types

From Dev

Deserializing attributes of same name but different types in Jackson?

From Dev

Deserializing attributes of same name but different types in Jackson?

From Dev

XSD multiple elements with same name but different types

From Java

Java compiler: How can two methods with the same name and different signatures match a method call?

From Dev

Java compiler: How can two methods with the same name and different signatures match a method call?

From Dev

Using the same name for variables which are in different functions?

From Dev

Are same name functions in different classes supported in Nodejs?

From Dev

How to choose from functions with the same name in C++

From Dev

How to not repeat myself in this situation? C functions that are the same but have different arguments

From Dev

Haskell does not support the C++ overloading style in which functions with different types share a common name

From Dev

Haskell does not support the C++ overloading style in which functions with different types share a common name

Related Related

  1. 1

    How is the C++ compiler's performance affected by defining several functions with the same name but different types?

  2. 2

    C#: Multiple functions with the same name and different signature, but the compiler calls the wrong one. How to fix it?

  3. 3

    multiple functions with same name but different argument types as template parameter

  4. 4

    How can different functions have the same name?

  5. 5

    How to declare several stylable attributes with the same name for different tags?

  6. 6

    XSD different types same name

  7. 7

    How to deal with functions with same name in C

  8. 8

    How to call the same functions to several buttons c#

  9. 9

    Functions with same name, different origin

  10. 10

    How to handle different data types with same attribute name with Gson?

  11. 11

    Java Templates, how to use two classes with the same name and different types

  12. 12

    C++ template functions with the same name but different return type

  13. 13

    Implement 2 functions with the same type and name but different parameters in C

  14. 14

    Defining different function with same name but different argument type in OpenCL 1.1

  15. 15

    How to combine functions to get several types of information

  16. 16

    Interface with members with same name and different return types

  17. 17

    Gson - Same field name, different types

  18. 18

    XSD multiple elements with same name but different types

  19. 19

    Deserializing attributes of same name but different types in Jackson?

  20. 20

    Deserializing attributes of same name but different types in Jackson?

  21. 21

    XSD multiple elements with same name but different types

  22. 22

    Java compiler: How can two methods with the same name and different signatures match a method call?

  23. 23

    Java compiler: How can two methods with the same name and different signatures match a method call?

  24. 24

    Using the same name for variables which are in different functions?

  25. 25

    Are same name functions in different classes supported in Nodejs?

  26. 26

    How to choose from functions with the same name in C++

  27. 27

    How to not repeat myself in this situation? C functions that are the same but have different arguments

  28. 28

    Haskell does not support the C++ overloading style in which functions with different types share a common name

  29. 29

    Haskell does not support the C++ overloading style in which functions with different types share a common name

HotTag

Archive