当一个参数需要多个参数时,如何组合 2 个函数
How to combine 2 functions when one takes more than one argument?
我正在研究一个小型快速测试类,它将执行一个函数/函子,以及为要执行的函数生成输入的函数/函子。
例如,如果第一个函数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...>&¶ms)
{
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 编译和运行的
相关文章:
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 模板元编程:如何将参数包组合成新的参数包
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- 组合函数参数包和默认参数
- uncrustify:如何将多行 C 函数调用的参数组合到一行上?
- 如何在C++中找到可变参数类型表的所有组合
- 从组合类公开模板参数的替代方法
- 使用参数包在C++中组合 lambda 函数
- 将约束组合应用于参数包
- 组合类模板默认参数和变异参数
- 如何将多个可变参数模板元组类组合成一个类?
- 使用参数组合创建对象
- 组合模板参数形成函数签名时无效的 void 参数
- 在类组合中调用参数化构造函数
- 模板参数的组合排序
- C++组合参数模板元编程
- Boost.Parameter:与 CRTP 组合的命名模板参数
- 具有可变参数模板的功能组合
- 根据 qt 组合框更改参数
- "return-by-reference"或"pass-by-reference"参数何时与constexpr兼容?