473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2180
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
1
by: Davide Pippa | last post by:
Hi! I'm trying to compile the code above code (actually a similar one...), and I get this error (with gcc 3.2.2) : trick_1.cpp:16: template parameters not used in partial specialization:...
5
by: Steve | last post by:
Hi, Does C++ allow the programmer to declare a template with in a template so that a generic function can instantiate the embedded template? For example, could code such as this exist: ...
4
by: Howard Gardner | last post by:
// I think that there is no way to write the template "cant_write_me" in // this example. Would love to be wrong. // one of many approaches that won't work template< template< typename class...
5
by: chrisstankevitz | last post by:
Hi, Q1: Is there a way to make a template function that works only for specific types which produces a compiler error if used with an invalid type? Q2: If not, how do people deal with this...
2
by: Nick | last post by:
I'm learning C++ and ran into a compile error using Visual C++ 2005 Express on the following example program (located at http://www.cplusplus.com/doc/tutorial/templates.html): // template...
1
by: toton | last post by:
Hi, I am doing some template specialization for a template class, where specialization is done on a member function. I am not able to get exact syntax for it. The code is give as below, enum...
2
by: adrian.hawryluk | last post by:
Hi everyone, I've been using templates for a while, but I'm not at full power yet (knowledge=power) ;). Does anyone know where I can get information on this 'new' template usage? template<a...
9
by: Adam Nielsen | last post by:
Hi all, I'm a bit confused about the syntax used to access a nested template class. Essentially I have a bunch of class types to represent different types of records in a database, and I want...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.