Boost :: mpl :: map failed boost :: mpl :: equal?

Boost MPL documentation says boost :: map :: equal

"Returns the true integral constant if the two sequences Seq1 and Seq2 are identical when comparing _element_ by _element_.

but it seems that the associative sequence map is not checked for element _wise _ equality:

The following demo will show this: Map2 should equal Map3, which both increment the value of 'int_ <1>' value_type by 'key'. Take a look at the typedef defining Map3. The size and the only item is reset in the demo:

#include<iostream> #include<boost/mpl/map.hpp> #include<boost/mpl/at.hpp> #include<boost/mpl/insert.hpp> #include<boost/mpl/erase_key.hpp> #include<boost/mpl/pair.hpp> #include<boost/mpl/int.hpp> #include<boost/mpl/plus.hpp> #include<boost/mpl/equal.hpp> #include<boost/mpl/size.hpp> #include<boost/mpl/front.hpp> namespace mpl = boost::mpl; using mpl::int_; using std::cout; using std::endl; using std::is_same; int main(int argc, char *argv[]) { typedef int key; typedef typename mpl::map<mpl::pair<key, int_<1>>> Map; typedef typename mpl::map<mpl::pair<key, int_<2>>> Map2; typedef typename mpl::insert< typename mpl::erase_key<Map, key>::type, mpl::pair<key, typename mpl::plus<int_<1>, typename mpl::at<Map, key>::type>::type >::type >::type Map3; cout << "equal? " << mpl::equal<Map2,Map3>::type::value << endl; cout << "size? " << mpl::size<Map3>::value << endl; cout << "key type at front? " << typeid(mpl::front<Map3>::type::first).name() << endl; cout << "value type at front? " << mpl::front<Map3>::type::second::value << endl; cout << "expected size? " << mpl::size<Map2>::value << endl; cout << "expected key type at front? " << typeid(mpl::front<Map2>::type::first).name() << endl; cout << "expected value type at front? " << mpl::front<Map2>::type::second::value << endl; return 0; } 

I am using gcc 4.8.1 with boost 1.51

+4
source share
2 answers

The reason it does not work as expected is because mpl::plus<int_<1>, mpl::at<Map, key>::type is mpl::integral_constant<int, 2> , then like mpl::int_<2> is a different type.

Working version:

 typedef mpl::map<mpl::pair<key, mpl::integral_c<int, 1>>> Map; typedef mpl::map<mpl::pair<key, mpl::integral_c<int, 2>>> Map2; typedef mpl::insert< mpl::erase_key<Map, key>::type, mpl::pair<key, mpl::plus<mpl::integral_c<int, 1>, mpl::at<Map, key>::type>::type> >::type Map3; 
+3
source

The result of inserting elements in a different order into the boost :: mpl :: map map is different types, even if the display is the same. To work around this problem, you can rebuild the sequence by sorting it every time you modify it. This greatly complicates the compiler you have to deal with, especially if there are more entries.

+2
source

Source: https://habr.com/ru/post/1497016/


All Articles