Is this valid template construct in C++ templates?
template < template <typename T2> class T>
void foo() {
}
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
Templates can be template parameters. In this case, they are called template parameters. The container adaptors std::stack, std::queue, and std::priority_queue use per default a std::deque to hold their arguments, but you can use a different container.
In UML models, template parameters are formal parameters that once bound to actual values, called template arguments, make templates usable model elements. You can use template parameters to create general definitions of particular types of template.
For example, given a specialization Stack<int>, “int” is a template argument. Instantiation: This is when the compiler generates a regular class, method, or function by substituting each of the template's parameters with a concrete type.
Yes. It is valid.
You can call this function with any class template which takes exactly one template parameter. For example,
template<typename T>
struct A
{
//...
};
foo< A >(); //ok
Note that you don't have to provide the template argument for A
class template, which means, the following would result in compilation error:
foo< A<int> >(); //error
Also, in your code T2
is optional, and in fact, you cannot use it in the function, so better remove it to make the definition simpler:
template < template <typename> class T>
void foo() {
T<int> x; //this is how T can be instantiated; provide template argument!
}
Demo : http://ideone.com/8jlI5
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