I am working with a codebase which is written in C and C++. About 50-50.
Due to this, there are a lot of macros in c-headers which act as functions,
e.g:
#define FOR_EACH_ITEM(item, func)
while((item)) {
func
item = item->next;
}
Due to their age, these macros are well established (fellow programmers are familiar with what they do), so they inevitably leak into C++ code.
Some macros are way too large, so ideally, it would be better if they were written as inline/function templates.
I see 3 tactics:
- copy each macro as an c++ inline function template as well
- C++ code could use the inline functions.
- C code can still use the macros
- (+) Debugging possible for C++.
- (-) code duplication.
- make c++ inline function templates that use (wrap) the macros
- C++ code could use the inline functions.
- C code can use the macros
- No debugging possible.
- No duplication
- convert every macro to c++ inline function
- C++ code will use the inline functions.
- C code will call the inline functions via (non-inline) extern “C” functions
- (+) debugging possible.
- (-) performance and expression freedom in c files will take a serious hit.
The C-files are old, and we sometimes convert them to C++,
but this will take many years (>10) to effectively change, and the macro usage will keep on growing.
How is this issue typically tackled in the industry?
Is there an alternative solution that I am not seeing?