Function Templates and Operator Overloading

std::cout << “Hello Readers!” << std::endl;
In this blog post I’m going to show you a short example of a function template by using a struct and operator overloading.

Function templates are blueprints of your function, it plugging-in the type that’s needed. Compiler generates the functions in compile time. The trick is if you use an operator, your type must support it! Otherwise, you have to overload that operator.

Right now let’s create a struct and use > and << operators for that struct. We’re a person individually, so let’s have a person struct with name and age attributes.

struct Person {
  std::string name;
  int age;
};

and create a template function named max 🙂

template <typename T>
T max(T a, T b){
  return (a > b) ? a : b;
};

max function will work with almost any type, almost because of some types, structs or classes doesn’t support operator >. If you try to get max of Person structs, your compiler will return you an error. What you got to do is to overload operator of >

struct Person {
  std::string name;
  int age;
  bool operator>(const Person &rhs) const{
    return this->age > rhs.age;
  }
};

It’s const, because we don’t want to modify them! In your main function:

  Person Muhammet{"Muhammet",28};
  Person Adrian{"Adrian",35};
  Person OldDude = max(Muhammet,Adrian);
  std::cout << OldDude.name << "is older" << std::endl;

Let’s create a display function with 2 different types:

template <typename T1, typename T2>
void display(T1 a, T2 b){
  std::cout << a << " " << b << std::endl;
};

And let’s try to display Person and an integer value in your main:

std::cout << Muhammet << std::endl;
display(Muhammet,20);

You’ll get an error, no suprise 🙂 Your compiler is about to return something like this:

error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘Person’)
std::cout << a << ” ” << b << std::endl;

,Compiler expects stream and Person for insertion operator. In this case a is a Person and there’s no overloaded insertion operator for Person. What we have got to do, overload an insertion operator for Person of course 🙂 We don’t need a friend public, because everything is public in here.

std::ostream &operator<<(std::ostream &os, const Person &p){
    os << p.name;
    return os;
}

Finally, here is full length of example code. Hope it’ll work for you:

#include <iostream>

struct Person {
  std::string name;
  int age;
  bool operator>(const Person &rhs) const{
    return this->age > rhs.age;
  }
};

template <typename T>
T max(T a, T b){
  return (a > b) ? a : b;
};

template <typename T1, typename T2>
void display(T1 a, T2 b){
  std::cout << a << " " << b << std::endl;
};

std::ostream &operator<<(std::ostream &os, const Person &p){
    os << p.name;
    return os;
}

int main()
{
    Person Muhammet{"Muhammet",28};
    Person Adrian{"Adrian",30};
    Person oldDude = max(Muhammet,Adrian);

    std::cout << oldDude.name <<" is older!" << std::endl;

    display(Muhammet,20);
    return 0;
}

Edit: I noticed that my Syntax Highlighter plugin has issues with operators displaying in post so I’ve uploaded my main.cpp, if you want to download!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *