This is follow up of question Is there a sequence point between return and expression in return statement? . The answer-er isn't replying to my comments , neither I'm unable to understand from his answer, nor I know how to bump the thread. So I created this question, my sincere apologies for doing this.
Consider the below code :
#include <iostream>
using namespace std;
struct foo{const char* bar ; foo(): bar("This is foo"){} };
foo returnByValue(){ return foo(); }
const foo& returnByConstRef() { return returnByValue(); } // *
int main() {
std::cout<< returnByConstRef().bar <<std::endl; // is life of temp is extended in while this expression?
return 0;
}
My understanding is that returnByValue()
expression (inside returnByConstRef()
) is the copy of temporary object foo()
(using copy ctor) . Now returnByConstRef()
which is const reference to temp object returned by returnByValue()
(copy of original temp()
object in the code) , Now when I invoke returnByConstRef().bar
why is it undefined behavior ?
Where is my thinking wrong? , does RVO does this?