[英]Passing std::array as arguments of template variadic function
我正在尝试学习C ++ 11中的可变参数模板。 我有一个基本上是std::array
的包装器的类。 我希望能够将函数对象(最好是lambdas)传递给成员函数,然后将std::array
的元素作为函数对象的参数传递。
我已经使用static_assert
来检查参数的数量是否与数组的长度匹配,但是我无法想到一种将元素作为参数传递的方法。
这是代码
#include <iostream>
#include <array>
#include <memory>
#include <initializer_list>
using namespace std;
template<int N, typename T>
struct Container {
template<typename... Ts>
Container(Ts&&... vs) : data{{std::forward<Ts>(vs)...}} {
static_assert(sizeof...(Ts)==N,"Not enough args supplied!");
}
template< typename... Ts>
void doOperation( std::function<void(Ts...)>&& func )
{
static_assert(sizeof...(Ts)==N,"Size of variadic template args does not match array length");
// how can one call func with the entries
// of data as the parameters (in a way generic with N)
}
std::array<T,N> data;
};
int main(void)
{
Container<3,int> cont(1,2,3);
double sum = 0.0;
auto func = [&sum](int x, int y, int z)->void{
sum += x;
sum += y;
sum += z;
};
cont.doOperation(std::function<void(int,int,int)>(func));
cout << sum << endl;
return 0;
}
所以我的问题(如代码所示)是如何以一种与N
通用的方式将data
条目传递到函数func
上?
额外的问题:是否有可能避免将main的std::function
难看地转换为main并直接传递一个lambda?
鉴于著名的索引基础架构:
namespace detail
{
template<int... Is>
struct seq { };
template<int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };
template<int... Is>
struct gen_seq<0, Is...> : seq<Is...> { };
}
您可以通过以下方式重新定义类模板:
template<int N, typename T>
struct Container {
template<typename... Ts>
Container(Ts&&... vs) : data{{std::forward<Ts>(vs)...}} {
static_assert(sizeof...(Ts)==N,"Not enough args supplied!");
}
template<typename F>
void doOperation(F&& func)
{
doOperation(std::forward<F>(func), detail::gen_seq<N>());
}
template<typename F, int... Is>
void doOperation(F&& func, detail::seq<Is...>)
{
(std::forward<F>(func))(data[Is]...);
}
std::array<T,N> data;
};
这是一个生动的例子 。
请注意,您不需要在main()
构造std::function
对象:可以从lambda隐式构造std::function
。 但是,您甚至根本不需要在这里使用std::function
,这可能会导致不必要的运行时开销。
在上面的解决方案中,我只是将可调用对象的类型设为可以由编译器推导的模板参数。
您可以使用此实用程序模板在编译时创建索引序列:
template< std::size_t... Ns >
struct indices {
typedef indices< Ns..., sizeof...( Ns ) > next;
};
template< std::size_t N >
struct make_indices {
typedef typename make_indices< N - 1 >::type::next type;
};
template<>
struct make_indices< 0 > {
typedef indices<> type;
};
然后,创建一个将indices
作为参数的调用程序函数,以便您推断出索引序列:
template<typename... Ts, size_t...Is>
void call(std::function<void(Ts...)>&& func, indices<Is...>)
{
func( data[Is]... );
}
然后您可以这样称呼它:
template< typename... Ts>
void doOperation( std::function<void(Ts...)>&& func )
{
static_assert(sizeof...(Ts)==N,"Size of variadic template args does not match array length");
call( std::forward<std::function<void(Ts...)>>(func), typename make_indices<N>::type() );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.