Pages

Thursday, November 24, 2011

All about C++ templates





Templates in C++ can be of class template or function template.
A template is used as a mechanism to generalize the parameter type we pass to either the class or function. This extends the function overloading concept of C++. Template takes the C++ concept of polymorphism to the next level.


Here is a example of function templates:
Let's say we have a swap function that swaps two variables of type T.

void f(int a, int b, float m, float n)
{
    swap(a, b);      //swap two integer values
    swap(m, n);      //swaps two float values
}

We can write a template function to do this as below:

template <class T>
void swap (T &x, T &y)
{
    T temp = x;
    x = y;
    y = T;
}

Similarly we have class template where we can pass any type of parameter using the template T.

template < class T>
class vector
{
     T *v; //type T vector, T can be int, float, anything
     int size;
   public:
     vector (int m)
     {
     v = new T[size = m]; // v is an array of type T
     //initialize the array
       for (int i = 0; i < size; i++)
         v[i] = 0;
    }

}


This is an example of a class vector which initialize an object of type T of array. Invoke it this way:
     vector < int> v1(10);    //a vector class of int
     vector < float> v2(20);    //a vector class of float



Q1. Find the o/p of the code snippet:
template < class T, int l=7> class Foo {
     static const T k = l;
     int q;

   public:
     Foo(int x = l) : q(x)
     { }
     T GetQ() const { return q; }
}

void f() {
     Foo < long, 9> foo(10);
     std :: cout << foo.GetQ();

}

Ans:
Explaination:
Default values for template parameters

Template parameters may have default values, and when instantiating the template these values can be omitted. Here’s an example:

template <typename T=int, int N=10>
class Foo {
};

Foo<float, 42> foo1;
Foo<double> foo2;
Foo<> foo3;

Note specifically the strange syntax for the definition of foo3, which means that it instantiates the Foo template with all parameters assigned their default values. Default values may only be specified for class templates.

Q2. What is STL?
Ans: STL is the C++ Standard Template Library. Instead of writing your own, you can use templates from STL just like other library functions that we use from any other C/C++ library. It is a collections of class templates and function templates. We sahould use template library as it is bug free rather than writing our own like inventing the wheel again.


In tehnical terms:
The Standard Template Libraries (STL's) are a set of C++ template classes to provide common programming data structures and functions such as doubly linked lists (list), paired arrays (map), expandable arrays (vector), large string storage and manipulation (rope), etc. The STL library is available from the STL home page. This is also your best detailed reference for all of the STL class functions available. STL can be categorized into the following groupings:

Here is the greatest post about C++ Templates:
http://eli.thegreenplace.net/2011/04/22/c-template-syntax-patterns/

Q3. What is partial specialization or template specialization?

Q4. How can you force instantiation of a template?

Q5. What is an iterator?

Q6. What is an algorithm (in terms of the STL/C++ standard library)?