C++ Logo

std-discussion

Advanced search

IOStreams behavior definition

From: Philip Prindeville <philipp_subx_at_[hidden]>
Date: Tue, 30 Jun 2020 10:35:00 -0600
Hi.

I’m trying to figure out what the appropriate behavior of std::setw() and std::ios_base::width() should be on a istream. It doesn’t seem to be well defined.

If I have:

    istringstream is(“0123456789”);
    unsigned u1, u2;

    is >> setw(2) >> u1 >> u2;

    cout << u1 << ‘ ‘ << u2 << endl;

I’d expect the output to be “1 23\n” with “4” being the next character on the input stream.

Not the case.

g++ and clang++ both try to consume all available digits into the first argument regardless of width, even overflowing if there are too many digits.

In short, there’s no way to do columnar input if there are no delimiter characters between numbers.

You’d thing this would be a fairly common/obvious usage scenario but it doesn’t seem to be well defined in the IOStreams specification.

http://www.cplusplus.com/reference/iomanip/setw/

Refers to both input and output streams but only describes output behavior concretely.

http://www.cplusplus.com/reference/ios/ios_base/width/

Its homologue width() on the other hand makes no mention of input at all.

Can some clarity be shed on this?

Thanks,

-Philip

Received on 2020-06-30 11:38:14