Q:

Having a stream's streambuf persist beyond stream's destruction?

Is it possible to have a stream's streambuf persist after the destruction of its originating stream's destruction?
streambuf* f() {
    ifstream my_stream;
    // ...
    return my_stream.rdbuf();
}

void g() {
    streambuf *s = f();
    // ...
    ifstream my_new_stream;
    my_new_stream.rdbuf( s );
}

I.e., I want the pointer to the streambuf object returned by f() to remain valid even after my_stream goes out of scope.  At some later time, I want to set some other stream's streambuf to the saved streambuf.
Is this possible?  If so, how?

A:

It's not possible. std::ifstream constructs, owns and destroys its buffer. The only reason it's "exported" through rdbuf is to allow redirection of std::cin etc. In the GNU implementation, the buffer is a simple member (not a pointer).
std::ifstream does not export the rdbuf() member with an argument. One of its parent classes does, but this should be shadowed by the redefinition of rdbuf in std::basic_ifstream with signature
std::filebuf *rdbuf() const;

std::filebuf is also non-copyable.
(The rdbuf(std::streambuf *) member can be retrieved by casting to std::ios. Using this will cause the familiar nasal demons.)

