[英]Conversions Between STL and Hana tuples
#include <boost/hana.hpp>
#include <iostream>
#include <tuple>
namespace hana = boost::hana;
int main()
{
int x{7};
float y{3.14};
double z{2.7183};
auto t = hana::to<hana::tuple_tag>(std::tie(x, y, z));
hana::for_each(t, [](auto& o) { std::cout << o << '\n'; });
}
什么是實現這個的hana方式? 我意識到我可以使用: hana::make_tuple(std::ref(x), std::ref(y), std::ref(z))
,但這似乎不必要地冗長。
要在hana::tuple
和std::tuple
之間進行轉換,需要使std::tuple
成為有效的Hana序列。 由於std::tuple
支持開箱即用,因此您只需要包含<boost/hana/ext/std/tuple.hpp>
。 因此,以下代碼有效:
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>
#include <iostream>
#include <tuple>
namespace hana = boost::hana;
int main() {
int x{7};
float y{3.14};
double z{2.7183};
auto t = hana::to<hana::tuple_tag>(std::tie(x, y, z));
hana::for_each(t, [](auto& o) { std::cout << o << '\n'; });
}
請注意,您也可以使用hana::to_tuple
來減少冗長:
auto t = hana::to_tuple(std::tie(x, y, z));
話雖如此,既然你正在使用std::tie
,你可能想創建一個包含引用的hana::tuple
,對吧? 現在這是不可能的,看到這個是因為這個原因。 但是,您可以在hana::for_each
使用std::tuple
,前提是您包含上面的適配器頭:
#include <boost/hana.hpp>
#include <boost/hana/ext/std/tuple.hpp>
#include <iostream>
#include <tuple>
namespace hana = boost::hana;
int main() {
int x{7};
float y{3.14};
double z{2.7183};
auto t = std::tie(x, y, z);
hana::for_each(t, [](auto& o) { std::cout << o << '\n'; });
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.