Is there a way in gcc/g++ 4.* to write a macro that expands into several lines?
The following code:
#define A X \ Y
Expands into
X Y
I need a macro expanding into
X Y
__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.
The double-number-sign or token-pasting operator (##), which is sometimes called the merging or combining operator, is used in both object-like and function-like macros. It permits separate tokens to be joined into a single token, and therefore, can't be the first or last token in the macro definition.
Advertisements. When the program run and if the C preprocessor sees an instance of a macro within the program code, it will do the macro expansion. It replaces the macro template with the value of macro expansion. The #define directive is a good way of declaring constant compared to a constant or a variable value.
The \ at the end of each line of the macro definition is used to allow the macro definition to be split into multiple physical source lines (presumably for readability reasons). This works because a \ followed by a new-line is deleted during phase two of translation, but the preprocessor is run later, in phase four.
Got it!
#define anlb /* */ A /* */ B anlb anlb
gcc -E -CC nl.c
/* */ A /* */ B /* */ A /* */ B
Make the macro generate a special markup, say __CR__
, then pipe the result of CPP into a script which translates the macro to a true newline, for example, sed 's/__CR__/\n/g'
.
I just found this useful to generate a code pattern to be filled by hand. It is quite easier when the code is readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With