Search code examples
c++macrosconditional-compilation

Compile time conditional inclusion. Segmenting headers vs including whole implementations


I'm refactoring a codebase with a large number of long header files (for ease of use, and to simplify compilation, the headers contain both the interface and the implementation).

The codebase avoids polymorphism like wildfire, and hence it resolves an internal storage type using macros like so:

#if defined USE_NIBBLE_CODES
#include "nibble.h"
#elif defined USE_BUTECODES 
#include "byte.h"
....
#endif

My question is, assuming that the aforementioned implementation header files have a large amount of lines in common, would it be preferable to merge them into one, and use the macros to separate the differences between implementations.


Solution

  • implementation header files have a large amount of lines in common, would it be preferable to merge them into one, and use the macros to separate the differences between implementations.

    Yes... factoring common code makes it much easier to ensure bug fixes and improvements are applied properly to both.

    That said, there are other ways to support a compile time choice of behaviours that may be better than using conditional compilation everywhere, such as using it once to select between two "policy" classes with support code for each of the behaviours.