Lambda函数,在编译时确定参数的数量

par*_*iad 11 c++ lambda templates c++11 c++14

我想声明一个具有N个参数的lambda函数,其中N是模板参数.就像是...

template <int N>
class A {
    std::function<void (double, ..., double)> func;
                        // exactly n inputs
};
Run Code Online (Sandbox Code Playgroud)

我想不出用元函数范式来做这个的方法.

nos*_*sid 15

您可以n_ary_function使用嵌套的typedef 编写模板type.此类型可以使用如下:

template <int N>
class A {
    typename n_ary_function<N, double>::type func;
};
Run Code Online (Sandbox Code Playgroud)

以下代码片段包含以下定义n_ary_function:

template <std::size_t N, typename Type, typename ...Types>
struct n_ary_function
{
    using type = typename n_ary_function<N - 1, Type, Type, Types...>::type;
};

template <typename Type, typename ...Types>
struct n_ary_function<0, Type, Types...>
{
    using type = std::function<void(Types...)>;
};
Run Code Online (Sandbox Code Playgroud)