C++ 如何在 cpp 宏中生成换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/98944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:50:52  来源:igfitidea点击:

How to generate a newline in a cpp macro?

c++macrosc-preprocessor

提问by user18458

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

如何编写一个扩展为包含换行符的 cpp 宏?

回答by Florian Fleissner

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

假设我们有一个跨多行的 C/C++ 宏,例如在名为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.hppthat contains the following:

在我定义此类宏的每个文件中,我都包含另一个文件MultilineMacroDebugging.hpp,其中包含以下内容:

// 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.

这定义了一个空宏__NL__,使__NL__定义在预处理过程中消失。然后可以在某处使用该宏,例如在名为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_VARIABLEmacro, I just uncomment the line that defines the macro HAVE_MULTILINE_DEBUGGINGin 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.

如果我需要调试PRINT_VARIABLE宏,我只需取消注释MyImplementation.cpp中定义宏HAVE_MULTILINE_DEBUGGING。生成的代码当然不会编译,因为__NL__宏结果未定义,这导致它保留在已编译的代码中,但是可以对其进行预处理。

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.

现在关键的一步是__NL__使用您最喜欢的文本编辑器用换行符替换预处理器输出中的字符串,瞧,预处理后您最终会得到替换宏结果的可读表示,这与编译器将看到的完全相似,除了对于人为引入的换行符。

回答by itj

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.

C 和 C++ 语言以相同的方式处理字符串之外的所有空格。就像一个分隔符。

回答by Mike Thompson

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.

C 和 C++ 编译器会忽略不带引号的空格(除了 > > 模板问题),因此让宏发出换行符实际上没有意义。您可以通过用反斜杠结束宏的每一行来使宏跨越多行,但这不会输出换行符。

回答by David L Morris

The C compiler is aware of white space, but it doesn't distinguish between spaces, tabs or new lines.

C 编译器知道空格,但它不区分空格、制表符或换行符。

If you mean how do I have a new line inside a string in a macro, then:

如果您的意思是如何在宏中的字符串中添加新行,那么:

#define SOME_STRING "Some string\n with a new line."

will work.

将工作。

回答by Charles Graham

Use the \ at the end of the line. I've seen a lot of C macos where they use a do...while(0)

在行尾使用\。我见过很多 C macos 使用 do...while(0)

#define foo() do \
{
  //code goes here \
  \
  \
}while(0);

Also, remember to use parenthases in many instances.

另外,请记住在许多情况下使用括号。

Example:

例子:

#define foo(x) a+b
//should be
#define foo(x) (a+b)

回答by PiNoYBoY82

Not quite sure what you're asking here. Do you want a macro on multiple lines?

不太确定你在这里问什么。你想要一个多行的宏吗?

#define NEWLINE_MACRO(x) line1 \
line2 \
line3

Additionally, if you would like to include a literal in your macro:

此外,如果您想在宏中包含文字:

#define NEWLINE_MACRO(x) ##x

what you you put in x will be put in place of ##x, so:

您放入 x 的内容将代替 ##x,因此:

NEWLINE_MACRO( line1 ) // is replaced with line1

This can be helpful for making custom global functions then just need part of the function name changed.

这有助于制作自定义全局函数,然后只需要更改部分函数名称。

Also:

还:

#define NEWLINE_MACRO(x) #x // stringify x

Will put quotes around x

将引号放在 x 周围

回答by Branan

Use \, like so:

使用\,像这样:

#define my_multiline_macro(a, b, c) \
if (a) { \
    b += c; \
}