When I came across this before, I used stringstream along with a manipulator that displays the current contents of stringstream using a MessageBox :
#include <windows.h> #include <sstream> #include <ostream> std::ostream &MessageBox(std::ostream &s) { std::ostringstream *st = dynamic_cast<std::ostringstream *>(&s); if (NULL != st) ::MessageBox(NULL, st->str().c_str(), "", MB_OK); return s; }
To use this, the syntax looks fair, like using cout , but with the replacement MessageBox std::endl . For instance:
std::ostringstream stm; stm << " blah blah blah. Value: " << 1213.1231 << MessageBox;
Edit: mainly for fnieto. In this case, a downgrade is actually necessary. The reason is quite simple: a typical pastor receives and returns a link to ostream:
std::ostream &operator<<(std::ostream &os, T const &t) {
This takes the original stringstream object and silently (and safely) passes it to a simple ostream. This is fine in itself and great for most plugins and manipulators because they only interact with the ostream interface.
This manipulator, however, is slightly different - it uses the str() element, which ostream does not define at all. For our call to str() to solve and compile, we need to convert ostream & to ostringstream & , so the compiler knows that the object we are working with will actually have the str() member.
To exclude suppression, we will have only one choice: make its parameter ostringstream & . This will work until we never bind the operators:
my_stream << x; my_stream << MessageBox;
but trying to mate them will not:
Worse, a compiler error message will probably try to tell the user about std::basic_ostream<char>::str() , which is not mentioned at all in the user code. Worse, most people are sufficiently accustomed to the chain or do not give the same results, which will probably take some time to even figure out why the code sometimes worked fine, and in other cases it was not possible to compile with a completely illegible error message.