gpt4 book ai didi

c++ - hana::second 无法推断类型

转载 作者:太空狗 更新时间:2023-10-29 20:21:32 25 4
gpt4 key购买 nike

我正在尝试使用 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

最佳答案

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

为此hana::type提供一元 operator+这只是取消对原始类型的引用,因此您可以执行以下操作:

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

hana::typeid_为此工作以及它幂等地将任何值包装在 hana::type 中删除了 const 和引用限定符:

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

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

first , second , at , at_key , 以及相关的 operator[] .

关于c++ - hana::second 无法推断类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43618052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com