私は2つのタプルを持っています。一つは値を含み、もう一つはこれらの値のアクションを含むタプルです。 これで、できるだけコードの「オーバーヘッド」を少なくして、各値に対応するアクションを適用したいと考えています。 次のような単純な例のようなものです。タプルの数でアクションのタプルを適用するには?
#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の内容を変更することがよりよいだろう動作しますが。
numを適切に修正する方法はありますか?
あなたがzip_with
を使用することができ
あなたは "場所に" と言うとき、あなたは "handlers_and_nums''で" 意味ですか、またはあなたが "nums' 'で" 意味ですか?後者の場合は、代わりに 'handlers_and_pointers_to_nums'を作成し、ポインタを介して間接的に作成できますか? (覚えていること: "コンピュータサイエンスの問題は、別のレベルの間接指示(間接指示のレベルが多すぎる場合を除きます)によって解決できます)" –