Possible Duplicate:
Does a const reference prolong the life of a temporary?
My compiler doesn't complain about assigning temporary to const reference:
string foo() {
return string("123");
};
int main() {
const string& val = foo();
printf("%s\n", val.c_str());
return 0;
}
Why? I thought that string returned from foo
is temporary and val can point to object which lifetime has finished. Does C++ standard allow this and prolongs the lifetime of returned object?