title: Could an optimizing compiler remove all runtime costs from std::unique_ptr?
body: <p>Reading about <code>std::unique_ptr</code> at <a href="http://en.cppreference.com/w/cpp/memory/unique_ptr" rel="noreferrer">http://en.cppreference.com/w/cpp/memory/unique_ptr</a>, my naive impression is that a smart enough compiler could replace correct uses of <code>unique_ptr</code> with bare pointers and just put in a <code>delete</code> when the <code>unique_ptr</code>s get destroyed. Is this actually the case? If so, do any of the mainstream optimizing compilers actually do this? If not, would it be possible to write something with some/all of <code>unique_ptr</code>s compile-time safety benefits that could be optimized to have no runtime cost (in space or time)?</p>
  
  <p>Note to those (properly) worried about premature optimization: The answer here won't stop me from using <code>std::unique_ptr</code>, I'm just curious if it's a really awesome tool or just an awesome one.</p>
  
  <p><strong>EDIT 2013/07/21 20:07 EST:</strong></p>
  
  <p>OK, so I tested with the following program (please let me know if there's something wrong with this):</p>
  
  <pre><code>#include &lt;climits&gt;
  #include &lt;chrono&gt;
  #include &lt;memory&gt;
  #include &lt;iostream&gt;
  
  static const size_t iterations = 100;
  
  int main (int argc, char ** argv) {
      std::chrono::steady_clock::rep smart[iterations];
      std::chrono::steady_clock::rep dumb[iterations];
      volatile int contents;
      for (size_t i = 0; i &lt; iterations; i++) {
          auto start = std::chrono::steady_clock::now();
          {
              std::unique_ptr&lt;int&gt; smart_ptr(new int(5));
              for (unsigned int j = 0; j &lt; UINT_MAX; j++)
                  contents = *smart_ptr;
          }
          auto middle = std::chrono::steady_clock::now();
          {
              int *dumb_ptr = new int(10);
              try {
                  for (unsigned int j = 0; j &lt; UINT_MAX; j++)
                      contents = *dumb_ptr;
                  delete dumb_ptr;
              } catch (...) {
                  delete dumb_ptr;
                  throw;
              }
          }
          auto end = std::chrono::steady_clock::now();
          smart[i] = (middle - start).count();
          dumb[i] = (end - middle).count();
      }
      std::chrono::steady_clock::rep smartAvg;
      std::chrono::steady_clock::rep dumbAvg;
      for (size_t i = 0; i &lt; iterations; i++) {
          smartAvg += smart[i];
          dumbAvg += dumb[i];
      }
      smartAvg /= iterations;
      dumbAvg /= iterations;
  
      std::cerr &lt;&lt; "Smart: " &lt;&lt; smartAvg &lt;&lt; " Dumb: " &lt;&lt; dumbAvg &lt;&lt; std::endl;
      return contents;
  }
  </code></pre>
  
  <p>Compiling with g++ 4.7.3 using <code>g++ --std=c++11 -O3 test.cc</code> gave <code>Smart: 1130859 Dumb: 1130005</code>, which means the smart pointer is within 0.076% of the dumb pointer, which is almost surely noise.</p>