[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppTemplate

(C++) template

Keyword enabling to Templates make it possible to use one function or class to handle many different data types. Exceptions provide a convenient uniform way to handle errors that occur within classes.

Template function examples

Suppose that you want to write a function that returns the absolute value of 2 numbers. As you no doubt remember from high school algebra, the absolute value of a number is its value without regard to its sign: The absolute value of 3 is 3 and the absolute value of -3 is also 3.

Ordinarily, this function would be written for a particular data type:

int MyAbs(int n)   //absolute vallue of ints
{
  return (n < 0 ? -n : n); //if n is negative , return -n
}


Here the function is defined to take an argument of type int and return a value of this same type. But now suppose you want to find the absolute value of a type double.

double MyAbs(double n)
{
  return ( n < 0 ? -n : n);
}


The body of the function is written the same way in each case, but they are completely different functions because they handle arguments and return values of different types.
Its true that in C++ these functions can all be  ?overloaded to have the same name but you must nevertheless write a seperate definition for each one.

Examples

//Sum two values of equal data type
template <class T>
T sum(const T& value1, const T& value2)
{  
  return value1 + value2;
}
template <class T>
T swap(T& value1, T& value2)
{  
  const T temp = value1;
  value1 = value2;
  value2 = temp;
}
//Sum a vector of any type
template <class T>
T sum(const std::vector<T>& v)
{  
  const int size = v.size(); //A vector's size is always of type int
  T mySum = 0.0;
  for(int i=0; i<size; ++i); //An index counter is always of type int
  {
    mySum+=v[i];
  }
  return mySum;
}


Note that all these functions are in the STL (the latter is called accumulate) and are all templated.

Template class examples

A template class defines how group of classes should be generated as opposed to a class in C++ as being how the group of objects should be generated. An example:

template <class T>
struct MyClass
{
  T mValue;
};
int main() 
{ 
  MyClass<int> m; 
  m.mX = 10; 
} 


Many classes from the STL ('Standard Template Library') are templates. So that's where that name came from!

Template metaprogramming examples

Example to calculate the factorial:

  1. include <iostream>
template <unsigned int N> struct factorial { static unsigned const value = N * factorial<N-1>::value; }; template <> struct factorial<0> { static unsigned const value = 1; }; int main() { std::cout << factorial<0>::value << std::endl << factorial<1>::value << std::endl << factorial<2>::value << std::endl << factorial<3>::value << std::endl << factorial<4>::value << std::endl << factorial<5>::value << std::endl << factorial<6>::value << std::endl << factorial<7>::value << std::endl << factorial<8>::value << std::endl << factorial<9>::value << std::endl << factorial<10>::value << std::endl << factorial<11>::value << std::endl << factorial<12>::value << std::endl; }


Code links



last edited (June 17, 2007) by bilderbikkel, Number of views: 5256, Current Rev: 16 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.