编译时条件包含。分段 headers 与包括整个实现

Compile time conditional inclusion. Segmenting headers vs including whole implementations

我正在重构一个包含大量长 header 文件的代码库(为了便于使用和简化编译,header 包含接口和实现)。

代码库避免了像野火一样的多态性,因此它使用宏解析内部存储类型,如下所示:

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

我的问题是,假设上述实现 header 文件有大量相同的行,是否最好将它们合并为一个,并使用宏来分隔实现之间的差异。

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.

是的...分解通用代码可以更轻松地确保错误修复和改进都正确应用于两者。

也就是说,还有其他方法可以支持编译时行为选择,这可能比在任何地方使用条件编译更好,例如在两次 "policy" 之间使用它一次 select 类 以及每个行为的支持代码。