hana :: second无法推断类型 [英] hana::second can't deduce type

查看:74
本文介绍了hana :: second无法推断类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用hana::second ...

I'm trying to access a hana::type from a pair using hana::second...

namespace hana = boost::hana;
using namespace hana::literals;

struct Key {};
struct Foo {};

int main() {

  auto test = hana::make_tuple(
      hana::make_pair(
        hana::type_c<Key>, 
        hana::type_c<Foo>));

  typename decltype(hana::type_c<Foo>)::type  finalTest; //Ok
  typename decltype(hana::second(test[0_c]))::type finalTest2; //Error
}

但是我收到以下编译器错误:

But I am getting the following compiler error:

stacktest.cpp: In function ‘int main()’:
stacktest.cpp:17:12: error: decltype evaluates to ‘boost::hana::type_impl<Foo>::_&’, which is not a class or enumeration type
   typename decltype(hana::second(test[0_c]))::type finalTest2;

为什么hana::second的结果未按预期返回包含的hana::type?

Why does the result of hana::second not return the contained hana::type as expected?

推荐答案

错误消息指出decltype评估为boost::hana::type_impl<Foo>::_&,虽然看起来有些神秘,但最后可以通过&看到它是对包含的hana::type引用.不幸的是,引用将不包含您希望在原始类型中找到的成员.

The error message states that the decltype is evaluating to boost::hana::type_impl<Foo>::_&, which while a little cryptic looking, you can see by the & at the end that it is a reference to the contained hana::type. Unfortunately the reference will not contain the members that you expect to find in the raw type.

为此hana::type提供了一个简单地取消引用原始类型的一元operator+,因此您可以执行以下操作:

For this hana::type provides a unary operator+ that simply dereferences to the raw type so you can do the following:

typename decltype(+hana::second(test[0_c]))::type finalTest2;

hana::typeid_可以做到这一点,并且可以等价地将任何值包装在hana::type中,并去除const和引用限定符:

hana::typeid_ works for this as well as it idempotently wraps any value in a hana::type with const and reference qualifiers stripped:

typename decltype(hana::typeid_(hana::second(test[0_c])))::type finalTest2;

值得注意的是,以下所有Hana函数都返回引用:

It's worth noting that all of the following Hana functions return references:

firstsecondatat_key和关联的operator[].

这篇关于hana :: second无法推断类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆