title: decltype(auto) foo() returns local reference without any warning
body: <p>While playing with examples from Modern C++ I've written the following code.</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  
  static int count = 0;
  
  class Counter
  {
  public:
      Counter() { ++count; };
      Counter(Counter&amp; r) { ++count; };
      Counter(Counter&amp;&amp; r) { ++count; };
      ~Counter() { --count; };
  
      void foo() {};
  };
  
  decltype(auto) foo_warn()
  {
      Counter c;
      return (c);             // Warning about returning local reference
  }
  
  decltype(auto) foo_no_warn()
  {
      Counter c;
      return 1==1 ? c : c;    // No warning, still local reference returned
  }
  
  int main()
  {
      Counter&amp; a = foo_warn();
      Counter&amp; b = foo_no_warn();
  
      std::cout &lt;&lt; count &lt;&lt; std::endl;  // prints 0
  
      a.foo();
      b.foo();
  
      return 0;
  }
  </code></pre>
  
  <p>Code compiled with command:</p>
  
  <p><code>g++-6 -std=c++14 -Wall -O0    decl_fail.cpp   -o decl_fail</code></p>
  
  <p>Output:</p>
  
  <pre><code>g++-6 -std=c++14 -Wall -O0    decl_fail.cpp   -o decl_fail
  decl_fail.cpp: In function ‘decltype(auto) foo_warn()’:
  decl_fail.cpp:19:10: warning: reference to local variable ‘a’ returned [-Wreturn-local-addr]
    Counter a;
            ^
  </code></pre>
  
  <p>It's clear for me that <code>decltype(auto)</code> returns the reference for expressions (but still not intuitive), therefore <code>a</code> and <code>b</code> are invalid references (proven by <code>count==0</code>).</p>
  
  <p>The question is why compiler didn't warn me about that in <code>foo_no_warn</code>?</p>
  
  <p>Did I just found a bug in compiler or this is some explainable behaviour?</p>