Reputation: 1532
For example, in <algorithm>
, the function equal_range
returns a pair
, so can I assume that if I #include <algorithm>
, <utility>
is #include
d?
Upvotes: 5
Views: 255
Reputation: 145279
OK, late answer, but I'll add this anyway.
It's about the <iostream>
header. In C++98 and C++03 the standard did not guarantee that it would include <istream>
and <ostream>
, which meant that you were not guaranteed to have available e.g. std::endl
. And at least one compiler insisted on being formal about it (in spite of all the non-normative examples in the standard showing the intention)!
Well, I can't remember who pointed it out first, but it was discussed up, down and sideways in [comp.lang.c++]. Several times. And finally James Kanze, bless his soul (well he's not dead yet, in fact he's here on SO), put it to the committee, and it was fixed for C++0x.
So, there's now one header dependency that you can depend on.
Hallelujah!
But in the other direction, C++0x increases the confusion about which headers can put which names in the global namespace and the std
namespace. Now any standard library header corresponding to a C library header can freely pollute the global namespace, and be conforming. And so now there's no point in including, say, <cstdio>
, but instead now the wise thing to do is to include <stdio.h>
and accept a guaranteed global namespace pollution.
Cheers & hth.,
Upvotes: 2
Reputation: 2833
There is no guarantee, but since <algorithm>
uses pair internally it is common sense that it should include it. There are ways to make sure that a header file is included only once:
#pragma once
http://en.wikipedia.org/wiki/Pragma_once
#ifndef MY_H
#define MY_H
//header code
#endif /* MY_H */
And usually a good library will include all the needed files to compile, and those will be using this kind of mechanism to make sure that they are included only once.
There are sometimes exceptions to this rule especially in private (personal, not standard) libraries where the included header files are placed in the order needed for them to compile. And in that case if you have three includes, probably the second include uses the code from the first and the third from the first and second.
Best approach is to include all that you need to have direct access to the data structures you use.
Upvotes: 1
Reputation: 92271
You should always include what you need, you cannot rely on all implementations including the same set of headers in another header.
In your example, if a function returns a pair you can be reasonably sure that the pair
class is declared, but nothing requires the implementation to include the rest of <utility>
.
In fact, it is impossible to implement the standard library using exactly the headers shown in the standard, because there are some circular references. Implementations must split them in smaller parts, and include those sub-headers in the required <>
headers.
The GCC team, for example, is working to minimize the amount of inclusions to speed up compile time.
Upvotes: 4
Reputation: 38166
Yeah, you should not assume anything about inclusion of headers. I have been playing around with inclusion of headers for a while, and you know i observed that when it comes to header files like vector and string, you are 'almost' sure that you are including them from somewhere. Yes but this things are good only when you are just playing around. I would suggest you to always include the headers.
Upvotes: 1
Reputation: 39807
There is never a guaranteed inclusion of header files that other headers depend on. Fortunately, it's common practice (though not certain 100% of the time) that headers are guarded against multiple inclusion -- meaning you can #include them as many times as you wish without causing harm.
Upvotes: 5