title: Why boost-multi-index gives back a wrong iterator?
body: <p>Boost documents said that iterator_to gives back a valid iterator but the code below shows that something other happens. </p>
  
  <blockquote>
    <p>All indices of Boost.MultiIndex provide a member function called
    iterator_to which returns an iterator to a given element of the
    container</p>
  </blockquote>
  
  <p><a href="http://www.boost.org/doc/libs/1_51_0/libs/multi_index/doc/tutorial/indices.html#iterator_to" rel="nofollow">http://www.boost.org/doc/libs/1_51_0/libs/multi_index/doc/tutorial/indices.html#iterator_to</a></p>
  
  <pre><code>#include &lt;boost/multi_index_container.hpp&gt;
  #include &lt;boost/multi_index/member.hpp&gt;
  #include &lt;boost/multi_index/ordered_index.hpp&gt;
  #include &lt;boost/multi_index/random_access_index.hpp&gt;
  
  using boost::multi_index_container;
  using namespace boost::multi_index;
  
  struct Test
  {
    int id;
    std::string name;
  };
  
  struct idx{};
  struct id{};
  struct name{};
  
  typedef boost::multi_index_container&lt;
    Test*,
    indexed_by&lt;
      random_access&lt;tag&lt;idx&gt; &gt;,
      ordered_unique&lt;tag&lt;id&gt;, member&lt;Test, int, &amp;Test::id&gt; &gt;,
      ordered_unique&lt;tag&lt;name&gt;, member&lt;Test, std::string, &amp;Test::name&gt; &gt;
    &gt;
  &gt; TTestTable;
  
  class TestTable : public TTestTable
  {
    public:
      // Fill table with some values
      TestTable()
      {
        Test* test1 = new Test();
        Test* test2 = new Test();
        Test* test3 = new Test();
        Test* test4 = new Test();
        test1-&gt;id = 1;
        test2-&gt;id = 2;
        test3-&gt;id = 3;
        test4-&gt;id = 4;
        test1-&gt;name = "name1";
        test2-&gt;name = "name2";
        test3-&gt;name = "name3";
        test4-&gt;name = "name4";
  
        push_back(test1);
        push_back(test2);
        push_back(test3);
        std::cout &lt;&lt; at(0)-&gt;name &lt;&lt; std::endl;
        std::cout &lt;&lt; at(1)-&gt;name &lt;&lt; std::endl;
        std::cout &lt;&lt; at(2)-&gt;name &lt;&lt; std::endl;
  
        typedef TTestTable::index&lt;idx&gt;::type test_table_by_index;
        test_table_by_index::iterator it = get&lt;0&gt;().iterator_to(test1); // gives back a wrong iterator
  
        std::cout &lt;&lt; get&lt;idx&gt;().iterator_to(test1) - get&lt;idx&gt;().begin() &lt;&lt; "\n"; // WRONG
        replace(iterator_to(test1), test4); // CRASH
        replace(it, rule4); // CRASH
      }
  };
  
  int
  main(int argc, char **argv)
  {
    TestTable table;
  
     return 0;
  }
  </code></pre>