Full size Banner

About template and thin template

Templates
C++ templates enable us to define a family of functions or classes that can operate on different types of information.
Use templates in situations that result in duplication of the same code for multiple types. For example, we can use function templates to create a set of functions that apply the same algorithm to different data types. You can also use class templates to develop a set of type safe classes.
Templates, which are sometimes called parameterized types, are mechanisms for generating functions and classes based on type parameters. By using templates, we can design a single class or function that operates on data of many types, instead of having to create a separate class for each type.

Class Templates
We can use class templates to create a family of classes that operate on a type. Class templates are parameterized types. They imply that a separate class could be created for each conceivable value of the parameters (known as template arguments) passed in.
Template arguments can be types or constant values of a specified type. For example,
// class_templates.cpp
#include <stdio.h>
template
class TempClass
{
public:
TempClass( void );
~TempClass( void );
int MemberSet( T a, int b );
private:
T Tarray[i];
int arraysize;
};

int main()
{
}

In this example, the template class uses two parameters, a type T and an int i. The T parameter can be passed any type, including structures and classes. The i parameter has to be passed an integer constant. Because i is a constant defined at compile time, you can define a member array of size i using a standard array declaration.

Class Templates
Class templates define a family of related classes that are based on the type arguments passed to the class upon instantiation. Function templates are similar to class templates but define a family of functions. With function templates, you can specify a set of functions that are based on the same code but act on different types or classes. The following function template swaps two items:
// function_templates1.cpp
template< class T > void MySwap( T& a, T& b ) {
T c(a);
a = b;
b = c;
}
int main()
{
}

This code defines a family of functions that swap the values of the arguments. From this template, you can generate functions that will swap int and long types and also user-defined types. MySwap will even swap classes if the class's copy constructor and assignment operator are properly defined.

In addition, the function template will prevent you from swapping objects of different types, because the compiler knows the types of the a and b parameters at compile time.

Thin Templates

  • Template code can lead to major increase in code size.
  • Symbian OS would likes code size optimized
  • Necessary code logic is implemented in generic base class that use type-unsafe TAny * pointer rather than templated code
  • A template class will be defined which uses private inheritance from this class to take advantage of the generic implementation.

class StackBase
{

public:
StackBase() {index = 0;}
void Push(void* aItem)
{
index++;
cout << "Push void* item in StackBase " << index << endl;
}
void* Pop()
{
index--;
cout << "Return void* item in StackBase " << index << endl;
return (void *)iStack[index];
}
private:
int index;
void *iStack[100];
};

template
class Stack : private StackBase
{
public:
void Push (T* aItem)
{
cout << "Push in Stack " << endl;
StackBase::Push((void*)aItem);
}

T* Pop ()
{
cout << "Pop in Stack " << endl;
return(T*)StackBase::Pop();
}
};


int main( )
{
Stack m1;
int i = 10;
m1.Push(&i);
int ii = 20;
m1.Push(&ii);

m1.Pop();
m1.Pop();
return 0;
}