title: Is throwing a temporary value as reference undefined behavior?
body: <p>To my surprise, <code>std::runtime_error</code> only has a <code>const std::string&amp;</code> constructor, but no value constructor. Thus I am wondering if <code>throw std::runtime_error(&quot;Temporary&quot; + std::to_string(100));</code> is defined. After all we are creating an exception object that refers to a temporary object inside the scope of a function that immediately exits (as we throw out of it). Further investigation showed that while returning a constant reference to a temporary value immediately causes a segfault on my system, throwing one doesn't:</p>
  <pre><code>
  const std::string&amp; undefined_behavior() {
      return &quot;Undefined &quot; + std::to_string(1);
  }
  
  void test() {
      throw std::runtime_error(&quot;Hello : &quot; + std::to_string(2));   
  }
  
  void test2() {
      const std::string&amp; ref = &quot;Hello : &quot; + std::to_string(3); 
      throw ref;
  }
  
  int main(int argc, char** argv) {
      //std::cout &lt;&lt; undefined_behavior() &lt;&lt; std::endl;
      try {
          test(); 
      } catch(const std::runtime_error&amp; ex) {
          std::cout &lt;&lt; ex.what() &lt;&lt; std::endl;
      }
      try {
          test2();
      } catch(const std::string&amp; ref) {
          std::cout &lt;&lt; ref &lt;&lt; std::endl;
      }
  }
  
  </code></pre>
  <p>On my system, <code>undefined_behavior()</code> crashes immediately but <code>test()</code> and <code>test2()</code> run fine.</p>
  <p>While I know the call to <code>undefined_behavior()</code> is undefined behavior, is <code>test()</code> and <code>test2()</code> undefined? If not, do thrown values get a specfied treatment?</p>
  <p>And if they are all undefined but happened to work on my computer by accident, what is the proper way to throw an exception with a variable message?</p>