title: Why does chaining methods in a statement fail to give an expected result?
body: <p>I can compute a <code>const char*</code> presentation of an <code>std::filesystem::path</code> variable like that:</p>
  
  <pre><code>std::filesystem::path path1(L"ConsoleApplication1.cpp");
  std::string strPath1 = path1.string();
  const char* charArrPath1 = strPath1.c_str();
  </code></pre>
  
  <p>But if I omit explicit assignment of a <code>string</code> variable and use method chaining in a char array initialization statement</p>
  
  <pre><code>const char* charArrPath1 = path1.string().c_str();
  </code></pre>
  
  <p>variable <code>charArrPath1</code> contains garbage after execution.</p>
  
  <p>How come the chaining of methods seemingly fails to work? </p>
  
  <p>PS If I explicitly assign the result of <code>string()</code> on <code>path1</code> to some unrelated variable after initialization of <code>charArrPath1</code>, I do have the result as expected:</p>
  
  <pre><code>std::filesystem::path path1(L"ConsoleApplication1.cpp");
  const char* charArrPath1 = path1.string().c_str();
  std::string strPathUnrelated = path1.string();
  std::cout &lt;&lt; charArrPath1 &lt;&lt; std::endl;
  </code></pre>
  
  <p>EPILEGOMENON:</p>
  
  <p>The method chaining seems to fail when the code is run in x86 configurations, both Debug and Release. When run in an x64 Release configuration, the following program</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;filesystem&gt;
  
  int main()
  {
      std::filesystem::path path1(L"ConsoleApplication1.cpp");
      const char* charArrPath1 = path1.string().c_str();
  
      std::cout &lt;&lt; charArrPath1 &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>prints out</p>
  
  <pre><code>ConsoleApplication1.cpp
  </code></pre>
  
  <p>Does anyone have an explanation for this behavior?</p>