Template Syntax

G

Gary Nastrasio

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 <class> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget> {...}

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


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

Thanks!
 
A

Alf P. Steinbach

* Gary Nastrasio:
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:

As I recall Andrei didn't say anything was "better". At least not
without also describing for what it would be better. I take it the
"since..." is your own invention.

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

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


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

It's a template (instead of a concrete type) as template argument.

Basically it removes redundancy in client code, and helps abstracting
that code, at the cost of also removing a choice. Usually that's what
one wants: that the design enforces some choices in order to simplify
client code and provide guarantees. E.g., that's why we use high level
file handling instead of going down to the sectors and disks level,
where there are more choices but also far more redundancy, and less
abstraction so that the code becomes more tied to a given disk drive.

Cheers, & hth.,

- Alf
 
S

Senthil

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 <class> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget> {...}

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

I'm very confused by the line "template <template <class> class
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::vector> mystack;
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 Cont> class 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<T> Container;
};

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<T> Container;
};

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top