如何在cpp宏中生成换行符?
如何编写扩展以包含换行符的 cpp 宏?
How do I write a cpp macro which expands to include newlines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何编写扩展以包含换行符的 cpp 宏?
How do I write a cpp macro which expands to include newlines?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
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.
在行尾使用 \。 我见过很多 C macos,它们使用 do...while(0)
另外,请记住在许多情况下使用括号。
例子:
Use the \ at the end of the line. I've seen a lot of C macos where they use a do...while(0)
Also, remember to use parenthases in many instances.
Example:
这不可能。 仅当您查看列表文件或预处理器输出时,它才有意义。
编写宏以使其更易于阅读的常用技术是使用 \ 字符将宏延续到下一行。
我(相信我)已经看到编译器在列出输出的扩展宏中包含新行 - 为了您的利益。 这仅对我们这些阅读扩展宏的可怜人有用,以尝试理解我们真正要求编译器做什么。 这对编译器没有影响。
营地; 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.
使用
\
,如下所示:Use
\
, like so:我正在开发一个大型项目,其中涉及大量预处理器宏函数来合成无法被模板替换的任何代码。 相信我,我熟悉各种模板技巧,但是只要没有可以直接创建代码的标准化、类型安全的元编程语言,我们就必须坚持使用旧的预处理器及其繁琐的宏来解决一些问题如果没有的话,需要编写十倍以上的代码。
有些宏跨越很多行,在预处理的代码中很难阅读。 因此,我想到了解决该问题的方法,我的想法如下:
假设我们有一个跨多行的 C/C++ 宏,例如在名为 MyMacro.hpp 的文件
中在我定义此类宏的每个文件中,我都包含另一个文件 MultilineMacroDebugging.hpp,其中包含以下内容:
这定义了一个空宏
__NL__
,这使得__NL__< /code> 定义在预处理期间消失。 然后可以在某个地方使用该宏,例如
在名为 MyImplementation.cpp 的文件中。
如果我需要调试
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
In every file where I defined such a macro, I include another file MultilineMacroDebugging.hpp that contains the following:
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.
If I need to debug the
PRINT_VARIABLE
macro, I just uncomment the line that defines the macroHAVE_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.不太确定你在这里问什么。 您想要多行宏吗?
此外,如果您想在宏中包含文字:
您在 x 中放入的内容将代替 ##x,因此:
这对于制作自定义全局函数很有帮助,然后只需更改函数名称的一部分即可。
另外:
将在 x 周围加上引号
Not quite sure what you're asking here. Do you want a macro on multiple lines?
Additionally, if you would like to include a literal in your macro:
what you you put in x will be put in place of ##x, so:
This can be helpful for making custom global functions then just need part of the function name changed.
Also:
Will put quotes around x
我也有同样的问题。 我在非 C 文件上滥用预处理器,因此 C 编译器忽略换行符这一事实与我无关。 我在宏定义的行尾使用 \\ (这是非常简洁的语法,类似于 LaTeX)并将结果传递给
这可以解决问题。 预处理器从 \\ 中去掉一个 \ 并连接各行,然后 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
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.
C 编译器可以识别空格,但它不区分空格、制表符或换行符。
如果您的意思是如何在宏的字符串内添加新行,那么:
会起作用。
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:
will work.