std::noskipws: #include<ios> or #include<iomanip>?

Discussion in 'C++' started by Bernd Gaertner, Nov 17, 2008.

  1. Dear experts,

    according to the standard, manipulators like noskipws are declared in
    the header ios ([lib.iostreams.base]). But when I look at code that is
    around, I usually see #include<iomanip> being used. What is the correct
    include, and why?

    Thanks in advance for your time!

    Best regards,
    Bernd.
    Bernd Gaertner, Nov 17, 2008
    #1
    1. Advertising

  2. Bernd Gaertner

    Salt_Peter Guest

    On Nov 17, 11:20 am, Bernd Gaertner <> wrote:
    > Dear experts,
    >
    > according to the standard, manipulators like noskipws are declared in
    > the header ios ([lib.iostreams.base]). But when I look at code that is
    > around, I usually see #include<iomanip> being used. What is the correct
    > include, and why?
    >
    > Thanks in advance for your time!
    >
    > Best regards,
    > Bernd.


    The header <iomanip> typically has member functions resetiosflags,
    setiosflags, setbase, setfill, setprecision and setw. So it all
    depends what you are doing.

    Assuming you are using std::cout by including <iostream>, and since
    iostream derives from istream/ostream which derive from ios (a diamond
    hierarchy), noskipws doesn't need an include <iomanip> unless you plan
    to use the set/reset members mentioned above.

    You probably saw example code with include <iomanip> mainly because
    the subject at hand was manipulation of streams.
    Salt_Peter, Nov 17, 2008
    #2
    1. Advertising

  3. Bernd Gaertner

    James Kanze Guest

    On Nov 17, 5:20 pm, Bernd Gaertner <> wrote:

    > according to the standard, manipulators like noskipws are
    > declared in the header ios ([lib.iostreams.base]). But when I
    > look at code that is around, I usually see #include<iomanip>
    > being used. What is the correct include, and why?


    It's rather arbitrary from a user point of view, but
    manipulators which require an argument are declared in
    <iomanip>, and those that don't are declared in <ios>. (There
    are also a few, like endl, which only work on an ostream, and
    are declared in <ostream>.) The reason behind this distinction
    is mainly one of implementation; a manipulator which takes an
    argument requires an additional type, which must be defined; if
    you only include <ios>, you don't get a definition these types.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Nov 18, 2008
    #3
  4. Salt_Peter wrote:
    > Assuming you are using std::cout by including <iostream>, and since
    > iostream derives from istream/ostream which derive from ios (a diamond
    > hierarchy), noskipws doesn't need an include <iomanip> unless you plan
    > to use the set/reset members mentioned above.


    I'm doing std::cin >> std::noskipws, and I ran across a platform where
    #include<iostream> doesn't suffice to do this. With an additional
    #include<ios> it worked. But according to what you write above, ios
    should be included with iostream, meaning that the platform is buggy. Or
    is it not allowed to infer this from the aforementioned diamond hierarchy?

    Thanks,
    Bernd.
    Bernd Gaertner, Nov 18, 2008
    #4
  5. James Kanze wrote:
    > It's rather arbitrary from a user point of view, but
    > manipulators which require an argument are declared in
    > <iomanip>, and those that don't are declared in <ios>.


    Thanks. This seems to mean that for example the following code (you find
    many similar things on the web) actually uses the wrong include:

    #include <iostream>
    #include <iomanip> // for noskipws - (no skip whitespace)

    char s;

    int main()
    {
    std::cin >> std::noskipws;
    while (std::cin >> s)
    {
    std::cout << s;
    }
    return 0;
    }

    Best,
    Bernd.
    Bernd Gaertner, Nov 18, 2008
    #5
  6. Bernd Gaertner

    Bo Persson Guest

    Bernd Gaertner wrote:
    > Salt_Peter wrote:
    >> Assuming you are using std::cout by including <iostream>, and since
    >> iostream derives from istream/ostream which derive from ios (a
    >> diamond hierarchy), noskipws doesn't need an include <iomanip>
    >> unless you plan to use the set/reset members mentioned above.

    >
    > I'm doing std::cin >> std::noskipws, and I ran across a platform
    > where #include<iostream> doesn't suffice to do this. With an
    > additional #include<ios> it worked. But according to what you write
    > above, ios should be included with iostream, meaning that the
    > platform is buggy. Or is it not allowed to infer this from the
    > aforementioned diamond hierarchy?


    The current standard does not specify which headers should be included
    by <iostream>, so it is not a bug. Technically it is enough to include
    <iosfwd> to be able to declare (but not define) the streams.


    Bo Persson
    Bo Persson, Nov 18, 2008
    #6
  7. Bernd Gaertner

    James Kanze Guest

    On Nov 18, 6:29 pm, Bernd Gaertner <> wrote:
    > James Kanze wrote:
    > > It's rather arbitrary from a user point of view, but
    > > manipulators which require an argument are declared in
    > > <iomanip>, and those that don't are declared in <ios>.


    > Thanks. This seems to mean that for example the following code
    > (you find many similar things on the web) actually uses the
    > wrong include:


    > #include <iostream>
    > #include <iomanip>    // for noskipws - (no skip whitespace)


    > char s;


    > int main()
    > {
    >         std::cin >> std::noskipws;
    >         while (std::cin >> s)
    >         {
    >                 std::cout << s;
    >         }
    >         return 0;
    > }


    Well, the comment is wrong, and the include of <iomanip> isn't
    necessary here. But it doesn't hurt, and I can imagine some
    house rules just saying to include it anytime you use a
    manipulator, rather than having the developers have to learn the
    detailed rule; it doesn't hurt. (Of course, <ios> will be
    included indirectly by <iostream>, so there's no risk of the
    manipulator not being defined.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Nov 18, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Magnusson

    Is std::ios::noreplace non-standard?

    Martin Magnusson, Sep 29, 2003, in forum: C++
    Replies:
    1
    Views:
    772
    Peter van Merkerk
    Sep 29, 2003
  2. Luther Baker

    std::ios::hex

    Luther Baker, Jun 4, 2004, in forum: C++
    Replies:
    6
    Views:
    19,473
    Luther Baker
    Jun 5, 2004
  3. [rob desbois]

    iomanip setw() and std::string

    [rob desbois], Feb 7, 2008, in forum: C++
    Replies:
    1
    Views:
    591
    James Kanze
    Feb 8, 2008
  4. mathieu
    Replies:
    2
    Views:
    2,358
    mathieu
    Apr 8, 2009
  5. , India
    Replies:
    3
    Views:
    2,828
    James Kanze
    Nov 13, 2010
Loading...

Share This Page