Recent Changes - Search:

Coding C++

Computing

Procedural: Variables, types, scopes

Trail Index | More | Templates

Templates are a very powerful mechanism of C++ that enables you to write code which does not depend on the specific type used, but still allows the compiler to enforce strict type safety.

/*
 * function overloading allows this
 */

double f(const double x) { return 2*x; }
int f(const int x) { return 2*x; }

/*
 * but it's much nicer to write it once only
 */

template <class T>  T f(const T x) { return 2*x; }

/*
 * also class templates
 */

template <class T>  class Something {
  T data1;
  T data2;
  int counter;
};

int main() {}

< More | Trail Index | Makefiles >

Edit - History - Print - Recent Changes - Search
Page last modified on April 11, 2007, at 02:44 am