1

Does the standard define which iomanip manipulators are sticky and which are not?

I keep finding myself referring to: https://stackoverflow.com/a/1533222/2642059

Which, is a great answer, but even says:

on my version

This coupled with the fact that I cannot find the concept of sticky on http://en.cppreference.com or http://www.cplusplus.com leads me to wonder whether this is an implementation defined concept.

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 1
    They are all sticky, except `setw` which is affected by the requirement for the standard operators to call `width(0)` at the end of their operations. – Bo Persson Oct 31 '16 at 12:00
  • 2
    @downvoters Can I get a reason for the downvote? Is there something wrong with this question (other than the mandatory downvoting for questions tagged with C++?) – Jonathan Mee Oct 31 '16 at 12:03
  • @underscore_d Yeah, I concur. The answer is better over there too. I've marked this as a duplicate. – Jonathan Mee Sep 30 '18 at 19:13

2 Answers2

2

There isn't really a concept of sticky/non-sticky manipulators in the standard C++ library. The only non-sticky manipulator in the standard C++ library is the width(). None of the other standard formatting flags is changed as part of input or output. What user operators do and whether they reset setting after use is entirely up to them.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Is there any documentation that warns of `setw` being stomped whereas other manipulators do not have said warning? Seems like the absence of a warning on all modifiers might be a good basis for an official statement? – Jonathan Mee Oct 31 '16 at 12:11
  • 2
    @JonathanMee: as Bo mentioned in the comment the standard library operators affecting the `width()` are careful to state that they call `width(0)`. There isn't a warning as such. There is just about half a dozens or so places scatter over the standard where `width(0)` is called (I found 8 locations scattered of the strings, locales, and IOStreams chapters). – Dietmar Kühl Oct 31 '16 at 12:18
  • 1
    @JonathanMee cppreference attempts to enumerate all stomping of width at http://en.cppreference.com/w/cpp/io/manip/setw – Cubbi Oct 31 '16 at 20:55
  • @Cubbi You know I was trying to write this list up myself, and demonstrate that `setw` is stomped at the standards behest where other manipulators are not. But reading this... I'd like to accept an answer with a copy and link to this list. – Jonathan Mee Nov 01 '16 at 11:07
2

Not "official", but cppreference.com says, in std::setw

The width property of the stream will be reset to zero (meaning "unspecified") if any of the following functions are called:

As the edit history in cppreference says, this list was compiled by grepping the standard draft for "width"

All other manipulators are "sticky", as in, the stream state changes they perform persist until explicitly changed again.

Cubbi
  • 46,567
  • 13
  • 103
  • 169