sfzhang
sfzhang

Reputation: 791

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

I read the dcmtk source code, and found a comment in ofstdinc.h:

// this file is not and should not be protected against multiple inclusion

And what kinds of header files SHOULD NOT be protected against multiple inclusion?

Upvotes: 15

Views: 1451

Answers (3)

Daniel Frey
Daniel Frey

Reputation: 56863

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_H

namespace 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
    }
}

Upvotes: 12

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

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.

Upvotes: 12

doron
doron

Reputation: 28882

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

Upvotes: 0

Related Questions