简体   繁体   English

具有多个参数的局部类模板专业化

[英]partial class template specialization with multiple arguments

I have a template class Foo that takes two (or more) template arguments. 我有一个模板类Foo ,它需要两个(或更多)模板参数。 I want to use its type in a separate class Bar . 我想在单独的类Bar使用它的类型。 See the following simple example, which compiles without error: 请参见以下简单示例,该示例编译时不会出错:

template <typename T, typename U> class Foo { };
template <typename T, typename U> class Bar { };
int main()
{
  Bar<int, char> bar; // quick example -- int & char could be any 2 types
  return 0;
}

The above is somewhat tedious, especially if Foo takes many template arguments and the programmer has to retype them all. 上面有些乏味,特别是如果Foo接受许多模板参数并且程序员必须重新键入所有参数。 I would like to have something like the following instead, but it does not compile: 我希望有类似以下内容的东西,但它不能编译:

template <typename T, typename U> class Foo { };
template <typename T, typename U> class Bar; // base
template <typename T, typename U> class Bar< Foo<T, U> > { }; // specialization
int main()
{
  typedef Foo<int, char> FooType;
  Bar<FooType> bar;
  return 0;
}
test.cpp:3:60: error: wrong number of template arguments (1, should be 2)
test.cpp:2:45: error: provided for ‘template class Bar’
test.cpp: In function ‘int main()’:
test.cpp:7:18: error: wrong number of template arguments (1, should be 2)
test.cpp:2:45: error: provided for ‘template class Bar’
test.cpp:7:23: error: invalid type in declaration before ‘;’ token

I am especially perplexed because this partial specialization idiom works fine for a single template argument; 我特别困惑,因为这种部分专业化的用法对于单个模板参数可以很好地工作。 see the question titled: total class specialization for a template 请参阅标题为: 模板的总类专门化的问题

Edit I realized that, at least for my purposes, I could get around this using C++11 variadic templates as follows. 编辑我意识到,至少出于我的目的,我可以使用C ++ 11可变参数模板来解决此问题,如下所示。 I still want to know why the second example doesn't work, though. 我仍然想知道为什么第二个示例不起作用。

template <typename... FooTypes> class Bar;
template <typename... FooTypes> class Bar< Foo<FooTypes...> > { };

Your class template Bar<T, U> takes two template arguments, but your specialization is only given one: 您的类模板Bar<T, U>带有两个模板参数,但是您的专业化仅给出一个:

template <typename T, typename U> class Bar<Foo<T, U> > {};

Did you mean to have Bar take just one template argument and specialize it correspondingly? 您是说让Bar只接受一个模板参数并对其进行专门化吗?

template <typename T> class Bar;
template <typename T, typename U> class Bar<Foo<T, U> > {};

Note that a specialization can depend on a different number of template parameters but the specialization needs to get the same number of arguments. 请注意,专业化可以依赖于不同数量的模板参数,但是专业化需要获取相同数量的参数。 It also works the other way around: a full specialization can have no template parameter: 它也可以以其他方式工作:完全专业化可以没有模板参数:

template <> class Bar<int> {};

I'm a little confused about what you're trying to do in this line: 对于您在此行中要执行的操作,我有些困惑:

template <typename T, typename U> class Bar< Foo<T, U> > { }; // specialization

You're stating that the template requires two types, T and U, as type parameters. 您要说明模板需要两种类型,即T和U作为类型参数。 Foo is itself only one type though, the type created by instantiating the Foo template with those two arguments. 但是Foo本身只是一种类型,该类型是通过使用这两个参数实例化Foo模板创建的。

I see that you're expecting it to pick up and determine the T and U since you used them in both places, but that doesn't circumvent the fact that you only provided one type argument for a two type template specialization. 我看到您期望它能够拾取并确定T和U,因为您在两个地方都使用了它们,但这并没有绕过您只为两种类型模板专门化提供一种类型实参的事实。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM