![]() |
|
|
|
#1 |
|
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! Gary Nastrasio |
|
|
|
|
#2 |
|
Posts: n/a
|
* 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 -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail? Alf P. Steinbach |
|
|
|
#3 |
|
Posts: n/a
|
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 Barry |
|
|
|
#4 |
|
Posts: n/a
|
On Oct 4, 3:47 am, Gary Nastrasio <noem...@please.com> wrote:
> 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 Senthil |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Python, syntax error for nothing | darkie | Software | 0 | 01-18-2009 11:36 AM |
| Looking for 2-up CD label software or template | M.L. | DVD Video | 14 | 05-31-2007 01:49 AM |
| Need help on Modelsim VHDL syntax? ASAP:) | kaji | General Help Related Topics | 0 | 03-14-2007 10:43 PM |
| Need help on a Modelsim VHDL Syntax? ASAP:) | kaji | Software | 0 | 03-14-2007 10:43 PM |
| Need Help on a Modelsim VHDL Syntax....ASAP:) | kaji | Hardware | 0 | 03-14-2007 10:41 PM |