功能上的模板模板参数

use*_*214 11 c++ templates

这是C++模板中的有效模板构造吗?

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

}
Run Code Online (Sandbox Code Playgroud)

Naw*_*waz 13

是.这是有效的.

你可以调用任何类模板这需要该功能正是一个模板参数.例如,

template<typename T> 
struct A
{
   //...
};

foo< A >(); //ok
Run Code Online (Sandbox Code Playgroud)

请注意,您不必为A类模板提供模板参数,这意味着,以下将导致编译错误:

foo< A<int> >(); //error
Run Code Online (Sandbox Code Playgroud)

此外,在您的代码中T2是可选的,事实上,您不能在函数中使用它,因此最好将其删除以使定义更简单:

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

    T<int> x; //this is how T can be instantiated; provide template argument!
}
Run Code Online (Sandbox Code Playgroud)

演示:http://ideone.com/8jlI5