提升MPL和_类型



我试图在代码中使用and_,但它的返回类型有问题。我正在尝试将它与其他接受并返回true_type或false_type的元编程构造一起使用,并且我还将SFINAE重载与这些类型一起使用。boost::mpl::and_和boost::mpl::or_返回与mpl其余部分不同的类型。至少,在我看来是这样的。

mpl_::failed************ boost::is_same<mpl_::bool_<true>, boost::integral_constant<bool, true> >::************)

那么,我该如何编写以下内容以使其通过断言呢?

template <typename T, typename U>
void foo(const T& test, const U& test2)
{
    typedef typename boost::mpl::and_<typename utilities::is_vector<T>, typename utilities::is_vector<U> >::type asdf;
    BOOST_MPL_ASSERT(( boost::is_same<asdf, boost::true_type::value> ));
}
void fooer()
{
    std::vector<int> test1;
    std::vector<int> test2;
    foo(test1, test2);
}

BOOST_MPL_ASSERT需要一个元函数谓词,即返回类型可以解释为"true"或"false"的元函数,也就是说,其返回类型例如为BOOST::MPL::true_或BOOST:。

正如定义的那样,类型"asdf"满足了这个需求,所以没有必要根据任何元编程抽象来明确检查它是否为"true",编写BOOST_MPL_ASSERT(( asdf ))正是您想要的。

当然,如果你愿意的话,你也可以明确地将其与"true"进行比较,但随后你必须将其与boost::mpl::true_进行比较,这与你所期望的boost::true_type的类型并不完全相同,因此会出现混乱!

只使用C++11:可能更容易

static_assert(asdf::value, "T and U aren't both vectors!");

static_assert(std::is_same<asdf, boost::true_>::value, "T and U aren't both vectors!");

最新更新