10

我正在尝试了解 C++11 中的可变参数模板。我有一个类,它基本上是一个std::array. 我希望能够将函数对象(最好是 lambdas)传递给成员函数,然后将std::array传递的元素作为函数对象的参数。

我已经使用 astatic_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给函数?funcN

奖励问题:是否可以取消难看的转换为std::functionin main 并直接传入 lambda?

4

2 回答 2

15

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

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

这是一个活生生的例子

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

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

于 2013-05-30T11:34:33.007 回答
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() );
}
于 2013-05-30T11:36:23.557 回答