473,626 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Wi dget 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<O pNewCreatorMyWi dgetMgr;
I'm very confused by the line "template <template <classclass
CreationPolicy> ". What exactly is going on in here?

Thanks!
Oct 3 '07 #1
2 2204
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<Wi dget 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<O pNewCreatorMyWi dgetMgr;

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...Hop e 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.Correc t!!!.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,vecto r<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
2373
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++ newsgroups, "Thinking In C++" to no avail. Background ----------
1
1446
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: trick_1.cpp:16: `P_args' Does anybody spot the error ? I know probably this code is invalid,
5
2161
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: template< class T<int N> > int func() { int the_n = N;
4
1820
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 struct cant_write_me{}; template< template< typename, typename class struct cant_write_me{}; template< typename struct arity_one{}; template< typename, typename struct arity_two{};
5
2359
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 issue? I believe A1 is no. I tried below with template function "f". For A2, functors work but have ugly syntax and runtime overhead. A
2
3922
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 specialization #include <iostream> using namespace std; template <class T> class container {
1
1614
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 DirectionType{ dtX,dtY,dtXD,dtYD }; template<typename T> class Point{
2
2003
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 (b, c, d)> Does one define it like:
9
1960
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 to store some cache data for each datatype. The simplified code below demonstrates my issue - I think I understand why it doesn't work as is (the structure only declares the storage, I need to instantiate it for each template type) but I'm not...
7
5762
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5576
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.