title: When should I use C++14 automatic return type deduction?
body: <p>With GCC 4.8.0 released, we have a compiler that supports automatic return type deduction, part of C++14. With <code>-std=c++1y</code>, I can do this: </p>
  
  <pre><code>auto foo() { //deduced to be int
      return 5;
  }
  </code></pre>
  
  <p>My question is: When should I use this feature? When is it necessary and when does it make code cleaner?</p>
  
  <h2>Scenario 1</h2>
  
  <p>The first scenario I can think of is whenever possible. Every function that can be written this way should be. The problem with this is that it might not always make the code more readable.</p>
  
  <h2>Scenario 2</h2>
  
  <p>The next scenario is to avoid more complex return types. As a very light example:</p>
  
  <pre><code>template&lt;typename T, typename U&gt;
  auto add(T t, U u) { //almost deduced as decltype(t + u): decltype(auto) would
      return t + u;
  }
  </code></pre>
  
  <p>I don't believe that would ever really be a problem, though I guess having the return type explicitly depend on the parameters could be clearer in some cases.</p>
  
  <h2>Scenario 3</h2>
  
  <p>Next, to prevent redundancy:</p>
  
  <pre><code>auto foo() {
      std::vector&lt;std::map&lt;std::pair&lt;int, double&gt;, int&gt;&gt; ret;
      //fill ret in with stuff
      return ret;
  }
  </code></pre>
  
  <p>In C++11, we can sometimes just <code>return {5, 6, 7};</code> in place of a vector, but that doesn't always work out and we need to specify the type in both the function header and the function body. This is purely redundant, and automatic return type deduction saves us from that redundancy.</p>
  
  <h2>Scenario 4</h2>
  
  <p>Finally, it can be used in place of very simple functions:</p>
  
  <pre><code>auto position() {
      return pos_;
  }
  
  auto area() {
      return length_ * width_;
  }
  </code></pre>
  
  <p>Sometimes, though, we might look at the function, wanting to know the exact type, and if it isn't provided there, we have to go to another point in the code, like where <code>pos_</code> is declared.</p>
  
  <h2>Conclusion</h2>
  
  <p>With those scenarios laid out, which of them actually prove to be a situation where this feature is useful in making the code cleaner? What about scenarios I have neglected to mention here? What precautions should I take before using this feature so that it doesn't bite me later? Is there anything new this feature brings to the table that isn't possible without it?</p>
  
  <p><sub>Note that the multiple questions are meant to be an aid in finding perspectives from which to answer this.</sub></p>