title: temporary object in range-based for
body: <p>I know that in general the life time of a temporary in a range-based <code>for</code> loop is extended to the whole loop (I've read <a href="https://stackoverflow.com/questions/9657708/c11-the-range-based-for-statement-range-init-lifetime">C++11: The range-based for statement: &quot;range-init&quot; lifetime?</a>). Therefore doing stuff like this is generally OK:</p>
  
  <pre><code>for (auto &amp;thingy : func_that_returns_eg_a_vector())
    std::cout &lt;&lt; thingy;
  </code></pre>
  
  <p>Now I'm stumbling about memory issues when I try to do something I thought to be similar with Qt's <code>QList</code> container:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;QList&gt;
  
  int main() {
    for (auto i : QList&lt;int&gt;{} &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3)
      std::cout &lt;&lt; i &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>
  
  <p>The problem here is that valgrind shows invalid memory access somewhere inside the <code>QList</code> class. However, modifying the example so that the list is stored in variable provides a correct result:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;QList&gt;
  
  int main() {
    auto things = QList&lt;int&gt;{} &lt;&lt; 1 &lt;&lt; 2 &lt;&lt; 3;
    for (auto i : things)
      std::cout &lt;&lt; i &lt;&lt; std::endl;
    return 0;
  }
  </code></pre>
  
  <p>Now my question is: am I doing something dumb in the first case resulting in e.g. undefined behaviour (I don't have enough experience reading the C++ standard in order to answer this for myself)? Or is this an issue with how I use <code>QList</code>, or how <code>QList</code> is implemented?</p>