Martin, I am working on the test for lib.string.io and there are several
questions:

1. Shall we implement an our own locale class to exercise the >> and <<
operators using it?

2. Small question about 21.3.7.9, paragraph 4:

"
Effects: Begins by constructing a sentry object k as if k were
constructed by typename basic_ostream<chart, thraits>::sentry k(os). If bool(k) is true, inserts characters as if by calling
os.rdbuf()->sputn(str.data(), n), padding as described in stage 3 of
22.2.2.2.2, where n is the larger of os.width() and str.size(); then
calls os.width(0). If the call to sputn fails, calls
os.setstate(iosbase::failbit).
"

As I understand os.width(0) should be called if bool(k) is true only. I
not sure, but Dinkumware STL from MSVC and STLPort does that while
stdcxx calls os.with(0) in any case:

incubator\stdcxx\trunk\include\ostream

// 21.3.7.9, p3 - defined here, declared inline in <string>
template<class _CharT, class _Traits, class _Allocator> inline
basic_ostream<_CharT, _Traits>&
operator<< (basic_ostream<_CharT, _Traits>                  & __strm,
           const basic_string<_CharT, _Traits, _Allocator> &__str) {
   _RW::__rw_insert (__strm, __str.data (), __str.length (),
                     __strm.width ()).width (0);
   return __strm;
}

Is this correct?
The attached small test (iocheck.cpp) to illustrate this situation.
3. Another small question about 21.3.7.9 paragraph 4:

The stdcxx calls os.setstate(ios_base::badbit). Dinkumware STL does the
same. STLPort  - according to standard.

The attached small test (iocheck2.cpp) to illustrate this situation.
What do you think about this?


Thanks,
Anton Pevtsov

#include <iostream>
#include <sstream>
#include <string>
#include <strstream>

int main(int argc, char* argv[])
{
    char buf[10];

    std::ostrstream os(buf, sizeof(buf));

    std::string str("12345678901");

    os << str;

    bool success = 
        std::ios_base::failbit == os.rdstate() & std::ios_base::failbit;

    if (success)
        std::cout << "test succeeded" << std::endl;
    else
        std::cout << "test failed, ostrstream.rdstate() doesn't contain " 
            << "std::ios_base::failbit" << std::endl;

    return 0;
}
#include <iostream>
#include <sstream>
#include <string>

int main(int argc, char* argv[])
{
    std::string str ("abcdefghijk");

    std::ostringstream os;

    const std::streamsize width = 2;

    os.width (width);

    os.clear (std::ios_base::failbit);

    os << str;

    bool success = (os.width () == width);

    if (success) 
        std::cout << "test succeeded" << std::endl;
    else
        std::cout << "test failed, expected ostringstream.width () == " 
            << width << ", got " << os.width () << std::endl;

    return 0;
}

Reply via email to