Opened 8 years ago

#11001 new Bugs

insert_range lacks support for extensible associative sequences

Reported by: brunocodutra@… Owned by: Aleksey Gurtovoy
Milestone: To Be Determined Component: mpl
Version: Boost 1.57.0 Severity: Problem
Keywords: insert_range, map, associative Cc:

Description

According to reference, the insert_range should work for any Extensible sequence or Extensible Associative sequence, but the following fails to compile.

#include <boost/mpl/pair.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/insert_range.hpp>
#include <boost/mpl/end.hpp>
#include <boost/mpl/equal.hpp>

typedef boost::mpl::map<boost::mpl::pair<int, int>, boost::mpl::pair<void, void> > myMap;
typedef boost::mpl::insert_range<boost::mpl::map<>, boost::mpl::end<boost::mpl::map<> >::type, myMap>::type copyOfMyMap;
BOOST_MPL_ASSERT((boost::mpl::equal<myMap, copyOfMyMap>));

int main()
{
    return 0;
}

I tracked the issue down to the definition of insert_range_impl, whose default implementation assumes a front_inserter is defined for the given sequence, but neither Extensible nor every Extensible Associative sequences are required to also be a Front Extensible sequence:

reverse_copy<
    joint_view< 
        iterator_range<typename begin<Sequence>::type,Pos>
    , joint_view< 
            Range
        , iterator_range<Pos,typename end<Sequence>::type>
        >
    >
, front_inserter< typename clear<Sequence>::type >
>

I found the fix to be implementing insert_range_impl by means of insert, which is defined for every Extensible sequence, using fold to iterate:

fold<
    joint_view< 
        iterator_range<typename begin<Sequence>::type,Pos>
    , joint_view< 
            Range
        , iterator_range<Pos,typename end<Sequence>::type>
        >
    >
, typename clear<Sequence>::type
, insert<_1, end<_1>, _2>
>

I'll file a pull request for this fix, any comments are welcome.

Change History (0)

Note: See TracTickets for help on using tickets.