花::秒不能推断类型

hana::second can't deduce type

本文关键字:类型 不能      更新时间:2023-10-16

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

从一对访问hana::type
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
}

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

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

错误消息指出,声明类型正在评估 boost::hana::type_impl<Foo>::_&,虽然看起来有些隐秘,但您可以在 &中看到它是 referita>到包含的hana::type。不幸的是,该参考将不包含您期望在原始类型中找到的成员。

对于此 hana::type提供了一个单一的operator+,它简单地将原始类型放置,因此您可以执行以下操作:

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

hana::typeid_为此工作,并以const和参考限定符剥夺了hana::type中的任何值:

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

值得注意的是,以下所有HANA功能返回参考:

firstsecondatat_key和关联的operator[]