I wanted to use the approach given in this answer: https://stackoverflow.com/a/5419388/2050788, but apparently I'm missing something, as cout does not wind up being redirected. Can someone explain what I'm missing?
Here's a minimal compileable example:
#include <sstream>
#include <streambuf>
#include <iostream>
struct cout_redirect {
cout_redirect( std::streambuf * new_buffer )
: old( std::cout.rdbuf( new_buffer ) )
{ }
~cout_redirect( ) {
std::cout.rdbuf( old );
}
private:
std::streambuf * old;
};
void no_guard() {
std::cout << "No RAII" << std::endl;
std::stringstream buffer;
std::streambuf *old = std::cout.rdbuf(buffer.rdbuf());
std::cout << "Bla" << std::endl;
std::cout.rdbuf(old);
std::cout << "Text = " << buffer.str() << std::endl;
}
void with_guard()
{
std::cout << "Using RAII" << std::endl;
std::stringstream buffer;
cout_redirect(buffer.rdbuf());
std::cout << "Bla";
}
int main() {
no_guard();
with_guard();
}
The output is:
No RAII
Text = Bla
Using RAII
Bla
The No RAII case works as expected. In the RAII case, I would expect there to be no output, as cout should be redirected to the stringstream buffer. What am I missing? (compiled with g++ -Wall test.cpp
using gcc 7.3.1).
EDIT: Ok, I was being really dumb -- but I'm big enough to admit it and leave this here as a reminder of my fallibility.