Search code examples
c++boostboost-mp11

Failing trying to define a Boost::mp11::find_if predicate


I've been writing some policy/trait-based designs, and I came across a problem with Boost::mp11 that I don't understand. I have reduced the problem to following example:

#include <iostream>
#include <variant>
#include <tuple>

#include <boost/hana/string.hpp>
#include <boost/mp11/algorithm.hpp>

using namespace boost::hana::literals;

struct stub_trait1
{
    static constexpr auto name = "stub_trait1"_s;
};

struct stub_trait2
{
    static constexpr auto name = "stub_trait2"_s;
};

struct test_card
{
    using traits_type = std::tuple<stub_trait1, stub_trait2>;

    explicit test_card() = default;
    traits_type traits;
};

using cards_t = std::variant<test_card>;

template <typename TraitName>
struct trait_name_predicate
{
    template <typename Trait>
    struct result : std::is_same<decltype(Trait::name), TraitName> {};
};

template <typename TraitName, typename Callable>
void generic_trait_processor(const cards_t& card, const Callable& f)
{
    std::visit([&](auto&& c) {
        using traits_type = typename std::decay_t<decltype(c)>::traits_type;
        using sr_idx = boost::mp11::mp_find_if<traits_type, trait_name_predicate<TraitName>::result>; // Fail!
        if constexpr (sr_idx::value != std::tuple_size_v<traits_type>) {
            auto& trait = std::get<sr_idx::value>(c.traits);
            f(c, trait);
        }
    },
    card);
}

int main()
{
    auto c = cards_t{test_card{}};
    generic_trait_processor<decltype("stub_trait1"_s)>(
        c,
        [](auto&& card, auto&& trait) {
            std::cout << "Trait: " << trait.name << std::endl;
        });

    return EXIT_SUCCESS;
}

You can (attempt) to run the code here. The compiler complains about the boost::mp11::find_if type:

<source>: In lambda function:

<source>:42:100: error: type/value mismatch at argument 2 in template parameter list for 'template<class L, template<class ...> class P> using mp_find_if = typename boost::mp11::detail::mp_find_if_impl::type'

         using sr_idx = boost::mp11::mp_find_if<traits_type, trait_name_predicate<TraitName>::result>;

                                                                                                    ^

<source>:42:100: note:   expected a class template, got 'trait_name_predicate<TraitName>::result'

Neither of these errors make sense, argument 2 is a type and trait_name_predicate<TraitName>::result is a class template!

The compiler and I are confusing each other - can anyone see what I'm doing wrong?


Solution

  • I needed to tell the compiler that the dependent name result is a template:

    using sr_idx = boost::mp11::mp_find_if<traits, trait_name_predicate<TraitName>::template result>;
                                                                                    ^^^^