Search code examples
c++boostboost-mpl

In boost MPL, how do I check if an operation worked as intended?


Normally when I write code, I frequently check that what I am doing works but using some sort of assertion operation:

std::vector<int> a(1, 1);
std::vector<int> b = {1};
assert(a == b); // this either works, or breaks in a helpful manner

How do I achieve this in boost mpl? I'm currently trying to generate a vector of pairs from 2 vectors, where the first vector represents the keys and the 2nd the values (ie. types):

using Keys = boost::mpl::vector<double, bool, int, char, bool*>;
using Types = boost::mpl::vector<char, char, int, int, int>;

using ExpectedOutput =                                                   
    boost::mpl::vector<                                                         
        boost::mpl::pair<double, char>,                                         
        boost::mpl::pair<bool, char>,                                           
        boost::mpl::pair<int, char>,                                            
        boost::mpl::pair<char, int>,                                            
        boost::mpl::pair<bool*, int>>;

// now I perform some operation which I _think_ might work
using PairsSequence =                                                           
    typename boost::mpl::transform<                                             
        Keys,                                                            
        Types,
        boost::mpl::pair<boost::mpl::_1, boost::mpl::_2>>;

// Now I attempt to check that it worked
BOOST_MPL_ASSERT(( boost::mpl::equal_to<PairsSequence, ExpectedPairsSequence> ));

But this doesn't work, presumably because the return type of boost::mpl::transform is some template expression. How can I force conversion of this output into a type that I can compare to an expected value?

How do others debug their MPL code? Boost doesn't seem to provide any facilities for checking the output of operations (or at least I don't know how to use them, BOOST_MPL_ASSERT being a case in point).


Solution

    1. equal_to models the Numeric Metafunction concept. You would want to use equal.
    2. you will want to apply the metafunction before comparing, I added ::type in the assert
    3. the expected type didn't actually match, so it would fail unless you match them.

    See it Live On Coliru

    #include <boost/mpl/vector.hpp>
    #include <boost/mpl/pair.hpp>
    #include <boost/mpl/transform.hpp>
    #include <boost/mpl/assert.hpp>
    #include <boost/mpl/equal.hpp>
    
    
    using Keys = boost::mpl::vector<double, bool, int, char, bool*>;
    using Types = boost::mpl::vector<char, char, int, int, int>;
    
    using ExpectedOutput =                                                   
        boost::mpl::vector<                                                         
            boost::mpl::pair<double, char>,                                         
            boost::mpl::pair<bool, char>,                                           
            boost::mpl::pair<int, int>,                                            
            boost::mpl::pair<char, int>,                                            
            boost::mpl::pair<bool*, int>>;
    
    // now I perform some operation which I _think_ might work
    using PairsSequence =                                                           
        typename boost::mpl::transform<                                             
            Keys,                                                            
            Types,
            boost::mpl::pair<boost::mpl::_1, boost::mpl::_2>>;
    
    BOOST_MPL_ASSERT(( boost::mpl::equal<PairsSequence::type, ExpectedOutput> ));