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

Categories

0 votes
1.2k 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 Answer

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[].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...