函数上的模板模板参数 [英] Template template parameter on function

查看:100
本文介绍了函数上的模板模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is this valid template construct in C++ templates?

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

}


推荐答案

是的。它是有效的。

您可以使用任何采用 完全 一个模板参数的类模板调用此函数。例如,

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

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

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

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

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!
}

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

这篇关于函数上的模板模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆