Topic: Stream width and operator<<(ostream &, const string &)


Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/10/03
Raw View
Brian Michael Freyburger (freyburg@gamut.stanford.edu) wrote:
: In the latest draft version I have access to, a legal definition of
: operator << on strings is:

: template <class charT, class osTraits, class strTraits>
: basic_ostream<charT, osTraits>&
: operator<< (basic_ostream<charT, osTraits>& os,
:             const basic_string<charT, strTtraits>& str)
: {
:     os.write (str.data (), str.size ());
:     return os;
: }

: However, this definition ignores the width format of the output
: stream.  For orthogonality to operator<<(ostream &, const char *), I
: think operator<< on strings should be defined as:

: template <class charT, class osTraits, class strTraits>
: basic_ostream<charT, osTraits>&
: operator<< (basic_ostream<charT, osTraits>& os,
:             const basic_string<charT, strTtraits>& str)
: {
:     return os << str.c_str();
: }

My first encounter with this had the same results.  What is this crazy
string that is to be better than char* yet does not adjust itself
properly?

So off to the DWP to find that it is required not a compiler bug.

Now consider:
 cin >> setw(10) >> someString;
which makes no sense.

It may be that there is a reason.

Anyone have the background for the decision?

John


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Brian Michael Freyburger <freyburg@gamut.stanford.edu>
Date: 1996/09/30
Raw View
In the latest draft version I have access to, a legal definition of
operator << on strings is:

template <class charT, class osTraits, class strTraits>
basic_ostream<charT, osTraits>&
operator<< (basic_ostream<charT, osTraits>& os,
            const basic_string<charT, strTtraits>& str)
{
    os.write (str.data (), str.size ());
    return os;
}

However, this definition ignores the width format of the output
stream.  For orthogonality to operator<<(ostream &, const char *), I
think operator<< on strings should be defined as:

template <class charT, class osTraits, class strTraits>
basic_ostream<charT, osTraits>&
operator<< (basic_ostream<charT, osTraits>& os,
            const basic_string<charT, strTtraits>& str)
{
    return os << str.c_str();
}

Of course, this should just specify the behavior of the function, and
the implementor is free to implement it as he wished, but she can't
ignore the width field!!

I had (something equivalent) to the following code which produced
unexpected output:

void output(const string &s, int i)
{
  cerr << setw(10) << s << ' ' << i;
}

Of course, the width was used for printing the integer, and the string
wasn't padded.

Brian Freyburger
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]