how to detect references to members of temporary objects
<p>My colleague recently compiled our program in Windows, and discovered a bug of the sort:   </p>

<pre><code>  std::string a = "hello "; 
  std::string b = "world"; 
  const char *p = (a+b).c_str();
  printf("%s\n", p);
</code></pre>

<p>which for some reason did not crash in our Linux executables.</p>

<p>None of our compilers give any kind of warning, so we are now worried that this error might exist in the code. </p>

<p>Although we can <em>grep</em> for <code>c_str()</code> occurrences and do a visual inspection, there is a possibility that one might have also done the following:</p>

<pre><code>struct I {
     int num;
     I()   { num=0; }
};

struct X { 
     I *m;
     X()  { m = new I; }
     ~X() { delete m; }
     I  get() { return *m; }   // version 1, or
     I&amp; get() { return *m; }   // version 2
};
</code></pre>

<p>and accessed it like:</p>

<pre><code>  I&amp; a = X().get();         // will get a reference to a temporary, or a valid copy?
  cout &lt;&lt; a.num;
</code></pre>

<p>instead of :</p>

<pre><code> cout &lt;&lt; X().get().num;   
</code></pre>

<p>which is safe <em>(isn't it?)</em></p>

<p><strong>Question:</strong> Is there a way I can catch such errors (perhaps using the compiler, or even an assertion) ?<br>
I need to be sure that if author of <code>struct X</code> changes <code>get()</code> between <em>version 1</em> and <em>2</em> that the program will warn for the error</p>
