Function Overloading and Overload Resolution
Explore the concept of function overloading in D, where multiple functions share the same name but differ in parameters. Understand how the compiler resolves which function to call by evaluating match quality among overloads. This lesson helps you grasp overload resolution rules and how to manage ambiguities effectively.
We'll cover the following...
Function overloading
Defining more than one function that have the same name is function overloading. In order to be able to differentiate these functions, their parameters must be different.
The following code has multiple overloads of the info() function, each taking a different type of parameter:
Although all of the functions are named info(), the compiler picks the one that matches the argument that is used when making the call. For example, because the literal 1.2 is of type double, the info() function that takes a double gets called.
The choice of which function to call is made at compile-time and may not always be easy or clear. For example, because int can implicitly be converted to both double and real, the compiler cannot decide which of the functions to call in the following program:
Note: It is usually unnecessary to write separate functions when the function bodies are exactly the same. We will see later in the templates chapter how a single definition can be used for multiple types.
However, if there is another function overload that takes a long parameter, the ambiguity would be resolved because long is a better match for int than double or real:
Overload resolution
The compiler picks the overload that is the best match for the arguments. This is called overload resolution.
Although overload resolution is simple and intuitive in most cases, it is sometimes complicated. The following are the overload resolution rules. They are presented in a simplified way in this course.
There are four states of match, listed from the worst to the best:
-
Mismatch
-
Match through automatic type conversion
-
Match through
constqualification -
Exact match
The compiler considers all of the overloads of a function during overload resolution. It first determines the match state of every parameter for every overload. For each overload, the lowest match state among the parameters is taken to be the match state of that overload.
After all of the match states of the overloads are determined, the overload with the best match is selected. If there is more than one overload with the best match, then more complicated resolution rules are applied. We will go into the rules in more detail in this course.
If your program is in a situation where it depends on complicated overload resolution rules then:
- It may be an indication that it is time to change the design of the program.
- Another option is to take advantage of other features of D, like templates.
- An even simpler but not always desirable approach would be to abandon function overloading altogether by naming functions differently for each type e.g.,
sevenTimes_real()andsevenTimes_double().