1template <class T>
2void swap(T & lhs, T & rhs)
3{
4 T tmp = lhs;
5 lhs = rhs;
6 rhs = tmp;
7}
1#include <iostream>
2using namespace std;
3
4template <typename T>
5void Swap(T &n1, T &n2)
6{
7 T temp;
8 temp = n1;
9 n1 = n2;
10 n2 = temp;
11}
12
13int main()
14{
15 int i1 = 1, i2 = 2;
16 float f1 = 1.1, f2 = 2.2;
17 char c1 = 'a', c2 = 'b';
18
19 cout << "Before passing data to function template.\n";
20 cout << "i1 = " << i1 << "\ni2 = " << i2;
21 cout << "\nf1 = " << f1 << "\nf2 = " << f2;
22 cout << "\nc1 = " << c1 << "\nc2 = " << c2;
23
24 Swap(i1, i2);
25 Swap(f1, f2);
26 Swap(c1, c2);
27
28 cout << "\n\nAfter passing data to function template.\n";
29 cout << "i1 = " << i1 << "\ni2 = " << i2;
30 cout << "\nf1 = " << f1 << "\nf2 = " << f2;
31 cout << "\nc1 = " << c1 << "\nc2 = " << c2;
32
33 return 0;
34}
35
1// template specialization
2#include <iostream>
3using namespace std;
4
5// class template:
6template <class T>
7class mycontainer {
8 T element;
9 public:
10 mycontainer (T arg) {element=arg;}
11 T increase () {return ++element;}
12};
13
14// class template specialization:
15template <>
16class mycontainer <char> {
17 char element;
18 public:
19 mycontainer (char arg) {element=arg;}
20 char uppercase ()
21 {
22 if ((element>='a')&&(element<='z'))
23 element+='A'-'a';
24 return element;
25 }
26};
27
28int main () {
29 mycontainer<int> myint (7);
30 mycontainer<char> mychar ('j');
31 cout << myint.increase() << endl;
32 cout << mychar.uppercase() << endl;
33 return 0;
34}
1// If two characters are passed to function template, character with larger ASCII value is displayed.
2
3#include <iostream>
4using namespace std;
5
6// template function
7template <typename T>
8T Large(T n1, T n2)
9{
10 return (n1 > n2) ? n1 : n2;
11}
12
13int main()
14{
15 int i1, i2;
16 float f1, f2;
17 char c1, c2;
18
19 cout << "Enter two integers:\n";
20 cin >> i1 >> i2;
21 cout << Large(i1, i2) <<" is larger." << endl;
22
23 cout << "\nEnter two floating-point numbers:\n";
24 cin >> f1 >> f2;
25 cout << Large(f1, f2) <<" is larger." << endl;
26
27 cout << "\nEnter two characters:\n";
28 cin >> c1 >> c2;
29 cout << Large(c1, c2) << " has larger ASCII value.";
30
31 return 0;
32}
1// function template in c++
2#include <iostream>
3using namespace std;
4float funAvg(int a, int b){
5 float avg = (a+b)/2.0;
6 return avg;
7}
8float funAvg2(int a, float b){
9 float avg2 = (a+b)/2.0;
10 return avg2;
11}
12
13//function template in c++
14
15template<class T1 , class T2>
16float fun(T1 a, T2 b){
17 float avg2 = (a+b)/2.0;
18 return avg2;
19}
20
21int main(){
22
23 float a;
24 a = funAvg(22 , 7);
25 printf("The average of these number is %.3f ",a);
26 cout<<endl;
27 float a1;
28 a1 = funAvg2(11 , 8.6);
29 printf("The average of these number is %.3f ",a1);
30 cout<<endl;
31 // float T;
32 // T = fun(11 , 8.6f);
33 // printf("The average of these number is %.3f ",T);
34 // cout<<endl;
35 // ---------------------function template in c++-----------------
36 float T;
37 T = fun(11 , 98);
38 printf("The average of these number is %.3f ",T);
39
40
41 return 0;
42}
1namespace std {
2 template<typename t> struct hash<MyClass<t>>
3 {
4 size_t operator() (const MyClass<t>& c) const;
5 }
6}
7
8// You can also do things like
9
10template<template<typename t> class type> func_name<type<t>>();