Lambda函数,其编译时确定的参数数 [英] Lambda function with number of arguments determined at compile-time

查看:135
本文介绍了Lambda函数,其编译时确定的参数数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想声明一个带有N个参数的lambda函数,其中N是一个模板参数。类似...

I would like to declare a lambda function with exactly N parameters, where N is a template argument. Something like...

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

我想不出一种方法来实现元功能范例。

I could not think of a way to do this with the metafunction paradigm.

推荐答案

可以使用嵌套的typedef 类型编写一个模板 n_ary_function / code>。此类型可以如下使用:

You can write a template n_ary_function with a nested typedef type. This type can be used as follows:

template <int N>
class A {
    typename n_ary_function<N, double>::type func;
};

遵循 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...)>;
};

这篇关于Lambda函数,其编译时确定的参数数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆