4

I am trying to use boost::assign to emulate C++11 initialization of a std::map containing a std::set.

#include <set>
#include <map>
#include <stdint.h>

#include <boost/assign/list_of.hpp>

typedef std::map< uint32_t, std::set< uint32_t> > the_map_t;

the_map_t data = boost::assign::map_list_of( 1, boost::assign::list_of(10)(20)(30) )
                                           ( 2, boost::assign::list_of(12)(22)(32) )
                                           ( 3, boost::assign::list_of(13)(23)(33) )
                                           ( 4, boost::assign::list_of(14)(24)(34) );

Initialisation of std::set using boost::assign::list_of works as expected when used on its own, but when I try the above code the assignment is ambiguous at the point where the std::set's constructor is called:

map-assign.cpp:16:   instantiated from here
include/c++/4.4.6/bits/stl_pair.h:101: error: call of overloaded set(const   boost::assign_detail::generic_list<int>&) is ambiguous
include/c++/4.4.6/bits/stl_set.h:188: note: candidates are: 
    std::set<_Key, _Compare, _Alloc>::set(
        const std::set<_Key, _Compare, _Alloc>&) 
        [with _Key = unsigned int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]

include/c++/4.4.6/bits/stl_set.h:145: note:                 
    std::set<_Key, _Compare, _Alloc>::set(
        const _Compare&, const _Alloc&) 
        [with _Key = unsigned int, _Compare = std::less<unsigned int>, _Alloc = std::allocator<unsigned int>]

How can I resolve this ambiguity error?

mark
  • 7,381
  • 5
  • 36
  • 61

1 Answers1

3

In this case boost::assign::map_list_of needs a hint for second template argument - <uint32_t, std::set< uint32_t> >. Therefore line

the_map_t data = boost::assign::map_list_of(...);

becomes

the_map_t data = boost::assign::map_list_of<uint32_t, std::set< uint32_t> >(...);
Greg
  • 1,660
  • 13
  • 12
  • 1
    cheers that works but I believe have uncovered a bug within `boost::assign`, see: http://boost.2283326.n4.nabble.com/Typedef-rejected-when-disambiguating-a-call-to-boost-assign-td4637775.html. – mark Oct 30 '12 at 11:42