Boost logo

Boost :

Subject: [boost] [multi_index] scalar insert interface
From: Maxim Yegorushkin (maxim.yegorushkin_at_[hidden])
Date: 2010-11-05 12:59:16


Hey guys,

In ordered_index there are two scalar insert overloads:

   std::pair<iterator,bool> insert(const value_type& x);
   iterator insert(iterator position,const value_type& x);

The second version is lacking the bool result, which is sometimes
required. In my source code of boost-1_42 I patched the second version
like this:

==== .../ordered_index.hpp#2 (text) ====

@@ -97,6 +99,16 @@
  struct ordered_unique_tag{};
  struct ordered_non_unique_tag{};

+template<class T, class U>
+struct auto_cast_pair : std::pair<T, U>
+{
+ typedef std::pair<T, U> base_pair;
+ auto_cast_pair(base_pair const& b) : base_pair(b) {}
+ using base_pair::operator=;
+ operator T&() { return this->first; }
+ operator T const&() const { return this->first; }
+};
+
  template<
    typename KeyFromValue,typename Compare,
    typename SuperMeta,typename TagList,typename Category
@@ -274,14 +286,14 @@
      return std::pair<iterator,bool>(make_iterator(p.first),p.second);
    }

- iterator insert(iterator position,value_param_type x)
+ auto_cast_pair<iterator,bool> insert(iterator
position,value_param_type x)
    {
      BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
      BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
      BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
      std::pair<final_node_type*,bool> p=this->final_insert_(
        x,static_cast<final_node_type*>(position.get_node()));
- return make_iterator(p.first);
+ return std::pair<iterator,bool>(make_iterator(p.first),p.second);
    }

    template<typename InputIterator>

Now it automatically figures which return value one wants. Just an idea.

-- 
Max

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk