Hi, I'm trying to get community feedback for a proposal to enhance the C preprocessor support for multi-line macros.
For this discussion, I prefer to put aside the philosophical debate about using macro vs generics - I'm assuming preprocessor macros are a valid solution to implement generic logic (and other constructs) that are not part of the "C" language. Summary: When implementing long macro that can not be practical expressed in a single line, one must use escaped new lines to implement the macros. This result in hard to maintain code - it's not possible to move code between regular functions, and macro functions. Also, it is hard to enter comments, or format the code for readability, as each unescaped newline will break macro definition. Proposing: adding a new directive '#macro' which will define a multi-line macro. The macro body will end with "#endmacro". (Similar to #if ... #endif). For example, a macro to count the number of elements that match a specific condition, using statement expressions. This is a relatively simple example, as there are macros that span > 30 (or more) lines in various projects. --- #macro count_if(array, length, item, condition) ({ int count = 0 ; for (int i=0 ; i<length ; i++ ) { typeof(array[u]) *item = &array ; if ( condition ) count++ ; } ; count ; }) #endmacro --- Compare with alternative: --- #define count_if(array, length, item, condition) \ ({ \ int count = 0 ; \ \ for (int i=0 ; i<length ; i++ ) { \ typeof(array[u]) *item = &array ; \ if ( condition ) count++ ; \ } ; \ count ; \ }) --- Or (hope this align well): --- #define count_if(array, length, item, condition) \ ({ \ int count = 0 ; \ \ for (int i=0 ; i<length ; i++ ) { \ typeof(array[u]) *item = &array ; \ if ( condition ) count++ ; \ } ; \ count ; \ }) --- Implementation is relatively easy - I have a prototype working. Looking for feedback, before attempting to submit. Yair