#include<iostream>usingnamespace std;// One function works for all data types. This would work// even for user defined types if operator '>' is overloadedtemplate<typenameT> T myMax(T x, T y){return(x > y)? x : y;}intmain(){
cout <<myMax<int>(3,7)<< endl;// Call myMax for int
cout <<myMax<double>(3.0,7.0)<< endl;// call myMax for double
cout <<myMax<char>('g','e')<< endl;// call myMax for charreturn0;}
#include<iostream>usingnamespace std;// One function works for all data types. This would work// even for user defined types if operator '>' is overloadedtemplate<typenameT>
T myMax(T x, T y){return(x > y)? x: y;}intmain(){
cout <<myMax<int>(3,7)<< endl;// Call myMax for int
cout <<myMax<double>(3.0,7.0)<< endl;// call myMax for double
cout <<myMax<char>('g','e')<< endl;// call myMax for charreturn0;}
A template is a simple and yet very powerful tool in C++. The simple idea is to
pass data type as a parameter so that we don’t need to write the same code for
different data types.
For example, a software company may need sort()for
different data types. Rather than writing and maintaining the multiple codes,
we can write one sort()and pass data type as a parameter.
C++ adds two new keywords to support templates: ‘template’ and ‘typename’. The
second keyword can always be replaced by keyword ‘class’.
/*
C++ template functions are an alternamive way to write a function that
can take different data types. C++ Template functions are only one
function, so if you need to make a change, then it only has to be done
once. Here is an example of a 'get_doubled' templated function:
*/#include<iostream>using std::cout;template<typenameT>// Now, T is a type of variable, for this scope:voidsay_something(T input){
cout << input << "
";}intmain(void){say_something(45);// Uses a intsay_something("Hello");// Uses a stringsay_something(90.5);// Uses a float/doublereturn0;}
template<classidentifier> function_declaration;template<typenameidentifier> function_declaration;//Example:template<classType>voidSwap( Type &x, Type &y){
Type Temp = x;
x = y;
y = Temp;}
// templates are used if the logic of function is same but // it differs due to data type./*Here is an example to show how user defined function print
can be made to print data of different data types.*/template<classT>
T print(T a){
cout << a;}intmain(){print('a');print(1);}
namespace std {template<typenamet>structhash<MyClass<t>>{
size_t operator()(const MyClass<t>& c)const;}}// You can also do things liketemplate<template<typenamet>classtype>func_name<type<t>>();
// function template in c++#include<iostream>usingnamespace std;floatfunAvg(int a,int b){float avg =(a+b)/2.0;return avg;}floatfunAvg2(int a,float b){float avg2 =(a+b)/2.0;return avg2;}//function template in c++template<classT1,classT2>floatfun(T1 a, T2 b){float avg2 =(a+b)/2.0;return avg2;}intmain(){float a;
a =funAvg(22,7);printf("The average of these number is %.3f ",a);
cout<<endl;float a1;
a1 =funAvg2(11,8.6);printf("The average of these number is %.3f ",a1);
cout<<endl;// float T;// T = fun(11 , 8.6f);// printf("The average of these number is %.3f ",T);// cout<<endl;// ---------------------function template in c++-----------------float T;
T =fun(11,98);printf("The average of these number is %.3f ",T);return0;}
template<classT>//or <typename T> it´s the same//T can be int, float, double, etc. //simple use example:
T sum(T a, T b){return a + b;}sum(5.0f,10f);//sum using floatsum(2,3);//sum using int