Boost logo

Boost Users :

From: Markus Werle (numerical.simulation_at_[hidden])
Date: 2007-02-02 04:55:10


> Hi!

I tried to use multi_index_container as a drop in replacement
for std::map and hoped that I could obtain non-const references
to the values therein.

Although returning a non-const reference might not be a problem for
a multi_index_container with only 1 sorting criterium the lib
and the compiler insist on const &. Workarounds beyond modify(...) available?

Markus

#include <boost/tuple/tuple.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/ordered_index.hpp>

#include <string>

namespace bm = ::boost::multi_index;

// http://lists.boost.org/boost-users/2005/12/16088.php
template<typename Tuple,int N>
struct tuple_member_extractor
{
        typedef typename
        boost::tuples::element<N,Tuple>::type result_type;

        const result_type& operator()(const Tuple& t) const
        {
                return boost::tuples::get<N>(t);
        }
};

typedef boost::tuple<std::string, std::string> element;

typedef bm::multi_index_container<
        element,
        bm::indexed_by<bm::ordered_unique
        <tuple_member_extractor<element, 0> >
> > multi_t;

int main()
{
        multi_t test;

        test.insert(boost::make_tuple("a", "aa"));

        // will not compile
        element & ref = test.find("a");
        
        std::string & s = ref.get<1>();
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net