Section 5.6.  Sequence Equality
Team LiB
Previous Section Next Section

5.6. Sequence Equality

It's important, particularly when handling computed results, not to fall into the trap of relying on sequence type identity. For example, you should not expect the following assertion to pass:



    BOOST_STATIC_ASSERT((      // error


      boost::is_same<


          mpl::pop_back<mpl::vector<int, short> >::type


        , mpl::vector<int>


      >::value


    ));



For most purposes, the two types being compared above will act the same, and most of the time you'll never notice a difference. That said, the result of using mpl::pop_back on a specialization of mpl::vector will not be another specialization of mpl::vector!

As you saw in our exploration of dimensional analysis in Chapter 3, a function template that can only be called with two identical types is likely not to work as expected if those types are sequences. The same goes for a class template partial specialization that matches only when two type arguments are identical.

The correct way to check for sequence equality is always to use the equal algorithm, as follows:



    BOOST_STATIC_ASSERT((        // OK


      mpl::equal<


          mpl::pop_back<mpl::vector<int, short> >::type


        , mpl::vector<int>


      >::value


    ));



    Team LiB
    Previous Section Next Section