Question

I'm trying to use and_ in my code, and I'm having an issue with it's return type. I'm trying to use it with other metaprogramming constructs that accept and return true_type or false_type, and I use SFINAE overloads with those types also. boost::mpl::and_ and boost::mpl::or_ return different types from much of the rest of the mpl. At least, it appears this way to me.

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

So, how do I write the following so that it passes the assert?

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);
}
Was it helpful?

Solution

BOOST_MPL_ASSERT expects a metafunction predicate, i.e. a metafunction whose return type can be interpreted either as "true" or "false", that is, whose return type is for example either boost::mpl::true_ or boost::mpl::false.

as defined, the type "asdf" attends to this requirement, so there's no need to explicitly check it against any metaprograming abstraction for "true", writing BOOST_MPL_ASSERT(( asdf )) does exactly what you want.

ofcourse you could also compare it explicitly to "true", if you wanted, but then you had to compare it to boost::mpl::true_, which is not exactly the same type as boost::true_type as you might expect, hence the confusion!

OTHER TIPS

Might be easier to just use C++11:

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

Or

static_assert(std::is_same<asdf, boost::true_>::value, "T and U aren't both vectors!");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top