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.