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

发布于 2024-07-04 16:28:50 字数 27 浏览 9 评论 0原文

如何编写扩展以包含换行符的 cpp 宏?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

只是一片海 2024-07-11 16:28:50

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

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.

无人问我粥可暖 2024-07-11 16:28:50

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

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

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

例子:

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

Use the \ at the end of the line. I've seen a lot of C macos where they use a 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)
拒绝两难 2024-07-11 16:28:50

这不可能。 仅当您查看列表文件或预处理器输出时,它才有意义。

编写宏以使其更易于阅读的常用技术是使用 \ 字符将宏延续到下一行。

我(相信我)已经看到编译器在列出输出的扩展宏中包含新行 - 为了您的利益。 这仅对我们这些阅读扩展宏的可怜人有用,以尝试理解我们真正要求编译器做什么。 这对编译器没有影响。

营地; C++ 语言以相同的方式处理字符串之外的所有空格。 只是作为分隔符。

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.

归途 2024-07-11 16:28:50

使用 \,如下所示:

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

Use \, like so:

#define my_multiline_macro(a, b, c) \
if (a) { \
    b += c; \
}
_蜘蛛 2024-07-11 16:28:50

我正在开发一个大型项目,其中涉及大量预处理器宏函数来合成无法被模板替换的任何代码。 相信我,我熟悉各种模板技巧,但是只要没有可以直接创建代码的标准化、类型安全的元编程语言,我们就必须坚持使用旧的预处理器及其繁琐的宏来解决一些问题如果没有的话,需要编写十倍以上的代码。
有些宏跨越很多行,在预处理的代码中很难阅读。 因此,我想到了解决该问题的方法,我的想法如下:

假设我们有一个跨多行的 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 */

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

// Content of MultilineMacroDebugging.hpp

#ifndef HAVE_MULTILINE_DEBUGGING
#define __NL__
#endif

这定义了一个空宏 __NL__,这使得 __NL__< /code> 定义在预处理期间消失。 然后可以在某个地方使用该宏,例如
在名为 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)

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

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

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.

作业与我同在 2024-07-11 16:28:50

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

#define NEWLINE_MACRO(x) line1 \
line2 \
line3

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

#define NEWLINE_MACRO(x) ##x

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

NEWLINE_MACRO( line1 ) // is replaced with line1

这对于制作自定义全局函数很有帮助,然后只需更改函数名称的一部分即可。

另外:

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

将在 x 周围加上引号

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:

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

尾戒 2024-07-11 16:28:50

我也有同样的问题。 我在非 C 文件上滥用预处理器,因此 C 编译器忽略换行符这一事实与我无关。 我在宏定义的行尾使用 \\ (这是非常简洁的语法,类似于 LaTeX)并将结果传递给

sed s/'\\ '/'\n'/g

这可以解决问题。 预处理器从 \\ 中去掉一个 \ 并连接各行,然后 sed 通过用真正的换行符替换剩余的 \ 来再次分割它们。

I have the same problem. I abuse the preprocessor on non C files, therefore the fact that the C compiler ignores line breaks is irrelevant for me. I am using \\ at the end of lines in the macro definition (which is pretty neat syntax, resembling LaTeX) and pass the result through

sed s/'\\ '/'\n'/g

This does the trick. The preprocessor strips one \ away from the \\ and joins the lines, and the sed splits them again by replacing the remaining \ by a real newline.

尽揽少女心 2024-07-11 16:28:50

C 编译器可以识别空格,但它不区分空格、制表符或换行符。

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

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

会起作用。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文