Search

13.17 — Overloading operators and function templates

In lesson 8.14 -- Function template instantiation, we discussed how the compiler will use function templates to instantiate functions, which are then compiled. We also noted that these functions may not compile, if the code in the function template tries to perform some operation that the actual type doesn’t support (such as adding integer value 1 to a std::string).

In this lesson, we’ll take a look at a few examples where our instantiated functions won’t compile because our actual class types don’t support those operators, and show how we can define those operators so that the instantiated functions will then compile.

Operators, function calls, and function templates

First, let’s create a simple class:

and define a max function template:

Now, let’s see what happens when we try to call max() with object of type Cents:

C++ will create a template instance for max() that looks like this:

And then it will try to compile this function. See the problem here? C++ has no idea how to evaluate x > y when x and y are of type Cents! Consequently, this will produce a compile error.

To get around this problem, simply overload operator> for any class we wish to use max with:

This works as expected.

Another example

Let’s do one more example of a function template not working because of missing overloaded operators.

The following function template will calculate the average of a number of objects in an array:

This produces the values:

3
5.535

As you can see, it works great for built-in types!

Now let’s see what happens when we call this function on our Cents class:

The compiler goes berserk and produces a ton of error messages! The first error message will be something like this:

c:test.cpp(45) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Cents' (or there is no acceptable conversion)

Remember that average() returns a Cents object, and we are trying to stream that object to std::cout using operator<<. However, we haven’t defined the operator<< for our Cents class yet. Let’s do that:

If we compile again, we will get another error:

c:test.cpp(14) : error C2676: binary '+=' : 'Cents' does not define this operator or a conversion to a type acceptable to the predefined operator

This error is actually being caused by the function template instance created when we call average(Cents*, int). Remember that when we call a templated function, the compiler “stencils” out a copy of the function where the template type parameters (the placeholder types) have been replaced with the actual types in the function call. Here is the function template instance for average() when T is a Cents object:

The reason we are getting an error message is because of the following line:

In this case, sum is a Cents object, but we have not defined operator+= for Cents objects! We will need to define this function in order for average() to be able to work with Cents. Looking forward, we can see that average() also uses the operator/=, so we will go ahead and define that as well:

Finally, our code will compile and run! Here is the result:

11 cents

Note that we didn’t have to modify average() at all to make it work with objects of type Cents. We simply had to define the operators used to implement average() for the Cents class, and the compiler took care of the rest!


13.x -- Chapter 13 comprehensive quiz
Index
13.16 -- Shallow vs. deep copying

149 comments to 13.17 — Overloading operators and function templates

  • Colin

    There seems a typo in the start of tutorial, where T and U should have been x and y:

  • Jinning Wu

    In the Cents example, should we let the operator+= to return a reference to the object? Although it doesn't matter in this example, it is to follow the convention of operator+=.

  • Cemre

    Is there a reason you're using parentheses instead of braces?

  • bhavna

    i tried this but it is showing error:call of overloaded "max(cents&,cents&) is ambiguous,please resove this

Leave a Comment

Put all code inside code tags: [code]your code here[/code]