当一个参数需要多个参数时,如何组合 2 个函数

How to combine 2 functions when one takes more than one argument?

本文关键字:参数 何组合 函数 组合 一个      更新时间:2023-10-16

我正在研究一个小型快速测试类,它将执行一个函数/函子,以及为要执行的函数生成输入的函数/函子。

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

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

进行一定次数的迭代。

我可以相对容易地做到这一点(2 std::function 秒或模板),但是如果第一个函数需要超过 1 个参数,将如何实现它?我只能想到强制一个函数取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 insiden";
    }
    int main()
    {
        invoke_from_tuple(f, std::tuple<int,long,std::tuple<int,long> >());
        return 0;
    }

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