Search code examples
c++templatesboostboost-mpl

c++ recursive mpl::equal problem?


i need an mpl::equal like procedure that supports recursion on types.

namespace mpl = boost::mpl;

BOOST_MPL_ASSERT(( mpl::equal< 
 mpl::vector<int, char>, 
 typename mpl::push_back<mpl::vector<int>, char>::type > )); // OK

the above compiles fine, however if i use it in mpl::transform or mpl::fold, visual studio 2010 rc1 complains.

typedef mpl::vector<
 mpl::vector<int, char>,
 mpl::vector<char, char>> type_1;
typedef mpl::transform<
 mpl::vector<
  mpl::vector<int>,
  mpl::vector<char>>,
 mpl::push_back<mpl::_, char>>::type type_2;
BOOST_MPL_ASSERT(( mpl::equal<type_1, type_2> )); // FAILS

however, these work...

BOOST_MPL_ASSERT(( mpl::equal<
    typename mpl::at_c<type_1, 0>::type, 
    typename mpl::at_c<type_2, 0>::type> )); // OK
BOOST_MPL_ASSERT(( mpl::equal<
    typename mpl::at_c<type_1, 1>::type, 
    typename mpl::at_c<type_2, 1>::type> )); // OK

is it that mpl::equal does not work on dynamically generated recursive types, or is there something wrong with my syntax?

any advice would greatly appreciated.


Solution

  • mpl::transform doesn't create mpl::vector<>'s in your case but mpl::vector2<>'s. These are different types, even if they are semantically equivalent. So if you write:

    typedef mpl::vector2< 
        mpl::vector2<int, char>, mpl::vector2<char, char> 
    > type_1;
    
    typedef mpl::transform< 
        mpl::vector<mpl::vector<int>, mpl::vector<char> >
      , mpl::push_back<mpl::_, char> 
    >::type type_2; 
    
    BOOST_MPL_ASSERT(( mpl::equal<type_1, type_2> )); 
    

    the assert will not fire.