0

Is this post still true? Which iomanip manipulators are 'sticky'?

As I understand all number manipulators are sticky like hex, oct, fixed, scientific. But not left and right. Are there any other that are sticky? With sticky I mean you can use the output stream more than once without you have to set the manipulator again.

I used this Code to test:

std::istringstream test { "Fully !!!weired~ word0s!! cheers" };
std::cout << std::right << std::setw(20) << std::scientific;
while (test.good()) {
    std::string x;
    test >> x;
    std::cout << x << "\n";
}

std::cout  <<  0.1;

Output:

               Fully
!!!weired~
word0s!!
cheers
1.000000e-01
Charlie
  • 1,126
  • 2
  • 14
  • 30
  • You should read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Putting `test.good()` in the loop condition is just the same as `!test.eof()`. – Some programmer dude Jan 28 '18 at 00:28
  • But I am checking for .good() - http://en.cppreference.com/w/cpp/io/basic_ios/good which is not !eof() like you see in the table. – Charlie Jan 28 '18 at 00:30
  • @thomas Ask yourself how you will know when `test >> x` *fails*. You won't until (a) it does, and (b) you print junk to `std::cout` immediately after that. Only after *both* of those happen will you iterate the next loop, where `good()` will finally tell you the wheels fell off. By then it's too late. [Read the linked question](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – WhozCraig Jan 28 '18 at 00:50
  • I understand your comment. Yes the wheel fell off and the stream is also in fail state if x is an int for example. My prof uses this example: while (is.good() && !std::isalpha(is.peek())) { is.ignore(); } with the comment: Use is.good() in the condition as this will prevent to read from the stream in EOF state with peek() – Charlie Jan 28 '18 at 01:07
  • @thomas professors rarely know C++ well enough to write code. It is an error to "use is.good() in the condition". – Cubbi Jan 28 '18 at 19:59
  • (and yes, in every version of C++, all iostream manipulators are "sticky" except one: `std::setw` is reset after use) – Cubbi Jan 28 '18 at 20:02
  • So, this is either closable as no longer reproducible or a duplicate of [Which iomanip manipulators are 'sticky'?](https://stackoverflow.com/questions/1532640/which-iomanip-manipulators-are-sticky). @Cubbi See that question, particularly Charles's answer to it: it is not the case that `width` is reset after use, only that multiple other stream functions are defined as setting it to `0` before they proceed. A technicality, sure, but quite important. – underscore_d Sep 29 '18 at 23:41

1 Answers1

0

It seems there was an error in the code with setw(20) which is needed that right and left manipulators work. This means left and right are also sticky. And I think the post is still valid with C++ 14.

Charlie
  • 1,126
  • 2
  • 14
  • 30