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::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'; });
}
相关文章:
- 构造函数和转换运算符之间的重载解析
- C ++中无符号位长度类型之间的隐式转换,即uint8_t,uint16_t
- 模板转换运算符在 clang 6 和 clang 7 之间的区别
- 派生类(构造函数具有参数)和基类(构造函数缺少参数)之间没有可行的转换
- Cxx.jl 在 Julia Complex 和 std::complex 之间进行转换
- 如何在 std::string 和 Aws::String 之间进行转换?
- 转换字符时Arduino DUE 和 Arduino UNO 之间的区别
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 指向 POD 类型的指针之间的静态转换与重新解释转换
- 在 Rcpp 中的字符串类型之间转换时出错
- 如何声明不同类型的模板化类之间的转换
- C 和 C++ 中的函数指针转换之间的差异
- 与浮点转换之间的规范化整数
- 重新解释强制转换和 C 样式转换之间的 C++ 区别
- C++ get 和类型转换之间的用法有什么区别?我应该使用哪一个?
- printf函数参数之间的序列点;转换之间的顺序点是否重要
- Clang和GCC中不明确基类转换之间的行为差异
- 转换之间的双和字节数组,为传输ZigBee API
- 隐式转换和显式转换之间的区别
- interpret_cast和C样式强制转换之间的区别是什么