title: Why doesn't passing temporary object to another thread cause undefined behavior?
body: <p>Below is an example quoted from <strong>C++ Concurrency in Action $2.2</strong>   </p>
  
  <pre><code>void f(int i,std::string const&amp; s);
  void oops(int some_param)
  {
    char buffer[1024];
    sprintf(buffer, "%i",some_param);
    std::thread t(f,3,buffer);
    t.detach();
  }  
  </code></pre>
  
  <p>And the author says it's undefined behavior:</p>
  
  <blockquote>
    <p>In this case, it’s the pointer to the local variable <code>buffer</code> that’s passed through to the new thread, and there’s a significant chance that the function oops will exit before the buffer has been converted to a <code>std::string</code> on the new thread, thus leading to <strong>undefined behavior</strong>. The <strong>solution</strong> is to cast to <code>std::string</code> before passing the buffer to the std::thread constructor:   </p>
  </blockquote>
  
  <pre><code>void f(int i,std::string const&amp; s);
  void not_oops(int some_param)
  {
    char buffer[1024];
    sprintf(buffer,"%i",some_param);
    std::thread t(f,3,std::string(buffer));
    t.detach();
  }   
  </code></pre>
  
  <p>But I'm confused about that. Because as far as I'm concernd, there's no difference between the two forms: </p>
  
  <p>In the first code snippet,when passing <code>buffer</code> to the function as parameter,it will also generate a temporary <code>std::string</code> object,and bind function parameter to that temporary object.So it's exactly the same with the second code snippet.The only difference is that temporary object in former one is generated by compiler implicitly and latter one by user explicitly.</p>