Header files are only text included when they occur. There is no real reason why they cannot be turned on more than once. If headers are used only for declaration and without definitions (and without template declarations with default arguments), there is not even a problem with including them more than once.
Thus, <cassert> is a canonical example of including a file more than once: you can change the NDEBUG definition and get different behavior from the assert() macro within the same translation unit.
Now, something like include_once , which includes a file only once, is not as trivial as people tend to think. Here is an example where it is not clear how often foo.h should be included:
#include_once "foo.h" #include_once "./foo.h" #include_once "bar/foo.h"
Assuming include_once includes each file only once, how often should foo.h be included? All three files can easily link to the same physical file, but it may not be easily visible, for example, because one of them is a link to the other. It seems best to give the programmer control over how you can control how often they end up being used.
Dietmar Kühl
source share