Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template template parameter on function

Tags:

c++

templates

Is this valid template construct in C++ templates?

template < template <typename T2> class T> 
void foo() {

}
like image 749
user103214 Avatar asked Oct 16 '11 06:10

user103214


People also ask

Can function parameter template?

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.

Can a template be a template parameter?

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.

What are template parameters?

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.

Which is the correct example of template parameters?

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.


1 Answers

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

like image 179
Nawaz Avatar answered Oct 14 '22 19:10

Nawaz