首页 > 解决方案 > 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

标签: c++boostboost-hana

解决方案


boost::hana::is_a返回对象的标签是否与给定的标签匹配。[参考]

您没有将标签传递给它,hana::type而是将其传递给它。

例如,您可以测试参数是否为 a hana::type,并且i是否包含 a size_c<0>(因为元组中的第一项已经是 a hana::type):

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

如果要检查某种类型是否相等,请使用equal::to

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

[参考 hana::equal]

活生生的例子


推荐阅读