Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c++ - hana::second can't deduce type

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;

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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_ 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;

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

first, second, at, at_key, and associated operator[].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...