What kinds of header files should not be protected against multiple inclusion? What kinds of header files should not be protected against multiple inclusion? c c

What kinds of header files should not be protected against multiple inclusion?


Preprocessor metaprogramming. That is, using the included file as a sort of compile-time function that performs some task. The arguments to the function are macros. For example, the file you linked has a section that looks like this:

// define INCLUDE_STACK to include "ofstack.h"#ifdef INCLUDE_STACK#include "dcmtk/ofstd/ofstack.h"#endif

So if I wanted to include "ofstack.h", I would do so like this:

#define INCLUDE_STACK#include "ofstdinc.h"#undef INCLUDE_STACK

Now, imagine later down the line, someone wants to use this particular section of the header:

// define INCLUDE_STRING to include "ofstring.h"#ifdef INCLUDE_STRING#include "dcmtk/ofstd/ofstring.h"#endif

So they do the following:

#define INCLUDE_STRING#include "ofstdinc.h"#undef INCLUDE_STRING

If "ofstdinc.h" had include guards, it wouldn't be included.


One example are header files which expect you to define a macro. Consider a header m.h with

M( foo, "foo" )M( bar, "bar" )M( baz, "baz" )

This can be used in some other header like this:

#ifndef OTHER_H#define OTHER_Hnamespace other{    enum class my_enum    {#define M( k, v ) k,#include "m.h"#undef M    };    void register_my_enum();}#endif

and in some other file (possibly implementation):

#include "other.h"namespace other{    template< typename E >    void register_enum_string( E e, const char* s ) { ... }    void register_my_enum()    {#define M( k, v ) register_enum_string( k, v );#include "m.h"#undef M    }}


You almost always want to protect against multiple inclusion. The only time you do not want to do so is if you are doing some fancy stuff with C macros and you therefore WANT to have multiple inclusion to get the code generation you desire (don't have an example of this offhand).