Ссылка на неназванный временный объект (время жизни)

После прочтенияэтот ответ изИлдъярнЯ написал следующий пример, и похоже, что безымянный временный объект имеет то же время жизни, что и его ссылка!

Почему это возможно?Это указано в стандарте C ++?Какая версия?

Исходный код:

#include <iostream>  //cout
#include <sstream>   //ostringstream 

int main ()
{
        std::ostringstream oss;
        oss << 1234;

        std::string const& str = oss.str();
        char        const* ptr = str.c_str();

        // Change the stream content
        oss << "_more_stuff_";
        oss.str(""); //reset
        oss << "Beginning";
        std::cout << oss.str() <<'\n';

        // Fill the call stack
        // ... create many local variables, call functions...

        // Change again the stream content
        oss << "Again";
        oss.str(""); //reset
        oss << "Next should be '1234': ";
        std::cout << oss.str() <<'\n';

        // Check if the ptr is still unchanged
        std::cout << ptr << std::endl;
}

Исполнение:

> g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
> g++ main.cpp -O3
> ./a.out
Beginning
Next should be '1234':
1234

Ответы на вопрос(3)

Ваш ответ на вопрос