C++ Logo

STD-DISCUSSION

Advanced search

Subject: IOStreams behavior definition
From: Philip Prindeville (philipp_subx_at_[hidden])
Date: 2020-06-30 12:01:35


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 think 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

P.S. Apologies for MacOS Mail.app being too stupid to do text/plain properly...


STD-DISCUSSION list run by herb.sutter at gmail.com

Older Archives on Google Groups