Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a newline in a cpp macro?

How do I write a cpp macro which expands to include newlines?

like image 327
user18458 Avatar asked Sep 19 '08 02:09

user18458


People also ask

How to do a line continuation in C?

The backslash character ('') is the line continuation character. It must be the last character on a line. The preprocessor joins lines ending with a line continuation character into a single line (for purposes of preprocessing and compilation).

Which operator is used to continue the definition of macro in the next line?

Therefore the operator '\' is used for continue the definition of macro in the next line.

How do you separate a multi line macro in C language?

We can write multiline macros like functions, but for macros, each line must be terminated with backslash '\' character. If we use curly braces '{}' and the macros is ended with '}', then it may generate some error. So we can enclose the entire thing into parenthesis.

Which of the following lines is the end of a macro?

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.


3 Answers

I am working on a large project that involves a lot of preprocessor macro functions to synthesize any code that cannot be replaced by templates. Believe me, I am familiar with all sorts of template tricks, but as long as there is no standardized, type safe metaprogramming language that can directly create code, we will have to stick with good old preprocessor and its cumbersome macros to solve some problems that would require to write ten times more code without. Some of the macros span many lines and they are very hard to read in preprocessed code. Therefore, I thought of a solution to that problem and what I came up with is the following:

Let's say we have a C/C++ macro that spans multiple lines, e.g. in a file named MyMacro.hpp

// Content of MyMacro.hpp

#include "MultilineMacroDebugging.hpp"

#define PRINT_VARIABLE(S) \
__NL__  std::cout << #S << ": " << S << std::endl; \
__NL__  /* more lines if necessary */ \
__NL__  /* even more lines */

In every file where I defined such a macro, I include another file MultilineMacroDebugging.hpp that contains the following:

// Content of MultilineMacroDebugging.hpp

#ifndef HAVE_MULTILINE_DEBUGGING
#define __NL__
#endif

This defines an empty macro __NL__, which makes the __NL__ definitions disappear during preprocessing. The macro can then be used somewhere, e.g. in a file named MyImplementation.cpp.

// Content of MyImplementation.cpp

// Uncomment the following line to enable macro debugging
//#define HAVE_MULTILINE_DEBUGGING

#include "MyMacro.hpp"

int a = 10;
PRINT_VARIABLE(a)

If I need to debug the PRINT_VARIABLE macro, I just uncomment the line that defines the macro HAVE_MULTILINE_DEBUGGING in MyImplementation.cpp. The resulting code does of course not compile, as the __NL__ macro results undefined, which causes it to remain in the compiled code, but it can, however, be preprocessed.

The crucial step is now to replace the __NL__ string in the preprocessor output by newlines using your favorite text editor and, voila, you end up with a readable representation of the result of the replaced macro after preprocessing which resembles exactly what the compiler would see, except for the artificially introduced newlines.

like image 173
Florian Fleissner Avatar answered Oct 26 '22 07:10

Florian Fleissner


It is not possible. It would only be relevant if you were looking at listing files or pre-processor output.

A common technique in writing macros so that they are easier to read is to use the \ character to continue the macro onto a following line.

I (believe I) have seen compilers that include new lines in the expanded macros in listing output - for your benefit. This is only of use to us poor humans reading the expanded macros to try to understand what we really asked the compiler to do. it makes no difference to the compiler.

The C & C++ languages treat all whitespace outside of strings in the same way. Just as a separator.

like image 29
itj Avatar answered Oct 26 '22 05:10

itj


C & C++ compilers ignore unquoted whitespace (except for the > > template issue), so getting a macro to emit newlines doesn't really make sense. You can make a macro span several lines by ending each line of the macro with a backslash, but this doesn't output newlines.

like image 6
Mike Thompson Avatar answered Oct 26 '22 05:10

Mike Thompson