我有两个元组,一个包含值,另一个元组包含对这些值的操作。 现在我想对每个值应用相应的操作,尽可能少的代码“开销”。 类似下面的简化示例。
#include <iostream>
#include <boost/hana.hpp>
namespace hana = boost::hana;
using namespace hana::literals;
struct ThinkPositive
{
void operator()(int &val) const
{
std::cout << "Think positive!\n";
val = std::abs(val);
}
};
struct Nice
{
void operator()(int &val) const
{
std::cout << val << " is nice!\n";
}
};
void numbers()
{
auto handlers = hana::make_tuple(Nice{}, ThinkPositive{});
auto nums = hana::make_tuple(5, -12);
auto handlers_and_nums = hana::zip(handlers, nums);
hana::for_each(handlers_and_nums, [](auto &handler_num) {
handler_num[0_c](handler_num[1_c]);
});
auto result = hana::transform(handlers_and_nums, [](const auto &handler_num) {
return handler_num[1_c];
});
hana::for_each(result, [](const auto num) {
std::cout << "got " << num << '\n';
});
}
int main()
{
numbers();
}
虽然上面的例子很有效,但修改nums的内容会更好。
有没有办法修改nums?
答案 0 :(得分:3)
你可以使用zip_with
,但它似乎违背了它的性质(它要求函数实际返回一些东西,但你的运算符()
不返回任何内容:
auto special_compose = [](auto&& l, auto&& r){ l(r); return 0; };
hana::zip_with(special_compose, handlers, nums);
如果您可以让操作员返回某些内容,则可以使用lockstep
:
hana::fuse(hana::fuse(hana::lockstep(hana::always(0)))(handlers))(nums);
如果没有外部lockstep
调用,应该会定义f
之类的内容,但我在文档中找不到任何内容。
更标准的解决方案(不能满足您对尽可能少的代码开销的要求):
template<typename Fs, typename Params, size_t... is>
void apply_in_lockstep_impl(Fs&& fs, Params&& ps, std::index_sequence<is...>){
int x[] = { (fs[hana::integral_c<size_t,is>](ps[hana::integral_c<size_t,is>]),0)... };
}
template<typename Fs, typename Params>
void apply_in_lockstep(Fs&& fs, Params&& ps){
static_assert(hana::size(fs) == hana::size(ps), "");
apply_in_lockstep_impl(std::forward<Fs>(fs),
std::forward<Params>(ps),
std::make_index_sequence<decltype(hana::size(ps))::value>{});
}
但是在通话网站上它更漂亮:
apply_in_lockstep(handlers, nums);
正如评论中所指出的,另一层次的间接也可以提供帮助。 这意味着将序列转换为指针序列,通过该序列修改原始值。
auto nums_ptr = hana::transform(nums, [](auto &num) { return # });
auto handlers_and_nums = hana::zip(handlers, nums_ptr);
hana::for_each(handlers_and_nums, [](auto &handler_num) {
handler_num[0_c](*handler_num[1_c]);
});
另一种更“传统”的方法是迭代范围。 这就像使用旧的for循环。
auto indices = hana::make_range(0_c, hana::length(handlers));
hana::for_each(indices, [&](auto i) {
handlers[i](nums[i]);
});