title: Why is taking the address of a temporary illegal?
body: <p>I know that the code written below is illegal</p>
  
  <pre><code>void doSomething(std::string *s){}
  int main()
  {
       doSomething(&amp;std::string("Hello World"));
       return 0;
  }
  </code></pre>
  
  <p>The reason is that we are not allowed to take the address of a temporary object. But my question is <strong>WHY?</strong></p>
  
  <p>Let us consider the following code</p>
  
  <pre><code>class empty{};
  int main()
  {
        empty x = empty(); //most compilers would elide the temporary
        return 0;
  }
  </code></pre>
  
  <p>The accepted answer <a href="https://stackoverflow.com/questions/4286301/why-isnt-the-copy-constructor-called-here/4286330#4286330">here</a> mentions</p>
  
  <p>"usually the compiler consider the temporary and the copy constructed as two objects that are located in the exact same location of memory and avoid the copy."</p>
  
  <p>According to the statement it can be concluded that the temporary was present in some memory location( hence its address could have been taken) and the compiler decided to eliminate the temporary by creating an in-place object at the same location where the temporary was present.</p>
  
  <p>Does this contradict the fact that the address of a temporary cannot be taken?</p>
  
  <p>I would also like to know how is return value optimization implemented. Can someone provide a link or an article related to RVO implementation?</p>