Click here to Skip to main content
13,297,048 members (65,027 online)
Click here to Skip to main content
Add your own
alternative version


84 bookmarked
Posted 28 Jul 2002

A Beginners guide to Templates - Part 2

, 28 Jul 2002
Rate this:
Please Sign up or sign in to vote.
Using advanced template functionality


In the first part of this Beginner's Guide, we handled function and class templates. For this part, I show you some more useful stuff that can be done with templates.

Overloading function templates

Function templates can be overloaded with other function templates as well as with normal functions. The compiler will step through the list of possible function templates and creates the appropriate template function. The result is complemented with other possible template functions. This list is being searched by normal overloaded functions for the one that fits best.

Using friend and templates inside a template

Class templates like the one used in part 1 can include other templates or classes as well as having other classes as friends. When a class template includes another class, there are two possibilities:

  • The second inner class can be a common class. Therefore this inner class is also dependent from the template parameters. Otherwise the inner class is again a class template.
  • The outer class template contains another template that is dependent from the outer class as well as its own template parameters. In this first example, we include some list processing into a class template:
template < class ElemType >
class Tree

    class Node
        friend Tree < ElemType >;

Here the inner class Node is dependent from Tree and therefore gets its parameters. The outer class is defined as a friend of Node with a parameter list.

Template types

When using types that are declared within the scope of template parameters they have to be declared using the word typename:

template < typename T >
class X
    typename T::X theStuff; // T::X is the type

class Test
    class X { /* ... */ };

Without specifying the keyword typename, the compiler expects T::X to be a variable or constant and returns an error!

Element templates

When using element templates, it is possible e.g. to create a common class Builder that creates objects. This class provides an element function to allocate custom memory. This element function will be implemented as template element function and can be used for any types:

class Builder
    template < class T > static T* allocateMem();

Note: Template element functions can not be declared as virtual!


I hope I gave you an easy overview of the possibilities how templates can help you saving time while developing your projects. And remember, it also can save you a lot of code redundancy and typing errors!


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Stefan Spenz
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

Generalinner class... Pin
bagchisandeep27-Jan-06 3:36
memberbagchisandeep27-Jan-06 3:36 
Generaltemplate topics Pin
bryanallott30-Jul-02 5:57
memberbryanallott30-Jul-02 5:57 
GeneralRe: template topics Pin
Daniel Lohmann30-Jul-02 13:17
memberDaniel Lohmann30-Jul-02 13:17 
GeneralRe: template topics Pin
Stefan Spenz1-Aug-02 1:50
memberStefan Spenz1-Aug-02 1:50 
bryanallott wrote:
looking forward to Part III ?

I think about posting a sample app with templates. Can't say when this will be available...



When I was young my mother told me to clean up my room, I told her -1!
GeneralRe: template topics Pin
Colin Leitner22-Aug-02 10:28
memberColin Leitner22-Aug-02 10:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 29 Jul 2002
Article Copyright 2002 by Stefan Spenz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid