C++ 如何在可变参数模板中有多个参数包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9831501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I have multiple parameter packs in a variadic template?
提问by Samuel Danielson
Function one()accepts one parameter pack. Function two()accepts two. Each pack is constrained to be wrapped in types Aand B. Why is it impossible to instantiate two()?
函数one()接受一个参数包。函数two()接受两个。每个包都被限制在类型A和B 中。为什么不可能实例化two()?
template <typename T>
struct A {};
template <typename T>
struct B {};
template <typename... Ts>
void one(A<Ts> ...as) {
}
template <typename... Ts, typename... Us>
void two(A<Ts> ...as, B<Us> ...bs) {
}
int main() {
auto a = A<int>();
auto b = B<int>();
// Just fine
one();
one(a);
one(a, a);
// All errors
two();
two(a);
two(a, b);
}
Tried with gcc and clang.
尝试使用 gcc 和 clang。
sam@wish:~/x/cpp$ gcc -std=c++0x variadic_templates.cpp
variadic_templates.cpp: In function ‘int main()':
variadic_templates.cpp:23:7: error: no matching function for call to ‘two()'
variadic_templates.cpp:23:7: note: candidate is:
variadic_templates.cpp:11:6: note: template<class ... Ts, class ... Us> void two(A<Ts>..., B<Us>...)
variadic_templates.cpp:24:8: error: no matching function for call to ‘two(A<int>&)'
variadic_templates.cpp:24:8: note: candidate is:
variadic_templates.cpp:11:6: note: template<class ... Ts, class ... Us> void two(A<Ts>..., B<Us>...)
variadic_templates.cpp:25:11: error: no matching function for call to ‘two(A<int>&, B<int>&)'
variadic_templates.cpp:25:11: note: candidate is:
variadic_templates.cpp:11:6: note: template<class ... Ts, class ... Us> void two(A<Ts>..., B<Us>...)
sam@wish:~/x/cpp$ clang -std=c++0x variadic_templates.cpp
variadic_templates.cpp:23:3: error: no matching function for call to 'two'
two();
^~~
variadic_templates.cpp:11:6: note: candidate function template not viable: requires at least 1 argument, but 0 were provided
void two(A<Ts> ...as, B<Us> ...bs) {}
^
variadic_templates.cpp:24:3: error: no matching function for call to 'two'
two(a);
^~~
variadic_templates.cpp:11:6: note: candidate function not viable: requires 0 arguments, but 1 was provided
void two(A<Ts> ...as, B<Us> ...bs) {}
^
variadic_templates.cpp:25:3: error: no matching function for call to 'two'
two(a, b);
^~~
variadic_templates.cpp:11:6: note: candidate function not viable: requires 0 arguments, but 2 were provided
void two(A<Ts> ...as, B<Us> ...bs) {}
^
3 errors generated.
回答by Alexandre Hamez
Here is another way to have several parameters packs using template template parameters:
这是使用模板模板参数拥有多个参数包的另一种方法:
#include <iostream>
template <typename... Types>
struct foo {};
template < typename... Types1, template <typename...> class T
, typename... Types2, template <typename...> class V
, typename U >
void
bar(const T<Types1...>&, const V<Types2...>&, const U& u)
{
std::cout << sizeof...(Types1) << std::endl;
std::cout << sizeof...(Types2) << std::endl;
std::cout << u << std::endl;
}
int
main()
{
foo<char, int, float> f1;
foo<char, int> f2;
bar(f1, f2, 9);
return 0;
}
回答by Samuel Danielson
I found one solution. Wrap each parameter pack in a Tuple. Use a struct for partial specialization. Here's a demo that forwards arguments to a functor by consuming one tuple as a list and accumulating another. Well, this one forwards by copying. Tuples are used in type deduction yet no tuples are used in function parameters, which I think is neat.
我找到了一种解决方案。将每个参数包包装在一个元组中。使用结构进行部分特化。这是一个演示,它通过将一个元组作为列表使用并累积另一个来将参数转发给函子。嗯,这个通过复制转发。元组用于类型推导,但函数参数中没有使用元组,我认为这很简洁。
#include <iostream>
#include <tuple>
template < typename ... >
struct two_impl {};
// Base case
template < typename F,
typename ...Bs >
struct two_impl < F, std::tuple <>, std::tuple< Bs... > > {
void operator()(F f, Bs... bs) {
f(bs...);
}
};
// Recursive case
template < typename F,
typename A,
typename ...As,
typename ...Bs >
struct two_impl < F, std::tuple< A, As... >, std::tuple< Bs...> > {
void operator()(F f, A a, As... as, Bs... bs) {
auto impl = two_impl < F, std::tuple < As... >, std::tuple < Bs..., A> >();
impl(f, as..., bs..., a);
}
};
template < typename F, typename ...Ts >
void two(F f, Ts ...ts) {
auto impl = two_impl< F, std::tuple < Ts... >, std::tuple <> >();
impl(f, ts...);
}
struct Test {
void operator()(int i, float f, double d) {
std::cout << i << std::endl << f << std::endl << d << std::endl;
}
};
int main () {
two(Test(), 1, 1.5f, 2.1);
}
Tuples are a very good compile time list.
元组是一个非常好的编译时间列表。
回答by Mathematical Joe
Function templates (like skypHyman's example) and partial specializationsof class and variable templates can have multiple parameter packs if each template parameter subsequent to a template parameter pack either has a default value or can be deduced. The only thing I'd like to add/point out is that for class and variable templates you need a partial specialization. (See: C++ Templates, The Complete Guide, Vandevoorde, Josuttis, Gregor 12.2.4, Second Edition)
如果模板参数包之后的每个模板参数都具有默认值或可以推导出来,则函数模板(如 skypHyman 的示例)以及类和变量模板的部分特化可以具有多个参数包。我唯一想补充/指出的是,对于类和变量模板,您需要部分专业化。(参见:C++ 模板,完整指南,Vandevoorde,Josuttis,Gregor 12.2.4,第二版)
// A template to hold a parameter pack
template < typename... >
struct Typelist {};
// Declaration of a template
template< typename TypeListOne
, typename TypeListTwo
>
struct SomeStruct;
// Specialization of template with multiple parameter packs
template< typename... TypesOne
, typename... TypesTwo
>
struct SomeStruct< Typelist < TypesOne... >
, Typelist < TypesTwo... >
>
{
// Can use TypesOne... and TypesTwo... how ever
// you want here. For example:
typedef std::tuple< TypesOne... > TupleTypeOne;
typedef std::tuple< TypesTwo... > TupleTypeTwo;
};