By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,933 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,933 IT Pros & Developers. It's quick & easy.

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

P: n/a
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<iomanipbeing used. What is the correct
include, and why?

Thanks in advance for your time!

Best regards,
Bernd.
Nov 17 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Nov 17, 11:20 am, Bernd Gaertner <gaert...@inf.ethz.chwrote:
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<iomanipbeing used. What is the correct
include, and why?

Thanks in advance for your time!

Best regards,
Bernd.
The header <iomaniptypically 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 <iomanipunless you plan
to use the set/reset members mentioned above.

You probably saw example code with include <iomanipmainly because
the subject at hand was manipulation of streams.

Nov 17 '08 #2

P: n/a
On Nov 17, 5:20 pm, Bernd Gaertner <gaert...@inf.ethz.chwrote:
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:ja*********@gmail.com
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
Nov 18 '08 #3

P: n/a
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 <iomanipunless 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<iostreamdoesn't suffice to do this. With an additional
#include<iosit 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.
Nov 18 '08 #4

P: n/a
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.
Nov 18 '08 #5

P: n/a
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<iostreamdoesn't suffice to do this. With an
additional #include<iosit 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
<iosfwdto be able to declare (but not define) the streams.
Bo Persson
Nov 18 '08 #6

P: n/a
On Nov 18, 6:29*pm, Bernd Gaertner <gaert...@inf.ethz.chwrote:
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 <iomanipisn'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, <ioswill be
included indirectly by <iostream>, so there's no risk of the
manipulator not being defined.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Nov 18 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.