Why is taking the address of a temporary illegal?
I know that the code written below is illegal void doSomething(std::string *s){} int main() { doSomething(&std::string("Hello World")); return 0; } The reason is that we are not allowed to take the address of a temporary object. But my question is WHY? Let us consider the following code class empty{}; int main() { empty x = empty(); //most compilers would elide the temporary return 0; } The accepted answer here mentions "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." According to the