Programmer's Wiki
Advertisement

Templates are a feature implemented in some languages that allow you to write functions and classes that can accept any type of parameter, when they would otherwise only accept one type of parameter.

C++[]

In C++, template functions are declared like so:

template<class T>
T add(T x, T y)
{
    T tmp = (x + y);
    return tmp;
}

and are implemented like:

add<int>(5, 5)

Template classes:

template <class T>
class test
{
private:
  T asdf;
};

test<int> instance;

Static member variables:

template <class T>
class test
{
private:
  static T stv;
};

template <class T> T test<T>::stv;
template <> int test<int>::stv = 4;


16px-Pencil.png
Advertisement