1

For example, for copy/paste purposes, it is very convenient to write :

C#

@"
SELECT * FROM ......
WHERE X = (SELECT Y FROM .....)
    AND M IN ('1','2','3')
";

or

Python:

"""
SELECT * FROM ......
WHERE X = (SELECT Y FROM .....)
    AND M IN ('1','2','3')
"""

than:

C++

"SELECT * FROM ...... \
WHERE X = (SELECT Y FROM .....) \
    AND M IN ('1','2','3')"

Is there any way to avoid the \ style in C++ and approach to C# or python style?

Thanks.

Tristan
  • 351
  • 2
  • 4
  • 15
  • Raw SQL literals are always a bad idea. – Puppy Jul 27 '12 at 02:04
  • possible duplicate of [Is there a way to delimit strings in C++ like PHP's Heredoc Syntax?](http://stackoverflow.com/questions/1348847/is-there-a-way-to-delimit-strings-in-c-like-phps-heredoc-syntax) – John Carter Jul 27 '12 at 02:09
  • By the answers and recommendations I received, -and thanks for that!- it seems to be impossible for C++ to emulate the C# or python styles such as I described. :-( – Tristan Jul 27 '12 at 02:24
  • @Tristan: In C++11 there are raw string literals which you can use. However, I don't know what compiler you are using as not all compilers support it. – Jesse Good Jul 27 '12 at 02:27
  • Another dupe - with a raw string literal example: http://stackoverflow.com/a/5460235/8331 – John Carter Jul 27 '12 at 23:24

2 Answers2

3

C++ (as well as C) automatically concatenates adjacent string literals. There's no need for \ with string literals. E.g. this

const char *p = "Hello" "World"   
  "!";

is equivalent to

const char *p = "HelloWorld!";

In your case you can simply do

"SELECT * FROM ...... "
"WHERE X = (SELECT Y FROM .....) "
"    AND M IN ('1','2','3')"

and the result will be equivalent to your original version with \, i.e. one continuous string literal will be produced.

This does not insert linebreaks or any other additional whitespace between the concatenated segments though. You have to remember to do it yourself, if you need something like that.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Thanks Andrey, the problem here (in this case) is that I can not copy/paste the sql command and to inmediately execute it, I must first delete the intermediate quotes. That was the spirit of my question. – Tristan Jul 27 '12 at 02:08
  • 1
    It would be hard to argue that this way is somehow better than using backslashes: instead of one extra character at the end you must now add two characters. But I guess it does count as a *different* way of doing it. – Sergey Kalinichenko Jul 27 '12 at 02:09
  • @dasblinkenlight: at least you don't have to make sure the backslash ist the last character on the line -- – eudoxos Jul 27 '12 at 02:19
  • @Tristan: Yeah... If you are looking for a way to just paste a block of text without having to do any per-line post-processing, then no, standard C++ does not have anything suitable. – AnT stands with Russia Jul 27 '12 at 02:55
  • 1
    @AndreyT: It depends on which version of the standard you are using. C++11 introduces raw string literals which allow newlines. – dreamlax Jul 27 '12 at 03:57
1

I think C++11 allows newlines in raw string literals, e.g.:

const char *query = R"(SELECT * FROM ...... 
                       WHERE X = (SELECT Y FROM .....) 
                       AND M IN ('1','2','3'))";

Each newline in the source should result in a newline in the execution string-literal. Raw string literals generally take the form of R"( ... )". For GCC this requires at least version 4.5, and for clang++, it requires version 3.0. Alternatively, you can use a custom delimiter to make it easier to disambiguate the end of the string:

const char *query = R"XXXX(SELECT * FROM ...... 
                       WHERE X = (SELECT Y FROM .....) 
                       AND M IN ('1','2','3'))XXXX";
dreamlax
  • 93,976
  • 29
  • 161
  • 209