File Top.h
#include <string>
...some code...
File Bottom.h:
#include "Top.h"
void someFunction() {
string s = new String();
...
}
The Bottom.h does not contain an include for the string header, but the code will work because it's implicitly been included by Top.h
Now, if Top.h changes for any reason not to include string anymore, Bottom.h will break.
I know that it's good practice to have your .h files "include independent", meaning each of them explicitly includes all the headers it uses. However, maintenance of such practice is additional mental task I need to do, which is prone to errors since I'm doing it manually.
Is there a way to have the IDE or compiler check for this? Are there static code analyzers that warn the user that they are using a component that isn't explicitly included in the file it's used in? Is there a compiler flag that checks for this? I've been poking around this and haven't managed to find anything like this, but it doesn't look like to difficult thing to implement, and seems like something that could ensure code stability. Given that, is there a reason that what I'm suggesting would be a bad idea or not work in some cases? If so, which?
Does this make any sense at all?
EDIT after reading some answers:
Could it be possible to make linker/compiler semi-process each file semi-separately?
It would take each file and look at all the headers it includes. Then it goes through that files, but doesn't compile them, only looks which symbols are defined there, excluding their own headers. Then it goes through the file we started with and see if it uses any symbols that it didn't encounter in the previous step. If it does, that means that the symbol wan't included at all or was included implicitly through header of one of the includes.
Wouldn't that solve the problem, or are there some features of C++ that wouldn't work with this?