Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template syntax

Tags:

c++

templates

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?

like image 586
Naveen Avatar asked Aug 27 '26 12:08

Naveen


1 Answers

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.)

like image 133
Daniel Earwicker Avatar answered Aug 30 '26 03:08

Daniel Earwicker