This question already has an answer here:
- Should I include every header? 2 answers
consider this translation unit:
#include <map>
#include <string>
int main()
{
std::map<std::string, std::size_t> mp;
mp.insert(std::make_pair("hello", 42));
}
There are two things in this translation unit that are bothering me, and they are
- std::size_t
- std::make_pair
I have just assumed that <cstddef>
and <utility>
must have been #include
d by <string>
and <map>
.
How rightful is this assumption? At least for make_pair
I think there's a pretty strong guarantee because map member interfaces use std::pair
. For std::size_t
there is no formal guarantee but still it is very very very likely that it is available as soon as you include map
or string
. The stylistic question number one is Would you explicitly include <cstddef>
and <utility>
in this translation unit?
This part partly deals with the uncertaintly of some header being already included. However, there's the second part of the question. Suppose we have this
//f.h
#ifndef BIG_F_GUARD
#define BIG_F_GUARD
#include <string>
std::string f();
#endif
//f.cpp
#include "f.h"
std::string f()
{
std::string s;
return s;
}
Second question is: Would you explicitly #include <string>
into f.cpp?
I think I made my question clear. Btw. both questions are followed by a big WHY
:) Thanks.
To answer your questions:
Include minimal stuff to make it compile, but no obsolete/additional dependencies that are already fulfilled
What I tend to do, and it's not necessarily the right thing to do, is to include all the files I need to get the module to compile. The only problem with this is that when dependencies change, you can end up with code included which isn't necessarily used. However a good compiler will normally deal with this.
There's no need to include
<string>
in your .cpp file however, because it's included through the header file. The contents of any included header files essentially get 'pasted' into your cpp file.Hope this helps!
My two bits:
main.cpp: I would #include
<cstddef>
but probably not<utility>
here. You are usingstd::size_t
independent of whether it happens to be defined in the standard library headers. On the other hand, read the documentation onstd::map
and it is very clear that<map>
must somehow define std::pair. There's no reason to #include<utility>
. (This is particularly so since the connection between std::pair and is a bit of a WTF without reading the fine documentation.)f.hh: I would grudingly #include
<string>
here. Normally it is better to forward declare a class rather than #include the header that defines the class if the class is only used as a reference, a pointer, or a return type. The only reason to #include<string>
in f.hh is because forward declaringstd::string
is a bit tough.f.cpp: I would #include
<string>
here. If the return type from f() was some class Foo rather than std::string, the expectation should be that the header merely forward declares Foo. The source file needs to #include the header that specifies class Foo. That the return type is std::string doesn't change this basic concept. Who knows? Some pedantically correct programmer might fix the problem in f.hh. (The problem with f.hh being that it #includes<string>
when it doesn't really need to do so.)In the first case, I probably wouldn't but I should, if I want my code to be properly portable. There's no requirement that
map::size_type
issize_t
, so there's no necessity for<map>
to include a definition ofsize_t
. For that matter,size_t
can be a type alias rather than a distinct type, so even ifsize_type
issize_t
, it needn't have been defined in<map>
using that name. So as you say, it's likely but not guaranteed that<map>
includes<cstddef>
.In the second case, I definitely wouldn't, because I know I don't need to. IMO a .cpp file is entitled to rely on the headers included by its corresponding .h file, since you kind of expect that if you modify the .h file you potentially need to modify the .cpp file too -- change an interface implies change its implementation, most of the time. And even if you feel I'm not entitled, I can always document that f.h includes
<string>
, in which case I can rely on that.Regarding
<utility>
, I don't know whether<map>
is allowed to definestd::pair
(because it needs it) without definingstd::make_pair
(which is from the same standard header, and for the sake of argument let's say it isn't needed to define<map>
). This would be possible if the implementation's version of<utility>
itself includes a bunch of other files, for different bits, and<map>
just includes the bit it needs. Specific permission is given for headers to include other headers, but I don't know whether specific permission is given for headers to put things in namespacestd
without including the whole of the corresponding header. The thing is, in practice it is very difficult to notice that you've forgotten a standard include in these cases, because implementations don't check for you, and that's why I know that in practice I'd quite likely not do it. Luckily it should be any easy fix for anyone porting to an implementation with different header dependencies.Atleast
<map>
is forced to include<utility>
because the template formap
looks like this:(Standard 23.3/1)