简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM