title: Why can't `boost::multi_index replace` be used for pointer types?
body: <p><strong>Short answer: User <code>modify</code> instead, details in the accepted answer and also <a href="https://stackoverflow.com/a/22777883/424153">this answer</a></strong></p>
  
  <p>I'm attempting to use a <code>boost::multi_index_container</code> holding a pointer type. It appears to me the <code>replace</code> function is broken, and am wondering what I am doing wrong.</p>
  
  <p>The following code demonstrates two cases: first a container holding a copy of the data (which works correctly) and secondly a container holding a pointer to the data (which fails).</p>
  
  <pre><code>using namespace boost::multi_index;
  using boost::multi_index_container;
  
  struct Data
  {
      int key1;
      int key2;
  };
  
  using DataContainer =
      multi_index_container&lt;
          Data,
          indexed_by&lt;
              hashed_unique&lt;member&lt;Data, int, &amp;Data::key1&gt;&gt;,
              hashed_unique&lt;member&lt;Data, int, &amp;Data::key2&gt;&gt;&gt;&gt;;
  
  using DataPtrContainer = 
      multi_index_container&lt;
          Data*,
          indexed_by&lt;
              hashed_unique&lt;member&lt;Data, int, &amp;Data::key1&gt;&gt;,
              hashed_unique&lt;member&lt;Data, int, &amp;Data::key2&gt;&gt;&gt;&gt;;
  
  TEST(DummyTest, Test1)
  {
      Data data{1,2};
      DataContainer dataContainer;
      dataContainer.insert(data);
  
      EXPECT_EQ(1, dataContainer.get&lt;0&gt;().find(1)-&gt;key1);
      EXPECT_EQ(2, dataContainer.get&lt;0&gt;().find(1)-&gt;key2);
  
      auto iter = dataContainer.get&lt;0&gt;().find(1);
      Data d = *iter;
      d.key2 = 5;
      dataContainer.replace(iter, d);
  
      EXPECT_EQ(1, dataContainer.get&lt;1&gt;().find(5)-&gt;key1);
      EXPECT_EQ(5, dataContainer.get&lt;1&gt;().find(5)-&gt;key2);
  
  }
  
  TEST(DummyTest, Test2)
  {
      Data* data = new Data{1,2};
      DataPtrContainer dataContainer;
      dataContainer.insert(data);
  
      EXPECT_EQ(1, (*dataContainer.get&lt;0&gt;().find(1))-&gt;key1);
      EXPECT_EQ(2, (*dataContainer.get&lt;0&gt;().find(1))-&gt;key2);
  
      auto iter = dataContainer.get&lt;0&gt;().find(1);
      Data* d = *iter;
      d-&gt;key2 = 5;
      dataContainer.replace(iter, d);
  
      EXPECT_EQ(1, (*dataContainer.get&lt;1&gt;().find(5))-&gt;key1);  // fail as the iterator not dereferencable
      EXPECT_EQ(5, (*dataContainer.get&lt;1&gt;().find(5))-&gt;key2); // fail as the iterator not dereferencable
  
  }
  </code></pre>