Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String literals C++?

Tags:

c++

string

I need to do the following C# code in C++ (if possible). I have to const a long string with lots of freaking quotes and other stuff in it.

const String _literal = @"I can use "quotes" inside here";
like image 810
Bad Man Avatar asked Apr 08 '10 03:04

Bad Man


2 Answers

That is not available in C++03 (the current standard).

That is part of the C++0x draft standard but that's not readily available just yet.

For now, you just have to quote it explicitly:

const std::string _literal = "I have to escape my quotes in \"C++03\"";

Once C++0x becomes reality, you'll be able to write:

const std::string _literal = R"(but "C++0x" has raw string literals)";

and when you need )" in your literal:

const std::string _literal = R"DELIM(more "(raw string)" fun)DELIM";
like image 181
R Samuel Klatchko Avatar answered Oct 18 '22 10:10

R Samuel Klatchko


There is no equivalent of C#'s "@" in C++. The only way to achieve it is to escape the string properly:

const char *_literal = "I can use \"quotes\" inside here";
like image 33
SoapBox Avatar answered Oct 18 '22 09:10

SoapBox