Search code examples
c++boostboost-hana

boost hana index_if and type


I wonder why, in this code, the type of i is an empty optional.

auto t = boost::hana::make_tuple(boost::hana::type_c<int>, boost::hana::type_c<double>);
auto i = boost::hana::index_if(t, boost::hana::is_a<boost::hana::type<double>>);

To me, it should be optional<hana::size_t<1>>

I know there is Boost hana get index of first matching but it is not exactly the same question


Solution

  • boost::hana::is_a returns whether the tag of an object matches a given tag. [reference]

    You're not passing it a tag, you're passing it a hana::type instead.

    For example, you could test whether the argument is a hana::type, and i would contain a size_c<0> (because the first item in the tuple is already a hana::type):

    auto i = hana::index_if(t, hana::is_a<hana::type_tag>);
    

    If you want to check for equality to some type, use equal::to:

    auto i = hana::index_if(t, hana::equal.to(hana::type_c<double>));
    

    [Reference to hana::equal]

    Live example