Mathieu Van Nevel
Mathieu Van Nevel

Reputation: 1486

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?

Upvotes: 4

Views: 219

Answers (1)

Jason Rice
Jason Rice

Reputation: 1696

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

Upvotes: 6

Related Questions