Thursday, Aug 16th

Last update12:59:40 PM GMT

C++ Templates: the Final Touch

Write e-mail

Now it is time we finish off with the nuances of templates in C++. After being well versed with function templates and class templates, we will certainly like to move to a little more practical issues that we face while using in templates.

argaiv1770

Regular data types in template declaration

We already know the syntax of template declaration: template <class T> where we said T represents the template parameter in the form of datatype.

It is quite obvious to question here what if we wish to pass another parameter along with the template parameter T , say a regular datatype like int. This can very well be done in the below manner

template <class T, int N>
class myClass {
T myArray [N];
public:
void myFunction(int X, T value);
};

template <class T, int N>
void myClass<T,N>::myFunc (int X, T value) {
myArray[x]=value;
 }


Using the above template is also similar to how we have been doing so far.


myClass <int,10> myInt;


myClass.myFunc (1,50);

Moreover, we can even set the default value or type for class template parameter


template <class T=char, int N=10> class myClass {..};

So we can create objects like:


myClass<> myObj;

which we will be equivalent to

myClass<char,10> myObj;

As you can see, all this is so similar to how normal functions work.


Template Specialisation

There may be a case where we may require having a different behavior when a particular datatype is passed as the function parameter. In such a case to define a different implementation from other datatypes for this specific datatype we make use of Template Specialization.

// class template:
template <class T>
class myClass {
T element; 
public:   
myClass (T arg) {element=arg;}   
T increase () {return ++element;}
};

// class template specialization:
template <>
class myClass <char> {   
char element; 
public:   
myClass (char arg) {element=arg;}   
char increase ()   
{
if ((element>='a')&&(element<='z'))     
element+='A'-'a';     
return element;   
}
};

In the above example, we have a made a distinction in the implementation of template when a particular datatype ‘char’ is passed. While for other datatypes the function increase increments the element, on the other hand if ‘char’ is passed as the template parameter the function increase() changes the character to uppercase if it is in lowercase.

Here the first one is the generic template and the one below it is its specialisation.
Note the syntax for the two:

template <class T> class myClass { ... };//generic template
template <> class myClass <char> { ... };//specialisation for char

The specialisation has empty angle bracket for parameter list followed by specialised datatype in the angle bracket after it.
The specialisation should not be confused with inheritance, and hence all the member elements in the specialised template class must be defined, even those present in the generic one.


Drawbacks in Templates

The main drawback of templates comes into picture working with multiple file project. As you know for large projects, the interface and implementation are abstracted from each other with interface (prototypes of all the functions) residing in separate file called header (.h extension) and the corresponding implementations of the functions residing in other files.

The problem with templates is that from compiler point of view they are different from normal functions. They are compiled on demand,that is, compiled only when an instantiation of a function is required with a particular datatype. This forces a restriction on multiple file projects because now the declaration and implementation of a template class or function must be present in the same file. Hence separating interface and implementation is not possible for in case of templates.

Since no code is generated until a template is instantiated when required, compilers allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.

Share this post



Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.


Security code
Refresh

Web Hosting