title: Refactoring with C++ 11
body: <p>Given the new toolset provided by c++ lots of programmers, aiming at code simplification, expressiveness, efficiency, skim through their old code and make tweaks (some pointless, some successful) to achieve their goals. Whilst trying not to loose too much time on such labors and just make non intrusive and self contained changes, what are the best practices? </p>
  
  <p>Let me cross out the obvious :</p>
  
  <ul>
  <li><p>Use <strong>auto</strong> to run iterator based loops :</p>
  
  <pre><code>for (std::vector&lt;foo&gt;::const_iterator it(lala.begin()), ite(lala.end()); it != ite;     
  ++it);
  // becomes
  for (auto it(lala.cbegin()), ite(lala.cend()); it != ite; ++it);
  </code></pre></li>
  <li><p>Use <strong>tie</strong> for multiple assignments that just produce C-style rows of code ( <a href="https://stackoverflow.com/questions/9269825/how-to-assign-multiple-values-into-a-struct-at-once/21513616#21513616">how to assign multiple values into a struct at once?</a> )</p>
  
  <pre><code>a = 1;
  b = 2; 
  c = 3;
  d = 4; 
  e = 5;
  // becomes
  std::tie(a, b, c, d, e) = std::make_tuple(1, 2, 3, 4, 5);
  </code></pre></li>
  <li><p>To make a class non inheritable just declare it as "final" and delete the code that achieved such a behavior <a href="http://www.parashift.com/c++-faq/final-classes.html" rel="noreferrer">http://www.parashift.com/c++-faq/final-classes.html</a> </p></li>
  <li><p>Use the delete keyword to explicitly hide constructors/destructors instead of declaring them private (eg code to create heap based objects, non copyable objects etc)</p></li>
  <li><p>Turn trivial functors created just to facillitate the execution of a single STL algorithm into <strong>lambda</strong> functions (apart from reducing code cluttering you'll have guaranteed inlined calls)</p></li>
  <li><p>Simplify RAII wrapping of an object by just using a <strong>smart pointer</strong></p></li>
  <li><p>Get rid of bind1st, bind2nd and just use <strong>bind</strong></p></li>
  <li><p>Replace hand written code for type traits (Is_ptr_but_dont_call_for_const_ptrs&lt;> and such :) ) with standard code provided by <strong>&lt; type_traits ></strong></p></li>
  <li><p>Stop including boost headers for functionallity now implented in STL (BOOST_STATIC_ASSERT vs static_assert) </p></li>
  <li><p>Provide move semantics to classes (although this wouldn't qualify as a dirty/quick/easy change)</p></li>
  <li><p>Use <strong>nullptr</strong> where possible instead of the NULL macro and get rid of the code that filled containers of pointers with 0's casted to object type</p>
  
  <pre><code>std::vector&lt;foo*&gt; f(23);
  for (std::size_t i(0); i &lt; 23; ++i)
  { f[i] = static_cast&lt;foo*&gt;(0); }
  // becomes
  std::vector&lt;foo*&gt; f(23, nullptr);
  </code></pre></li>
  <li><p>Clear the vector data accessing syntax</p>
  
  <pre><code>std::vector&lt;int&gt; vec;
  &amp;vec[0];    // access data as a C-style array
  vec.data(); // new way of saying the above
  </code></pre></li>
  <li><p>Replace throw() with <strong>noexcept</strong> (apart from avoiding the deprecated exception specifiation you get some speed benefits <a href="http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler" rel="noreferrer">http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler</a> @ 00.29.42) </p>
  
  <pre><code>void some_func() noexcept; // more  optimization options
  void some_func() throw();  // fewer optimization options
  void some_func() ;         // fewer optimization options
  </code></pre></li>
  <li><p>Replace code where you'd push a tempory in a container and hoped that the optimizer would ellide the copy away, with an <strong>"emplace"</strong> function where available, in order to perfectly forward the argument and construct directly an object into a container without temporary at all.</p>
  
  <pre><code>vecOfPoints.push_back(Point(x,y,z)); // so '03
  vecOfPoints.emplace_back(x, y, z);   // no copy or move operations performed
  </code></pre></li>
  </ul>
  
  <h2>UPDATE</h2>
  
  <p><a href="https://stackoverflow.com/a/21519186/2567683">The answer by Shafik Yaghmour</a> was rightfully awarded the bounty for having the greatest acceptance by the audience. </p>
  
  <p><a href="https://stackoverflow.com/a/22802574/2567683">The answer by R Sahu</a> was my accepted one, because the <strong>combination</strong> of features it proposes captures the <a href="http://c2.com/cgi/wiki?WhatIsRefactoring" rel="noreferrer"><strong>spirit of refactoring</strong></a> :  making code clearer and cleaner and simpler and elegant.</p>