Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a case where including the same header twice is actually helpful?

Tags:

c++

c

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?

like image 538
Luke B. Avatar asked Dec 18 '12 19:12

Luke B.


People also ask

What happens if a header file is included twice?

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.

Should headers include other headers?

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.

Is it acceptable for one header file to #include another?

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.

Do header files need to have the same name?

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" (.


4 Answers

"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.

like image 180
AnT Avatar answered Oct 21 '22 22:10

AnT


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.

like image 20
Pubby Avatar answered Oct 22 '22 00:10

Pubby


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.

like image 6
Jonathan Leffler Avatar answered Oct 21 '22 23:10

Jonathan Leffler


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...

like image 1
Luchian Grigore Avatar answered Oct 21 '22 23:10

Luchian Grigore