Creating header guards for my h/hpp files has always been standard practice for me, but I wonder, why is it even possible to include de same file twice? Is there a case where you actually need unprotected headers?
If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time.
Implementation. This rule means that if the header uses a type — such as ' FILE * ' or ' size_t ' - then it must ensure that the appropriate other header ( <stdio. h> or <stddef. h> for example) should be included.
No problem. Making codebases a better place one line of code at a time :) Also, are you saying I can add both my #include <iostream> and #include "WorldState. h" inside global.
That is not mandatory, but it is a very good idea. It means that you can easily associate the header file as being the declarations of the non-static items in a file of source code. It is not specific to embedded programming. The "file name extension" (.
"Parameterized" header files can be used to simulate C++-ish-style templates in C. In such cases the header file will depend on a number of macros ("template parameters"). It will generate different code depending on the actual "value" of these macros.
So, the typical usage of such header would look as a sequence of "template parameter" macro definitions followed by the #include
directive, followed by another sequence of "template parameter" macro definitions followed by the same #include
, and so on.
https://stackoverflow.com/a/7186396/187690
When using this technique you will see header files without any include guards or header files with include guards that cover only a portion of the file.
Stuff like Boost.PP does lots of tricks by including headers multiple times. It essentially allows for primitive forms of loops.
Also, X-Macros are designed to be included multiple times.
In C:
#undef NDEBUG
#include <assert.h>
...code using active asserts...
#define NDEBUG
#include <assert.h>
...code using disabled asserts...
Rinse and repeat. The analogue in C++ uses the header <cassert>
instead.
So, sometimes there are reasons to include a header twice. Not often, but there are reasons to do so.
Cases like these are rare, and when they do exist, a re-design is better suited anyway. One I can think of is headers that amass declarations:
//functions.h
virtual void foo();
virtual void goo();
//classes.h
class A : public Base
{
#include "functions.h"
};
class B : public Base
{
#include "functions.h"
};
This wouldn't work if functions.h
had include guards, but then again, this is pretty awkward code...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With