Symbian OS provides its own variation of templates. This is partly because
compilers at the time of EPOC’s inception lacked sufficient template
support. More importantly, templates may lead to code duplication if not
used correctly.
The Symbian thin template idiom works by implementing the essential
functions (for example Pop() and Push() for a stack template) in a
protected base class and using void* (TAny*) pointers to provide typeagnostic
behavior. The base class is privately inherited (see Section 3.2)
with the derived class using a regular C++ template to specify the type.
class StackBase // Implementation class
{
protected: // template interface *hidden*
void Push(void* item);
void* Pop();
private:
void* iStack[100]; // or some other internal representation
};
template<typename T>
class Stack : private StackBase
{
public:
void Push(T* item) {StackBase::Push((void*)item);} // must be inline
T* Pop() {(T*)StackBase::Pop();} // must be inline
};
The Stack class implements its functions inline to reduce the class
overheads, only writing the code where it is needed. In effect the Stack
template is a type-safe wrapper for the underlying type-agnostic Stack-
Base container class.