访问tuple_t的类型
Accessing the types of a tuple_t
我有这个代码:
auto myTuple = hana::tuple_t<int, char*, long>;
std::cout << boost::typeindex::type_id<decltype(myTuple[1_c])>().pretty_name() << std::endl;
这输出:
boost::hana::type_impl<char*>::_
我想访问"char*"类型,但如果我这样做:
std::cout << boost::typeindex::type_id<decltype(myTuple[1_c])::type>().pretty_name() << std::endl;
它输出:
error: 'decltype(myTuple[1_c])' (aka 'boost::hana::type_impl<char *>::_ &') is not a class, namespace, or scoped enumeration
std::cout << boost::typeindex::type_id<decltype(myTuple[1_c])::type>().pretty_name() << std::endl
这是因为它是一个参考,如果我这样做:
std::cout << boost::typeindex::type_id<decltype(boost::hana::traits::remove_reference(myTuple[1_c]))::type>().pretty_name() << std::endl;
然后它输出"char*"。
这是访问tuple_t类型的方式吗?必须有不那么繁琐的方法。
这确实很棘手。Hana 在 hana::type
上提供了一个一元加运算符,可将任何符合 ref 条件的hana::type
衰减为右值。所以基本上,
#include <boost/hana.hpp>
namespace hana = boost::hana;
using namespace hana::literals;
auto myTuple = hana::tuple_t<int, char*, long>;
using T = decltype(+myTuple[1_c])::type;
// ^~~~~ notice unary plus here
另请注意,您可能有兴趣使用 <boost/hana/experimental/printable.hpp>
中的hana::experimental::print
。这是一个实验性(因此不稳定)功能,但我可以向你保证,它最终应该以一种或另一种形式进入库:
#include <boost/hana.hpp>
#include <boost/hana/experimental/printable.hpp>
#include <iostream>
namespace hana = boost::hana;
int main() {
auto myTuple = hana::tuple_t<int, char*, long>;
std::cout << hana::experimental::print(myTuple) << std::endl;
}
输出:
(type<int>, type<char*>, type<long>)
编辑:一元加运算符记录在hana::type
的参考中。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 为什么 std::when_any 使用 std::tuple 而不是 std::vector 作为其结果类型?
- 如果创建支持返回可变参数类型列表的通用模板 API,我应该使用 std::tuple 还是其他东西?
- 类型安全 - all_of/ any_of/ none_of for std::tuple
- 来自std::tuple的引用绑定的类型
- C 模板递归以检查std :: tuple中的类型
- 如何创建具有指定数量的元素(相同类型)的 boost::tuple
- 如何有效地实现 std::tuple,使得空类型的元组本身就是空类型
- 将 BOUNDED std::参数列表<class>转换为类型 std::tuple<class,class,class> tup<classObj1, classObj
- 关于 std::tuple 对象上 std:get() 的返回类型的混淆
- std::tuple数组的std::array不完整类型错误
- 类元组类型(tuple,pair)的模板重载
- std::tuple,按继承类型获取项
- 使用tuple with class作为c++的类型
- 为什么"std::p air"将"std::tuple"作为ctor参数类型而不是"const std::tuple&"?
- 如何像访问数组一样访问Tuple(具有括号重载和类型安全)
- std::tuple是否接受auto作为类型