title: Binding const& of temporary: No compiler warning?
body: <p>I have a <code>TestClass</code> with a <code>const&amp;</code> member variable. I know from various places and own experiences that it is a bad idea to initialize this <code>const&amp;</code> with the reference to a temporary value. So I was quite suprised that the following code will compile fine (tested with <code>gcc-4.9.1</code>, <code>clang-3.5</code>, and <code>scan-build-3.5</code>) but fail to run properly.</p>
  
  <pre><code>class TestClass {
    public:
      // removing the "reference" would remove the temporary-problem
      const std::string &amp;d;
  
      TestClass(const std::string &amp;d)
          : d(d) {
          // "d" is a const-ref, cannot be changed at all... if it is assigned some
          // temporary value it is mangled up...
      }
  };
  
  int main() {
  
      // NOTE: the variable "d" is a
      // temporary, whose reference is not valid... what I don't get in the
      // moment: why does no compiler warn me?
      TestClass dut("d");
  
      // and printing what we got:
      std::cout &lt;&lt; "beginning output:\n\n";
      // this will silently abort the program (gcc-4.9.1) or be empty
      // (clang-3.5) -- don't know whats going on here...
      std::cout &lt;&lt; "dut.d: '" &lt;&lt; dut.d &lt;&lt; "'\n";
      std::cout &lt;&lt; "\nthats it!\n";
  
      return 0;
  }
  </code></pre>
  
  <p>Why does none of the two compilers warn me at compile-time? See also this <a href="http://ideone.com/NDI9Gw" rel="nofollow">ideone</a>, with some more testing going on.</p>