传入std::array作为模板可变函数的参数

Passing std::array as arguments of template variadic function

本文关键字:函数 参数 std array 传入      更新时间:2023-10-16

我正在尝试学习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;
}

所以我的问题(如代码中所示)是如何将data的条目以与N通用的方式传递到函数func上?

附加问题:是否有可能消除在main中不美观的转换为std::function并直接传递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对象:std::function可以从lambda隐式构造。但是,在这里甚至不需要使用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() );
}