Template Syntax

I read a book about templates and found the following code snippet:

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

I did not get the nested templates specified for CreationPolicy (which is again a template). What is the meaning of this strange syntax?

+3
source share
2 answers

This means that CreationPolicy must also be a template that accepts a single type parameter. You can think of it as a pattern equivalent to function pointers or callbacks.

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

 CreationPolicy<SomeType> 

This would not have been possible if CreationPolicy had not been declared as a "template template parameter" (yes, that’s really what they are called.)

+5
source

This is a template template parameter.

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

Basically, CreationPolicy is a template parameter, with the restriction that it should be a template with one parameter.

+2
source

All Articles