template

Declaring Generic Objects

Syntax:

    template <class data-type> return-type name( parameter-list ) {
    statement-list;
    }

Templates are used to create generic functions and generic classes and can operate on data without knowing the nature of that data. They accomplish this by using a placeholder data-type for which many other data types can be substituted. Example code: For example, the following code uses a template to define a generic swap function that can swap two variables of any type:

     template<class X> void genericSwap( X &a, X &b ) {
       X tmp;
 
       tmp = a;
       a = b;
       b = tmp;
     }
     int main(void) {
       ...
       int num1 = 5;
       int num2 = 21;
       cout << "Before, num1 is " << num1 << " and num2 is " << num2 << endl;
       genericSwap( num1, num2 );
       cout << "After, num1 is " << num1 << " and num2 is " << num2 << endl;
       char c1 = 'a';
       char c2 = 'z';
       cout << "Before, c1 is " << c1 << " and c2 is " << c2 << endl;
       genericSwap( c1, c2 );
       cout << "After, c1 is " << c1 << " and c2 is " << c2 << endl;
       ...
       return( 0 );
     }

The next template is used to descibe a generic class:

#include <cassert>
 
const unsigned int maxSize = 20;
 
template<class T>
class simpleStack
{
   public:
     simpleStack(): amount(0) {}
     bool empty() const { return amount == 0; }
     bool full() const { return amount == maxSize; }
     unsigned int size() const { return amount; }
     void clear() { amount = 0; }
     const T& top() const;
     void pop();
     void push( const T &x);
   private:
     unsigned int amount;
     T array[ maxSize ];
};
 
template<class T>
const T& simpleStack<T>::top() const
{
   assert( !empty() );
   return array[ amount - 1 ];
}
 
template<typename T> /*it's allowed and equal to replace class with typename*/
void simpleStack<T>::pop()
{
   assert( !empty() );
   --amount;
}
 
template<typename T>
void simpleStack<T>::push(const T &x)
{
   assert( !full() );
   array[ amount++ ] = x;
}
 
#include <iostream.h>
 
/* main code */
 
int main()
{
  simpleStack< int > aIntStack;
 
  int i = 100;
  while ( !aIntStack.full() )
      aIntStack.push( i++ );
  cout <<  "stack size: " << aIntStack.size() << endl;
  return 0;
}

Disambiguating Dependent-Names

The template keyword is also used to indicate that a Dependent Names refers to a template. In contrast to the placement of the typename keyword, the template keyword is placed just before the name of the template member. For example:

struct Foo {
  template<typename T>
  struct Member {
    typedef int type;
  };
};
 
template<class T>
struct Bar {
  typedef typename T::template Member<int>::type bar;
};
 
typedef Bar<Foo> FooBar;

Related Topics: typename