I was reading about multiple inclusions of the same header in one file, and found an interesting statement (link):
There are a couple of tricks with header files were you deliberately include it multiple times (this does actually provide a useful feature).
I understand that those tricks are probably undesired and confusing in real-world projects (especially since people take precautions against multiple inclusions, like include guards and #pragma once
).
But still, what are those tricks? I came up with a few ideas, but would like to see some actual examples (ideally, safe and tried).
My thoughts:
- Pseudo-templates in C, where template parameters are substituted with preprocessor definitions. It can be done without inclusions, but functions may be too big or too numerous, so making a separate file would make sense.
- Block-by-block struct/class construction (concatenation of pieces). It may help emulate inheritance in C and prevent code duplication when defining structs with common members.
- Look-up tables and other compile-time data structures (again, with the aid of preprocessor definitions).