1

我一直在编写一些基于策略/特征的设计,但遇到了一个我不理解的 Boost::mp11 问题。我已将问题简化为以下示例:

#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;
}

您可以(尝试)在此处运行代码。编译器抱怨boost::mp11::find_if类型:

<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'

这些错误都没有意义,参数 2一个类型并且 trait_name_predicate<TraitName>::result 一个类模板!

编译器和我互相混淆 - 谁能看到我做错了什么?

4

1 回答 1

1

我需要告诉编译器依赖名称result是一个模板:

using sr_idx = boost::mp11::mp_find_if<traits, trait_name_predicate<TraitName>::template result>;
                                                                                ^^^^
于 2018-07-24T08:04:34.153 回答