24

What is the C++ equivalent of the C# @ symbol prefixing strings? For escaping symbols automatically?

Example: var howManySlashesAreThereIn = @"\\\\\\";

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Jake
  • 11,273
  • 21
  • 90
  • 147
  • possible duplicate of [String literals C++?](http://stackoverflow.com/questions/2597322/string-literals-c) – user541686 May 09 '12 at 04:53
  • @Jake - Gotcha! Thanx for the clarification. The .Net term is "verbatim string literal", and the purpose is so that "\" isn't treated as an escape character: http://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx. There is no equivalent in either Java or current versions of C++ (and no, I *don't* consider C++11 a "current version" ;)) – paulsm4 May 09 '12 at 05:52

3 Answers3

38

In C++11, you can use raw string literals:

std::string s = R"(This\is\a\raw\string\literal)";

std::string s = R"*(This \one contains \a )", which would normally end the string)*";

Here's the C++11 FAQ word on it, and a reference.

chris
  • 60,560
  • 13
  • 143
  • 205
9

You're looking for the "raw string" feature of C++ but it's a fairly recent addition (C++11, I believe).

std::string howManySlashesAreThereIn = R"(\\\\\\)";

It's documented in 2.14.5 String lieterals of the C++11 standard, though that tome may be a little dry for you, so I'd suggest googling up on "raw string" c++.

As well as getting rid of those escape doubling monstrosites like "\\\\nasbox\\sharename\\downloads\\super_sekrit_stuff", it also allows you to bypass the "adding \n characters and combining adjacent strings" trick such as turning:

htmlString =
    "<HTML>\n"
      "<HEAD>\n"
        "<TITLE>My page</TITLE>\n"
        "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\">\n"
      "</HEAD>\n"
      "<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n"
        "<P> </P>\n"
        "<PRE>\n";

into something more readable (not exactly the same due to spaces in the second variant, but perfectly okay for HTML since it ignores the added spaces in this case):

htmlString = R"xyzzy(
    <HTML>
      <HEAD>
        <TITLE>My page</TITLE>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
      </HEAD>
      <BODY LINK="#0000ff" VLINK="#800080" BGCOLOR="#ffffff">
        <P> </P>
        <PRE>
    )xyzzy";
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • This is old but it's supposed to be `R"( ... )"` in your last example, and it probably shouldn't have a backslash since it's a raw string. – Rapptz Oct 02 '13 at 01:38
  • The first version as written won't have the indentation that the second version shows. You could move all the left quotes in the old-fashioned version to the left to accumulate white space if you want indentation. Raw strings are easier to get right. – emsr Nov 20 '14 at 19:17
4

C++11 adds raw string literals that are at least somewhat similar.

R"(This is a raw literal)";

These are particularly useful for regexes, like:

R"@(\w+\d*)@"

...which would, as a traditional literal would be:

"\\w+\\d*"

Although the difference isn't huge, it can make a difference, especially in a longer regex.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111