在c++ 11中,原始字符串字面值可以有多行

In C++11, can raw string literals have multiple lines?

本文关键字:字面值 字符串 c++ 原始      更新时间:2023-10-16

这在c++ 11下合法吗?

string s = R"(This is the first line
And this is the second line)";

…相当于:

string s = "This is the first linenAnd this is the second line";

是的,这完全有效。看到这里。

同时,从(草案)标准2.14.5/4:

原始字符串字面值中的源文件new-line将产生一个新行在结果执行中字符串字面值。的行开始处没有空白下面的例子,断言将成功:

const char *p = R"(a
b
c)";
assert(std::strcmp(p, "a\nbnc") == 0);