这取决于你正在传递什么它,如果你试图实例化的模板需要作为参数接受2(或在C++ 11中可变数量)类的类模板,那么你可以将std :: vector传递给它。然而,在大多数情况下,模板需要类作为参数,并且不能传递类模板std :: vector。
template <class T>
struct gimme_a_type{};
template <template <class,class> class T>
struct gimme_a_template{};
gimme_a_type<std::vector> //<-- does not compile, expected a type, got a template
gimme_a_type<std::vector<int> > //<-- compiles, expected a type, got a type
gimme_a_template<std::vector> //<-- compiles, expected a template, got a template that has the correct signature
gimme_a_template<std::vector<int> > //<-- does not compile, expected a template, got a type
作为对编辑的回应,使用类模板作为模板参数存在困难。当你想要传递的类模板中有默认参数时(如我们的例子中的std::vector
),准确地匹配参数的数量实际上很难做到。 请注意,上面的例子需要一个类模板,它需要2个类,而不仅仅是一个。这是因为std::vector
需要两个参数,第二个参数对我们来说只是默认为std::allocator<T>
。
下面的例子说明这个问题:
template <template <class, class> class Tem>
struct A
{
Tem<int> v; //<-- fails to compile on gcc, Tem takes two parameters
Tem<int, std::allocator<int> >; //<-- compiles, but requires a priori knowledge of Tem
};
template <template <class...> class Tem>
struct A2
{
Tem<int> v; //<-- This C++11 example will work, but still isn't perfect.
};
的C++ 11的例子是更好的,但是如果有人通过了具有作为签名template <class, bool = false> class A3
再次失败一类,因为A2
需要一个类的可变参数数量,而不是可变数量的凡是。所以即使A3<int>
可能是一个有效的实例,你不能将该类传递给A2
。
解决方法是始终在模板参数列表中使用类型,并使用std::integral_constant
包装模板来传递整型常量。
您可以将其用作“模板模板参数”。 – Pubby
您必须实例化的模板是怎样的? –
http://stackoverflow.com/questions/6477365/c-templates-template-double-template – dchhetri