Boost logo

Boost :

From: Moore, Paul (Paul.Moore_at_[hidden])
Date: 2000-03-07 05:43:26

From: Marco Manfredini [mailto:marco_at_[hidden]]
> Are there are substatial problems with the ideas? could they
> be useful? Or are they simply stupid animal tricks :-)

I like this idea. Both the implementation for formatting, and the
"capturing" process for streams. On a vaguely related note, I once
implemented (what I thought was) a neat little trick to do formatted
messages on Windows using streams.

Basically, the idea is to make a class derived from stringstream with a
destructor which picks up what is in the stream and "does something" with it
- in my case, passes it to the Windows MessageBox() function. To use the
class, construct a temporary object of that class and write to it.
Temporaries are destroyed at end of statement, so the destructor action
happens when you hit the semicolon. Ie,

    MyClass() << "This is some output, you can do numbers, too: " << hex()
<< 1234;

This relies on the rule that temporaries are destroyed at the end of the
full expression where they are created. As far as I can see, this rule means
that the example works, but it also means that it's a little fragile in

Does anyone have any comments? Is this a good idea, or is there a better


--- cut here ---

class MB : public stringstream {
    const char *t;
    MB(const char *title = "Application Message") : t(title) {}
    ~MB() { ::MessageBox(0, str().c_str(), t, 0); }

int main()
    MB("Hello") << "This is message number " << 1;
    MB() << "This is message number " << 2;
    return 0;

Boost list run by bdawes at, gregod at, cpdaniel at, john at