Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2003-03-18 07:44:43


Jaap Suter wrote:
> Hi,

Hi Jaap,

>
> In some of my MPL-using code I needed set-based functionality. So I wrote
a
> function that does an insertion into an ordered list of constants.
However,
> it seems that if I compare a list created from a bunch of constants to an
> explicit list, they don't end up the same. I've attached the example code
> below.

[snip]

> template < class N, class L >
> struct add_to_sorted_list
> {
> typedef typename mpl::if_<
> typename mpl::contains< L, N >::type,
> L,
> typename mpl::insert< L,
> typename mpl::lower_bound< L,
> N,
> mpl::less< mpl::_, mpl::_ >
> >::type,
> N
> >::type
> >::type type;
> };
>
> typedef mpl::list_c< int, 0, 1, 2, 3 > list0;
> typedef mpl::list_c< int, 0, 1, 3 > list1;
> typedef add_to_sorted_list<
> mpl::integral_c< int, 2 >,
> list1,
> >::type result;
>
> BOOST_STATIC_ASSERT( is_same< list0, result >::value ); // THIS FAILS;

The lists _are_ the same, content-wise, and that's what you actually should
be checking for:

BOOST_STATIC_ASSERT(( mpl::equal< list0, result >::type::value ));

Test for two sequences having the same C++ type isn't guaranteed to work, as
any manipulation of sequence content changes the sequence's type, and often
it's not worthy to maintain invariants along the lines of this one:

    typedef list_c<int,0> l0;
    typedef list_c<int,1,0> l1;
    typedef push_front<l0, int_<1> >::type res;
    BOOST_STATIC_ASSERT((is_same<res,l1>::value)); // most probably fails

If an analogy can help here, in some sense comparing for type equality
instead of content equality is somewhat similar to writing

    std::vector<int> v1;
    std::vector<int> v2;

    assert(&v1 == &v2);

instead of

    assert(v1 == v2);

HTH,
Aleksey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk