diff --git a/CPP11.md b/CPP11.md index cab5323..822eb53 100644 --- a/CPP11.md +++ b/CPP11.md @@ -35,6 +35,7 @@ C++11 includes the following new language features: - [trailing return types](#trailing-return-types) - [noexcept specifier](#noexcept-specifier) - [char32_t and char16_t](#char32_t-and-char16_t) +- [raw string literals](#raw-string-literals) C++11 includes the following new library features: - [std::move](#stdmove) @@ -696,6 +697,27 @@ char32_t utf8_str[] = U"\u0123"; char16_t utf8_str[] = u"\u0123"; ``` +### Raw string literals +C++11 introduces a new way to declare string literals as "raw string literals". Characters issued from an escape sequence (tabs, line feeds, single backslashes, etc.) can be inputted raw while preserving formatting. This is useful, for example, to write literary text, which might contain a lot of quotes or special formatting. This can make your string literals easier to read and maintain. + +A raw string literal is declared using the following syntax: +``` +R"delimiter(raw_characters)delimiter" +``` +where: +* `delimiter` is an optional sequence of characters made of any source character except parentheses, backslashes and spaces. +* `raw_characters` is any raw character sequence; must not contain the closing sequence `")delimiter"`. + +Example: +```cpp +// msg1 and msg2 are equivalent. +const char* msg1 = "\nHello,\n\tworld!\n"; +const char* msg2 = R"( +Hello, + world! +)"; +``` + ## C++11 Library Features ### std::move diff --git a/README.md b/README.md index b7c4056..d602f1a 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ C++11 includes the following new language features: - [trailing return types](#trailing-return-types) - [noexcept specifier](#noexcept-specifier) - [char32_t and char16_t](#char32_t-and-char16_t) +- [raw string literals](#raw-string-literals) C++11 includes the following new library features: - [std::move](#stdmove) @@ -1932,6 +1933,27 @@ char32_t utf8_str[] = U"\u0123"; char16_t utf8_str[] = u"\u0123"; ``` +### Raw string literals +C++11 introduces a new way to declare string literals as "raw string literals". Characters issued from an escape sequence (tabs, line feeds, single backslashes, etc.) can be inputted raw while preserving formatting. This is useful, for example, to write literary text, which might contain a lot of quotes or special formatting. This can make your string literals easier to read and maintain. + +A raw string literal is declared using the following syntax: +``` +R"delimiter(raw_characters)delimiter" +``` +where: +* `delimiter` is an optional sequence of characters made of any source character except parentheses, backslashes and spaces. +* `raw_characters` is any raw character sequence; must not contain the closing sequence `")delimiter"`. + +Example: +```cpp +// msg1 and msg2 are equivalent. +const char* msg1 = "\nHello,\n\tworld!\n"; +const char* msg2 = R"( +Hello, + world! +)"; +``` + ## C++11 Library Features ### std::move