当一个函数接受多个参数时,如何合并两个函数?

无意义

我正在研究一个小的小型测试类,它将使用一个函数/函数来执行,而函数/函数会生成对该函数的输入来执行。

例如,如果第一个函数是第一个函数sqrt(x),而第二个函数有一个状态并且正在计数,我希望该类执行

sqrt(0.0);  
sqrt(1.0);
...

对于一些迭代。

我可以相对容易地做到这一点(2 std::functions或模板),但是如果第一个函数接受多个参数,该如何实现呢?我只能想到强制一个函数采用tuple,而强制另一个函数返回tuple,但这看起来很丑陋,并在测试的函数上强制使用接口。

编辑:我现在所拥有的:

template <typename Func, typename ArgGen>
class Magic
{
    std::function<double (time_t)> time_to_frequency;
    std::set<decltype(Func()(ArgGen()()))> results_ok;
    std::set<decltype(Func()(ArgGen()()))> results_fail;
    ArgGen ag;
    Func f;
public:
    Magic(std::set<decltype(Func()(ArgGen()()))> results_ok, std::set<decltype(Func()(ArgGen()()))> results_fail,  std::function<double (const time_t)> time_to_frequency) : 
               results_ok(results_ok), results_fail(results_fail),  time_to_frequency(time_to_frequency)
    {}
    Magic() 
    {}
    void run(const int n)
    {
       bool success=true;
       for (int i=0; i< n; ++i)
       {
           auto rv = f(ag());
           const bool result_ok = results_ok.count(rv);
           if (result_ok)
           {
               printf(".");
           }
           else
           {
               success = false;
               std::cout << i <<": value unexpected "<< std::endl;
           }

       }
       std::cout << "run succeeded: " << std::boolalpha << success;
    }

};
爵士先生

我将让ArgGen返回一个元组,然后使用以下命令打开该元组的包装:

    #include <tuple>
    #include <iostream>

    template <class Func, class ... Args>
    struct invoke_from_tuple_helper
    {
        typedef decltype(std::declval<Func>()(std::declval<Args>()...)) return_type;

        template <unsigned total, std::size_t ... Indices>

        static return_type apply_helper(Func && f, std::tuple<Args...> && params,
                                 typename std::enable_if<total == sizeof...(Indices)>::type* =NULL)
        {
            return f(std::forward<Args>(std::get<Indices>(params))...);
        }

        template <unsigned total, std::size_t ... Indices>
        static return_type apply_helper(Func && f, std::tuple<Args...> && params,
                                 typename std::enable_if<(total > sizeof...(Indices)) >::type* =NULL)
        {
            return apply_helper<
                total, Indices..., sizeof...(Indices)>
                (std::forward<Func>(f), std::forward<std::tuple<Args...> >(params));
        }

        static return_type apply(Func && f, std::tuple<Args...> && params)
        {
            return apply_helper<sizeof...(Args)>(std::forward<Func>(f),
                                                 std::forward<std::tuple<Args...> >(params));
        }

    };

    template <class Func, class ... Args>
    typename invoke_from_tuple_helper<Func,Args...>::return_type
    invoke_from_tuple(Func && f, std::tuple<Args...>&&params)
    {
        return invoke_from_tuple_helper<Func,Args...>::apply(std::forward<Func>(f),
                                                             std::forward<std::tuple<Args...> >(params));
    }

    void f(int, long, std::tuple<int,long>)
    {
        std::cout << "I'm on the inside\n";
    }

    int main()
    {
        invoke_from_tuple(f, std::tuple<int,long,std::tuple<int,long> >());
        return 0;
    }

这是使用g ++-4.8编译并运行的

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

实现一个接受两个不同类的参数的通用函数?

试图将两个Select Case函数合并为一个For循环参数

将两个或多个 lambda 函数合并为一个

如何在C ++中创建一个函数原型,该函数原型接受两个整数作为参数并返回它们的差值?

一个函数调用中的两个参数

合并两个数组并应用一个函数

如何遍历两个列表的组合并每次执行一个函数?

如何将两个代码合并为一个函数?

如何将一个列表中的一个元素与另一个列表的每个元素一起传递给一个接受两个参数的函数?

创建一个函数,该函数接受两个名为键和值的列表作为参数并返回一个数据帧

如何编写一个scala函数来接受两种类型的参数?

如何使一个方法接受一个或两个参数?

如何在Dart / Flutter中声明一个接受多个参数的函数回调?

如何使一个函数接受另一个函数的值的参数?

如何编写一个可以接受一个或多个参数并发送回加法的函数

如何创建一个接受两个参数(numRows、numColumns)并返回具有正确网格值的二维数组的 JavaScript 函数?

如何在不同的场合在一个函数中传递两个参数?

如何在反应中将两个 onClick 参数传递给一个函数

如何使用两个参数创建一个函数来查找节点是否存在?

如何创建一个函数来查找给定两个参数的可整除位数

可以接受两个对象或合并对象的重载函数

Haskell-如何将两个Monadic Maybe函数合并为一个函数

定义一个 TypeScript 函数,该函数将接受一个可以有多个签名的函数的参数?

反应本机如何将两个数组与一个函数合并

如何编写一个带有两个参数的python函数,一个是列表

将两个函数变量合并为一个函数

用一个函数和一个参数生成两个序列

如何使一个函数接受两个带有返回相等值的字符串的集合?(JavaScript)

如何迭代两个函数参数的所有组合并在r中返回一个'n by m'矩阵