Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

template template parameters

Status
Not open for further replies.

zerbitx

Programmer
Sep 13, 2005
2
US
I thought I was familiar with template syntax until...
I'm reading "Modern C++ Design" and was comfortable with the code:

//Library Code
template <class CreationPolicy>
class WidgetManager : public CreationPolicy
{
...
};

//Application Code
typedef WidgetManager< OpNewCreator<Widget> > MyWidgetMgr;


Then expanding the example so the user of the template wouldn't have to specify <Widget> after "OpNewCreator" the code became:

//Library Code
template <template <class> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget>
{
...
};

//Application Code
typedef WidgetManager<OpNewCreator> MyWidgetMgr;

I don't understand how the Library Code here works. I assume its getting the Widget from its being derived from the templated class CreationPolicy<Widget>, but how that gets put into the above is beyond me. Can someone either provide a specific explanation of what this is doing, a general explanation that would encompass all such questions, or both? Thanks in advance, for even reading it over.

-ryan
 
Code:
//Library Code
template <class CreationPolicy>
class WidgetManager : public CreationPolicy
{
 ...
};
[green]//this is basic, no?[/green]

//Application Code
typedef WidgetManager< OpNewCreator<Widget> > MyWidgetMgr;
[green]//From now on MyWidgetMgr is short for the type WidgetManager< OpNewCreator<Widget> >[/green]

[red]/*
Then expanding the example so the user of the template wouldn't have to specify <Widget> after "OpNewCreator" the code became:
*/[/red]

//Library Code
template <template <class> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget>
{
 ...
};
[green]// take a look at [URL unfurl="true"]http://www.informit.com/articles/article.asp?p=167842&seqNum=6&rl=1[/URL]
// appears to be a templated template.[/green]

//Application Code
typedef WidgetManager<OpNewCreator> MyWidgetMgr;
[green] and here again is the type def to make life easier[/green]

[plug=shameless]
[/plug]
 
Thanks for the link, but that's actually and excerpt from the book that I'm reading that confused me in the first place.

I just don't get how that saves you from having to specify Widget in the typedef.

The original example makes perfect sense to me. Is it that it inherits from a specified CreationPolicy (CreationPolicy<Widget>) that defaults the argment <class> to be of that type? I guess what I'm after is an answer to how the compiler is expanding it all.

-Ryan
 
If you read chapter 1 you will see this....
Code:
template<template<class Created>class CreationPolicy>
class WidgetManager:public CreationPolicy<Widget>
{
   ...
}
In spite of appearances, the Created symbol does not contribute to the definition of WidgetManager. You cannot use Created inside WidgetManager-it is a formal argument for CreationPolicy(not WidgetManager) and can simply be omitted.
I think this says it all really. If you stare at it long enough and reread it a couple of times it will all click into place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top