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

时间:2013-05-30 11:21:52

标签: c++ templates c++11 variadic-templates

我正在尝试学习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的条目以func的通用方式传递给函数N

奖金问题:是否有可能取消主要内容中的std::function难看的转换并直接传入lambda?

2 个答案:

答案 0 :(得分:13)

鉴于众所周知的指数基础设施:

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;
};

这是live example

注意,您不需要在std::function中构造main()对象:std::function可以从lambda隐式构造。但是,您根本不需要在此使用std::function,可能会产生不必要的运行时开销。

在上面的解决方案中,我只是让可调用对象的类型成为模板参数,可以由编译器推导出来。

答案 1 :(得分:2)

您可以使用此实用程序模板在编译时创建索引序列:

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() );
}