STL 和 Hana 元组之间的转换
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
受支持 out-of-the-box,您只需包含 <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
,对吗?现在这是不可能的,请参阅 this 了解原因。但是,您可以简单地在 hana::for_each
中使用 std::tuple
,前提是您包括上面的适配器 header:
#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'; });
}
#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
受支持 out-of-the-box,您只需包含 <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
,对吗?现在这是不可能的,请参阅 this 了解原因。但是,您可以简单地在 hana::for_each
中使用 std::tuple
,前提是您包括上面的适配器 header:
#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'; });
}