Streaming and wrappers

The parameters provided by std::ios_base (left, right, hex, width, precision, and so on) are a closed set--a set defined in the mid-1980s and basically untouched since then. Since each manipulator modifies one of these parameters in the state of the stream, the set of manipulators is essentially closed as well. The modern way to affect the formatting of a particular data value is to wrap it in a wrapper. For example, suppose that we have written a generic algorithm for quoting values in a data file:

    template<class InputIt, class OutputIt>    OutputIt do_quote(InputIt begin, InputIt end,      OutputIt dest)    {      *dest++ = '"';      while (begin != end) {        auto ch = *begin++;        if (ch == '"') {            *dest++ = '\\';        }        *dest++ = ch;      } *dest++ ...

Get Mastering the C++17 STL now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.