如何编写多行字符串文字
有没有办法在 C++ 中使用 Perl 那样的多行纯文本、常量文字? 也许有一些解析文件的技巧?
我知道你可以用 C++11 中的原始字符串来做到这一点。
Is there any way to have multi-line plain-text, constant literals in C++, à la Perl?
Maybe some parsing trick with #include
ing a file?
I know you can do it with raw strings in C++11.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
嗯……有点。 最简单的方法是仅利用相邻字符串文字由编译器连接的事实:
缩进并不重要,因为它不在引号内。
您也可以这样做,只要注意转义嵌入的换行符即可。 如果不这样做,就像我的第一个答案一样,将无法编译:
再次注意每行末尾的反斜杠,它们必须紧接在行结束之前,它们在源代码中转义换行符,以便一切都充当如果换行符不存在。 在字符串中有反斜杠的位置不会出现换行符。 使用这种形式,您显然无法缩进文本,因为缩进将成为字符串的一部分,并用随机空格混淆它。
Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler:
The indentation doesn't matter, since it's not inside the quotes.
You can also do this, as long as you take care to escape the embedded newline. Failure to do so, like my first answer did, will not compile:
Again, note those backslashes at the end of each line, they must be immediately before the line ends, they are escaping the newline in the source, so that everything acts as if the newline wasn't there. You don't get newlines in the string at the locations where you had backslashes. With this form, you obviously can't indent the text since the indentation would then become part of the string, garbling it with random spaces.
在 C++11 中,您拥有原始字符串文字。 有点像 shell 和脚本语言(如 Python、Perl 和 Ruby)中的此处文本。
字符串中的所有空格、缩进和换行符都会被保留。
这些也可以是 utf-8|16|32 或 wchar_t(带有常用前缀)。
我应该指出,这里实际上并不需要转义序列 V0G0N。 它的存在将允许将 )" 放入字符串中。换句话说,我可以放入
(注意额外的引号)并且上面的字符串仍然是正确的。否则我也可以使用
引号内的括号仍然需要。
In C++11 you have raw string literals. Sort of like here-text in shells and script languages like Python and Perl and Ruby.
All the spaces and indentation and the newlines in the string are preserved.
These can also be utf-8|16|32 or wchar_t (with the usual prefixes).
I should point out that the escape sequence, V0G0N, is not actually needed here. Its presence would allow putting )" inside the string. In other words, I could have put
(note extra quotes) and the string above would still be correct. Otherwise I could just as well have used
The parens just inside the quotes are still needed.
您还可以这样做:
You can also do this:
#define MULTILINE(...) #__VA_ARGS__
消耗括号之间的所有内容。
将任意数量的连续空白字符替换为单个空格。
#define MULTILINE(...) #__VA_ARGS__
Consumes everything between the parentheses.
Replaces any number of consecutive whitespace characters by a single space.
输入多行字符串的一种可能方便的方法是使用宏。 仅当引号和括号平衡并且不包含“顶级”逗号时,这才有效:
使用 gcc 4.6 或 g++ 4.6 编译,这会产生:
[[使用此技巧(,),您不需要使用引号。 尽管换行符和多个空格将被单个空格替换。]]
请注意,
,
不能出现在字符串中,除非它包含在括号或引号内。 单引号是可能的,但会产生编译器警告。编辑:如评论中所述,
#define MULTI_LINE_STRING(...) #__VA_ARGS__
允许使用,
。A probably convenient way to enter multi-line strings is by using macro's. This only works if quotes and parentheses are balanced and it does not contain 'top level' comma's:
Compiled with gcc 4.6 or g++ 4.6, this produces:
[[Using this trick(,) you don't need to use quotes. Though newlines and multiple white spaces will be replaced by a single whitespace.]]
Note that the
,
cannot be in the string, unless it is contained within parenthesis or quotes. Single quotes is possible, but creates compiler warnings.Edit: As mentioned in the comments,
#define MULTI_LINE_STRING(...) #__VA_ARGS__
allows the use of,
.你可以这样做:
You can just do this:
只是为了澄清一下 @unwind 的答案中 @emsr 的评论,如果一个人没有足够的运气拥有 C++11 编译器(例如 GCC 4.2.1),并且想要在字符串中嵌入换行符(char *或类字符串),可以写这样的东西:
非常明显,正确,但是当我第一次读到这篇文章时,@emsr 的简短评论并没有引起我的注意,所以我必须自己发现这一点。 希望我已经为其他人节省了几分钟。
Just to elucidate a bit on @emsr's comment in @unwind's answer, if one is not fortunate enough to have a C++11 compiler (say GCC 4.2.1), and one wants to embed the newlines in the string (either char * or class string), one can write something like this:
Very obvious, true, but @emsr's short comment didn't jump out at me when I read this the first time, so I had to discover this for myself. Hopefully, I've saved someone else a few minutes.
由于一盎司的经验胜过大量的理论,因此我尝试了一个
MULTILINE
的小测试程序:使用
cpp -P -std=c++11 filename
编译此片段重现。#__VA_ARGS__
背后的技巧是__VA_ARGS__
不处理逗号分隔符。 因此您可以将其传递给字符串化运算符。 前导和尾随空格被修剪,然后单词之间的空格(包括换行符)被压缩为单个空格。 括号需要平衡。 我认为这些缺点解释了为什么 C++11 的设计者尽管有#__VA_ARGS__
仍然看到了对原始字符串文字的需求。Since an ounce of experience is worth a ton of theory, I tried a little test program for
MULTILINE
:Compile this fragment with
cpp -P -std=c++11 filename
to reproduce.The trick behind
#__VA_ARGS__
is that__VA_ARGS__
does not process the comma separator. So you can pass it to the stringizing operator. Leading and trailing spaces are trimmed, and spaces (including newlines) between words are compressed to a single space then. Parentheses need to be balanced. I think these shortcomings explain why the designers of C++11, despite#__VA_ARGS__
, saw the need for raw string literals.选项 1. 使用 Boost 库,您可以如下声明字符串:
选项 2. 如果您的项目中没有 Boost,您可以在现代 C++ 中使用
std::string_view()
。Option 1. Using Boost library, you can declare the string as below:
Option 2. If Boost is not available in your project, you can use
std::string_view()
in modern C++.