views:

174

answers:

1

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.

+2  A: 

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.

hkaiser
How things are going to get simpler for metaprogramming with the upcoming `C++0x`... congratz for finding it out, I suppose you actually tried to compile it ?
Matthieu M.
Well, I ran into this problem before. But regardless, you can always use mpl::print<T> to force the compiler to generate a warning which lists the actual type of T.
hkaiser
thanks! i never knew about mpl::print<T>, is nowhere in the docs. it's a real lifesaver.
kyunghoon