yohhoyの日記

技術的メモをしていきたい日記

生文字リテラル中の改行コード

C++11で導入された生文字列リテラル(raw string literal)に含まれる改行は、ソースコードファイルの改行コードによらず'\n'(LF)として扱われる。

std::string s = R"(abc
xyz)";
assert(s == "abc\nxyz");

C++11 2.14.5/p4より引用。

[Note: A source-file new-line in a raw string literal results in a new-line in the resulting execution string-literal. Assuming no whitespace at the beginning of lines in the following example, the assert will succeed:

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

-- end note]

関連URL