Header.hpp
#ifndef HEADER_HPP
#define HEADER_HPP
#include <vector>
struct MyStruct
{
std::vector<int> getStuff();
};
#endif
Now, if I use vector because of Header.hpp, then I don't #include <vector>:
#include "Header.hpp"
// I don't #include <vector> because the only time I'm ever using it is because of
// functions/classes defined in Header.hpp (that is, it's because of Header.hpp I'm
// using a vector here at all, so I don't include <vector> because it's Header.hpp's
// responsibility to include its dependencies)
MyStruct s;
std::vector<int> v = s.getStuff();
// That is, MyStruct is defined in Header.hpp, and so is its getStuff() method. If I
// use a vector because of MyStruct::getStuff(), then I don't include the vector
However, to continue the example, if I ever use vector in a way that is not associated with Header.hpp, then I #inlcude <vector>
#include "Header.hpp"
#include <vector> // Notice I'm including vector
// Here is the same code as before...
MyStruct s;
std::vector<int> v = s.getStuff();
// Here, I'm using vector in a way that is not caused by Header.hpp, so I make sure I manually #include <vector>
std::vector<float> other = {1.0};
Note that I also manually include any dependencies if references/pointers are used to non-standard objects (because these things could be forward declared (but you can't (legally) forward declare standard types (except for a few exceptions))). So in the previous example, if getStuff() returned a MyOtherStruct* instead, then I would always include "MyOtherStruct.hpp" (in both code samples) because then Header.hpp can forward declare MyOtherStruct.