Boost logo

Boost :

From: Gary Powell (Gary.Powell_at_[hidden])
Date: 2001-06-07 10:47:22


Hi Darin,

>>
        template<class T, class charT, class traits>
        basic_ostream<charT, traits>&
        operator<<(basic_ostream<charT, traits>& o, const complex<T>& x)
        {
                basic_ostringstream<charT, traits> s;
                s.flags(o.flags());
                s.imbue(o.getloc());
                s.precision(o.precision());
                s << ’(’ << x.real() << "," << x.imag() << ’)’; //
Line A
                return o << s.str();
        }

Note that your proposed implementation above has some bugs. Besides the
fact that it doesn't work with basic_ostream template classes other than
ostream, because it outputs each part of the complex number separately, it
will act strangely with regard to formatting state. For example, if a
width is set, the width will apply to the output of the ')' character
rather than to the output of the entire complex number.
<<

Is this then the general idiom for output of any formatted type? i.e. to
create an stringstream with the same properties of the ostream, format it,
and
then output the string?

I'm asking because while I've used the basic stream code for a number of
years,
I am not clear on how to best use the "std" version, and I can see a number
of
places where I am going to have problems.

Also, should Line A read:

        s << o.widen(’(’)
        << x.real()
        << o.widen(',')
        << x.imag()
        << o.widen(’)’); // Line A

(As in add a call to "widen()" on any formated character in case "wchar" is
the char type.
Which of course brings up the question, if I had a "stringOfChars" do I have
to loop through each one calling "widen()" on them individually?

  -gary-

gary.powell_at_[hidden]

PS
 I know this is slightly off topic to boost libraries, but as this appears
to be not well known it seems worthwhile to discuss it, or at least get a
pointer to a text which definitatively explains it.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk