468,720 Members | 1,597 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Template Syntax

I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and
I'm a bit confused by one bit of template syntax in chapter 1.

Here is a code example:

template <class CreationPolicy>
class WidgetManager : public Creation Policy {...}

// Create an instance of WidgetManager which managers type Widget
WidgetManager< OpNewCreator<Widget MyWidgetMgr;

Basically the above creates a WidgetManager that handles creating new
Widgets - I completely understand this code. Since it's obvious a
WidgetManager will create type Widget, he says this code is better:

template <template <classclass CreationPolicy>
class WidgetManager : public CreationPolicy<Widget{...}

// Create an instance of WidgetManager which managers type Widget
WidgetManager<OpNewCreatorMyWidgetMgr;
I'm very confused by the line "template <template <classclass
CreationPolicy>". What exactly is going on in here?

Thanks!
Oct 3 '07 #1
2 1903
Gary Nastrasio wrote:

This thread can give you something to start with:
http://groups.google.com/group/comp....fcd960428f68f9

or try google "C++ template template parameter"
I think it's much faster than you ask here :-)
Oct 4 '07 #2
On Oct 4, 3:47 am, Gary Nastrasio <noem...@please.comwrote:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and
I'm a bit confused by one bit of template syntax in chapter 1.

Here is a code example:

template <class CreationPolicy>
class WidgetManager : public Creation Policy {...}

// Create an instance of WidgetManager which managers type Widget
WidgetManager< OpNewCreator<Widget MyWidgetMgr;

Basically the above creates a WidgetManager that handles creating new
Widgets - I completely understand this code. Since it's obvious a
WidgetManager will create type Widget, he says this code is better:

template <template <classclass CreationPolicy>
class WidgetManager : public CreationPolicy<Widget{...}

// Create an instance of WidgetManager which managers type Widget
WidgetManager<OpNewCreatorMyWidgetMgr;

I'm very confused by the line "template <template <classclass
CreationPolicy>". What exactly is going on in here?

Thanks!
Following is an extract of the mail i wrote to my team explaining the
template template parameter...Hope it helps...

I want you to write a declaration for a template class stack into
which i can push any data type and i can use any of the standard
library containers to implement the stack.
To put it simply,i want to write code as follows

Stack<int,std::vectormystack;
mystack.push(0);
mystack.push(19);

The immediate solution that occurs in our mind is to use
template.Correct!!!.But our problem is how to represent the
second parameter for the template
i.e
template <class T,????class Stack{
};

What shall i write in the place of ????.Lets try this one,

template<class T,class Contclass Stack {
Cont Container;
};
This is fine, but just one limitation.To use the above template i have
to write
Stack<int,vector<int mystack.

I had to repeat the int twice in my declaration and i am lazy enough
to resent that. Equivalently , I could have left out the first
template argument altogether.

To achieve this we need to use "template template parameter".i.e for a
template parameter we pass another template.
The above template class can be re-written as

template <class T, template < typename U class Cont class Stack{
Cont<TContainer;
};

or since we did not use U anywhere we can remove it from the
definition

template <class T, template < typename class Cont class Stack{
Cont<TContainer;
};

And here we are, we used template template parameter to pass a
template itself to a template class and
we also provided the user a very convenient form to represent his
data.
Best Regards,
Senthil
Oct 4 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Thomas Matthews | last post: by
1 post views Thread by Davide Pippa | last post: by
5 posts views Thread by Steve | last post: by
2 posts views Thread by adrian.hawryluk | last post: by
9 posts views Thread by Adam Nielsen | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
9 posts views Thread by bryonone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.