title: What's the lifetime of temporary objects in a range-for?
body: <p>Consider this class:</p>
  
  <pre><code>class Foo
  {
  public:
  
      ~ Foo ()
      {
          std::cout &lt;&lt; "~Foo\n";
      }
  
      typedef std::vector&lt;std::string&gt; Words;
  
      const Words &amp; words ()
      {
          return m_words;
      }
  
  private:
  
      Words m_words = {"foo", "bar", "baz"};
  };
  </code></pre>
  
  <p>Section 12.2 of the C++ standard specifies lifetimes of temporary objects. I thought this would be okay:</p>
  
  <pre><code>for (auto w : Foo () .words ())
      std::cout &lt;&lt; w &lt;&lt; "\n";
  </code></pre>
  
  <p>But it wasn't</p>
  
  <pre><code>~Foo
  terminate called after throwing an instance of 'std::logic_error'
    what():  basic_string::_M_construct null not valid
  [1]    10290 abort (core dumped)  ./a.out
  </code></pre>
  
  <p>The standard is confusing me. Why is <code>~Foo</code> being called before the loop runs?</p>