tags:

views:

120

answers:

2
+3  Q: 

Template syntax

I was reading a book on templates and found the following piece of code:

template <template <class> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget>
{
...
void DoSomething()
{
Gadget* pW = CreationPolicy<Gadget>().Create();
...
}
};

I didn't get the nested templates specified for the CreationPolicy (which is again a template). What is the meaning of that weird looking syntax?

+5  A: 

It means that CreationPolicy must also be a template, which accepts one type parameter. You can think of it as a little like the template equivalent of function pointers, or callbacks.

As you can see in that example, CreationPolicy is used with an argument:

CreationPolicy<SomeType>

That wouldn't be possible unless CreationPolicy had been declared as a "template template parameter" (yes, that's really what these are called.)

Daniel Earwicker
+2  A: 

It's a Template Template Parameter.

See http://www.comeaucomputing.com/techtalk/templates/#ttp

Basically CreationPolicy is the template parameter, with the constraint that it must be a templated class with one parameter.

Douglas Leeder