Stereo Stereo - 2 months ago 26
C++ Question

OPERATOR_BRACKET_IS_NOT_SUPPORTED on boost::bimap

I am trying to use the bracket operator on a

boost::bimap
but without success.

For the problem I seek to solve I need a
bimap
that fulfills the following requirement:


  • left sorted, unique
    int

  • right non unique, non sorted type



This lead me to choose the following
typedef
for my
bimap
,

typedef bimap<int, multiset_of<int> > bm;


I want to use the bracket operator on this type but without success. This is the code that I use,

#include <iostream>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>

using namespace std;
using namespace boost::bimaps;

int main()
{
typedef bimap<int, multiset_of<int> > bm;
bm mapping;

mapping.insert(bm::value_type(1, 1));
mapping.insert(bm::value_type(2, 1));
mapping.insert(bm::value_type(3, 4));

for (auto it : {1 , 2, 3})
mapping.left[it] = it;

for (auto it : mapping.left)
cout << "{ " << it.first << ", " << it.second << " } ";

return 0;
}


This gives me a long compile error where the essential bit seems to be

/usr/include/boost/bimap/detail/map_view_base.hpp:351:9: error: no matching function for call to 'assertion_failed'
BOOST_BIMAP_STATIC_ERROR( OPERATOR_BRACKET_IS_NOT_SUPPORTED, (Derived));


(full live live example and compiler output can be found on: rextester)

I tried the solution below but it still yields errors,

#include <iostream>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>

using namespace std;
using namespace boost::bimaps;

int main()
{
typedef bimap<int, multiset_of<int> > bm;
typedef bm::left_map map_type;
bm mapping;

map_type& m = mapping.left;

mapping.insert(bm::value_type(1, 1));
mapping.insert(bm::value_type(2, 1));
mapping.insert(bm::value_type(3, 4));

for (auto it : {1 , 2, 3})
m[it] = it;

for (auto it : mapping.left)
cout << "{ " << it.first << ", " << it.second << " } ";

return 0;
}


How can I declare a
bimap
that fulfills my requirements and supports the bracket operator?

Answer

As always, we refer to the documentation:

set_of and unordered_set_of map views overload operator[] to retrieve the associated data of a given key only when the other collection type is a mutable one. In these cases it works in the same way as the standard.

You are using a non-mutable type on the right-hand-side:

Side collection type       Dereferenced data
--------------------------------------------
set_of                     constant
multiset_of                constant
unordered_set_of           constant
unordered_multiset_of      constant
list_of                    mutable
vector_of                  mutable
unconstrained_set_of       mutable

So, you cannot use operator[].

The error message says as much:

OPERATOR_BRACKET_IS_NOT_SUPPORTED
Comments