C++ Function Templates

October 27, 2015 Posted by WithU Technologies
Till now we already work with many functions and in every program there is at least one default function i.e. the main function.

As you know that we have declare a return type and parameters to declare a function.
Example:

#include <iostream>

using namespace std;

int show(int a){

      return a;

}

int main() {

      int x = 5;

      cout<<show(x);

      return 0;

}

// Output 5

·         If we go through the example given above, we will find out that till now we have to limit C++ functions to certain data types which we have to declare earlier before compilation.

·         And if we need the same program written above in any other return type like float then we need to write the same code again by changing it’s return type to float as shown below:

float show(float a){

      return a;

}

……………………………….
·         But with the help of Function Templates we can write one version of function to work with parameters and return of any type.

·         As Function Templates does not specify any particular data type.

·         Function Templates use Placeholders to define a functions and its parameters called template type parameters.

Syntax:

      template <class template variable name>

                or

    template <typename template variable name>

Before declaring the function, template keyword is used followed by class or typename keyword along with the template variable name within angle brackets to declare the function template.


#include <iostream>

using namespace std;

template<class R>    //alternative template<typename R>

 R show(R a, R b){

      return a*b;

}

int main() {

      cout<<show(12.5,5.4);

      return 0;

}

// Output 67.5

·         Compiler automatically calls the function for its corresponding type.

·         Function templates help to reduce code maintenance, because duplicate code is reduced significantly.  

·         They work with every data types and thus we need to write redundant codes once at a time only.

Function Templates with Multiple Parameters:

Function templates can also be applied for multiple parameters of varying types.
#include <iostream>

using namespace std;

template<class R, class S>    //alternative template<typename R>

 R DisplayGreater(R a, S b){

      return (a>b?a:b);     //Ternary operator is declared

}

int main() {

      int x = 18.23;

      float y = 16.3;

      cout<<DisplayGreater(x,y);

      return 0;

}

// Output 18
·         We declare two different generic data types R and S.

Ternary operator
·         Ternary operators work like If statements. It checks the a>b condition and then provide the corresponding result.

·         The ternary operator syntax provided here is (a>b?a:b) which is equivalent to the expression if a is greater than b, return a, else, return b.

R DisplayGreater(R a, S b)


·         As we can see here that the return type of the function is R and according to the main function R takes X value which is an integer, thus R is also an integer.

·         So, the result we generate is also an integer and thus it only outputs 18.