I was searching for something else related to template template parameters and happened upon this answer which claims that template template template parameters are not allowed by the standard.
However, the following code compiles in the latest clang (3.2) and the latest GCC (4.8):
template<template<template<typename> class> class T> struct test {};
template<template<typename> class T> struct foo {};
test<foo> bar;
Is this an extension, or is the other answer actually incorrect and it is allowed by the standard? If not, is there any particular reason for the omission?
A template argument for a type template parameter must be a type-id, which may name an incomplete type: A template argument for a template template parameter must be an id-expression which names a class template or a template alias. When the argument is a class template, only the primary template is considered when matching the parameter.
This means the second template argument should be a template requiring two template parameters. The first template parameter is the type of elements the container stores and the second template parameter is the defaulted allocator a container of the standard template library has.
Okay, types are the most often used template parameters. Here are a few examples: Integrals are the most used non-types. std::array is the typical example because you have to specify at compile time the size of a std::array:
Normal functions just accept arguments like normal templates just accept types. However, some functions accept function pointers which accept arguments, just like template template types accept templates that accept types: To answer your question in the comments: template template template parameters are not possible.
In std::vector<int>
the class template std::vector
is passed the type int
as a parameter. In std::get<42>(some_tuple)
, the function template std::get
is passed the value 42
as a parameter. Perhaps unimaginatively the former kind of argument is called a type argument of a template (or template type argument) while the latter kind is a (template) non-type argument.
But templates can also accept another kind of arguments: other templates. For instance template<template<typename> class T> void foo();
declares a function template taking a template as argument, that itself takes a type argument. (As a note, while templates are not types the term 'non-type argument' still does not cover template template arguments. It is reserved for arguments like template<int NonTypeArgument>
.)
Since there is no such thing as a template template in C++ (there are class, function, and alias templates -- but they're collectively simply 'templates' anyway), there is no such thing as a template template template parameter. What you have is a run off the mill template template parameter, where the expected template argument has a template template argument itself. I cannot find a reference in the Standard that forbids this, like the answer you link claims.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With