将 boost::mpl::find_if 与自定义谓词一起使用

Using boost::mpl::find_if with custom predicate

给出

struct A {};
struct B {};
struct C {C(B) {}};
struct D : B {};
struct E {operator A() const {return A();}};
struct F {};

using AllowedTypes = boost::mpl::vector<A, C>;

我正在尝试实现一个谓词 is_allowed_type 以便

static_assert(is_allowed_type<A>::value, "A is in AllowedTypes");
static_assert(is_allowed_type<B>::value, "C is constructible from B");
static_assert(is_allowed_type<C>::value, "C is in AllowedTypes");
static_assert(is_allowed_type<D>::value, "D inherits from B, from which C is constructible");
static_assert(is_allowed_type<E>::value, "E is convertible to A");
static_assert(!is_allowed_type<F>::value, "F is not convertible to A nor C");

谓词必须 return 为真当且仅当其参数可转换为 AllowedTypes 中的一种类型。

这是我想出的。

template <typename T>
struct is_allowed_type
{
    using I = boost::mpl::find_if<AllowedTypes, std::is_convertible<T, boost::mpl::_>>;
    using End = boost::mpl::end<AllowedTypes>;
    enum {value = !std::is_same<I, End>::value};
};

最后一个断言失败。我的 is_allowed_type 怎么了?

我在写问题时找到了答案,但由于我很难找到 material 关于这个主题的答案,所以我发布了它。

缺少 is_allowed_type 中的迭代器定义 ::type。正确的实现是

template <typename T>
struct is_allowed_type
{
    using I = typename boost::mpl::find_if<AllowedTypes, std::is_convertible<T, boost::mpl::_>>::type;
    using End = typename boost::mpl::end<AllowedTypes>::type;
    enum {value = !std::is_same<I, End>::value};
};