typename template c++ code example
Example 1: c++ template function
template <class T>
void swap(T & lhs, T & rhs)
{
T tmp = lhs;
lhs = rhs;
rhs = tmp;
}
Example 2: function template
template <class T>
void swap(T & lhs, T & rhs)
{
T tmp = lhs;
lhs = rhs;
rhs = tmp;
}
void main()
{
int a = 6;
int b = 42;
swap<int>(a, b);
printf("a=%d, b=%d\n", a, b);
double f = 5.5;
double g = 42.0;
swap(f, g);
printf("f=%f, g=%f\n", f, g);
}
Example 3: cpp how to create an object of template class
template class Graph<string>;
Example 4: templates of templates c++
namespace std {
template<typename t> struct hash<MyClass<t>>
{
size_t operator() (const MyClass<t>& c) const;
}
}
template<template<typename t> class type> func_name<type<t>>();