Mat*_*vel 3 c++ metaprogramming boost-hana
我正在尝试hana::type使用一对来访问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
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下编译器错误:
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;
Run Code Online (Sandbox Code Playgroud)
为什么不按预期hana::second返回包含的结果hana::type?
错误消息指出decltype正在评估boost::hana::type_impl<Foo>::_&,虽然看起来有点神秘,但你可以看到&最后它是对包含的引用hana::type.不幸的是,引用不包含您希望在原始类型中找到的成员.
为此hana::type提供了一个operator+简单的解除引用原始类型的一元,因此您可以执行以下操作:
typename decltype(+hana::second(test[0_c]))::type finalTest2;
Run Code Online (Sandbox Code Playgroud)
hana::typeid_适用于此,以及它在hana::type使用剥离的const和引用限定符时,以幂等方式包装任何值:
typename decltype(hana::typeid_(hana::second(test[0_c])))::type finalTest2;
Run Code Online (Sandbox Code Playgroud)
值得注意的是,以下所有Hana函数都返回引用:
first,second,at,at_key,和相关的operator[].