【发布时间】:2017-06-23 12:49:26
【问题描述】:
给定
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");
如果谓词的参数可转换为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 有什么问题?
【问题讨论】:
标签: c++ typetraits boost-mpl