C++ multiline string literal












347















Is there any way to have multi-line plain-text, constant literals in C++, à la Perl? Maybe some parsing trick with #includeing a file? I can't think of one, but boy, that would be nice. I know it'll be in C++0x.










share|improve this question




















  • 1





    Generally you don't want to embed string literals into code. For I18N and L10N it is preferable to put string literals into a configuration file that is loaded at run time.

    – Martin York
    Jul 16 '09 at 7:09






  • 37





    There are enough cases where putting string literals into code is not a problem: if the string isn't used to represent it to the user; i.e.: SQL statements, file names, registry key names,command lines to be executed, ...

    – mmmmmmmm
    Jul 16 '09 at 7:18






  • 1





    @Martin: It can still be useful to know, however. I've done it to break up complex regexs, for example.

    – Boojum
    Jul 16 '09 at 7:22
















347















Is there any way to have multi-line plain-text, constant literals in C++, à la Perl? Maybe some parsing trick with #includeing a file? I can't think of one, but boy, that would be nice. I know it'll be in C++0x.










share|improve this question




















  • 1





    Generally you don't want to embed string literals into code. For I18N and L10N it is preferable to put string literals into a configuration file that is loaded at run time.

    – Martin York
    Jul 16 '09 at 7:09






  • 37





    There are enough cases where putting string literals into code is not a problem: if the string isn't used to represent it to the user; i.e.: SQL statements, file names, registry key names,command lines to be executed, ...

    – mmmmmmmm
    Jul 16 '09 at 7:18






  • 1





    @Martin: It can still be useful to know, however. I've done it to break up complex regexs, for example.

    – Boojum
    Jul 16 '09 at 7:22














347












347








347


71






Is there any way to have multi-line plain-text, constant literals in C++, à la Perl? Maybe some parsing trick with #includeing a file? I can't think of one, but boy, that would be nice. I know it'll be in C++0x.










share|improve this question
















Is there any way to have multi-line plain-text, constant literals in C++, à la Perl? Maybe some parsing trick with #includeing a file? I can't think of one, but boy, that would be nice. I know it'll be in C++0x.







c++ string-literals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 8 '13 at 12:36









kokociel

147412




147412










asked Jul 16 '09 at 6:55









rlbondrlbond

38.8k41149207




38.8k41149207








  • 1





    Generally you don't want to embed string literals into code. For I18N and L10N it is preferable to put string literals into a configuration file that is loaded at run time.

    – Martin York
    Jul 16 '09 at 7:09






  • 37





    There are enough cases where putting string literals into code is not a problem: if the string isn't used to represent it to the user; i.e.: SQL statements, file names, registry key names,command lines to be executed, ...

    – mmmmmmmm
    Jul 16 '09 at 7:18






  • 1





    @Martin: It can still be useful to know, however. I've done it to break up complex regexs, for example.

    – Boojum
    Jul 16 '09 at 7:22














  • 1





    Generally you don't want to embed string literals into code. For I18N and L10N it is preferable to put string literals into a configuration file that is loaded at run time.

    – Martin York
    Jul 16 '09 at 7:09






  • 37





    There are enough cases where putting string literals into code is not a problem: if the string isn't used to represent it to the user; i.e.: SQL statements, file names, registry key names,command lines to be executed, ...

    – mmmmmmmm
    Jul 16 '09 at 7:18






  • 1





    @Martin: It can still be useful to know, however. I've done it to break up complex regexs, for example.

    – Boojum
    Jul 16 '09 at 7:22








1




1





Generally you don't want to embed string literals into code. For I18N and L10N it is preferable to put string literals into a configuration file that is loaded at run time.

– Martin York
Jul 16 '09 at 7:09





Generally you don't want to embed string literals into code. For I18N and L10N it is preferable to put string literals into a configuration file that is loaded at run time.

– Martin York
Jul 16 '09 at 7:09




37




37





There are enough cases where putting string literals into code is not a problem: if the string isn't used to represent it to the user; i.e.: SQL statements, file names, registry key names,command lines to be executed, ...

– mmmmmmmm
Jul 16 '09 at 7:18





There are enough cases where putting string literals into code is not a problem: if the string isn't used to represent it to the user; i.e.: SQL statements, file names, registry key names,command lines to be executed, ...

– mmmmmmmm
Jul 16 '09 at 7:18




1




1





@Martin: It can still be useful to know, however. I've done it to break up complex regexs, for example.

– Boojum
Jul 16 '09 at 7:22





@Martin: It can still be useful to know, however. I've done it to break up complex regexs, for example.

– Boojum
Jul 16 '09 at 7:22












7 Answers
7






active

oldest

votes


















504














Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler:



const char *text =
"This text is pretty long, but will be "
"concatenated into just a single string. "
"The disadvantage is that you have to quote "
"each part, and newlines must be literal as "
"usual.";


The indentation doesn't matter, since it's not inside the quotes.



You can also do this, as long as you take care to escape the embedded newline. Failure to do so, like my first answer did, will not compile:




const char *text2 =
"Here, on the other hand, I've gone crazy
and really let the literal span several lines,
without bothering with quoting each line's
content. This works, but you can't indent.";


Again, note those backslashes at the end of each line, they must be immediately before the line ends, they are escaping the newline in the source, so that everything acts as if the newline wasn't there. You don't get newlines in the string at the locations where you had backslashes. With this form, you obviously can't indent the text since the indentation would then become part of the string, garbling it with random spaces.






share|improve this answer





















  • 2





    I have been told in the past that the first option can be up to implementation, however I've yet to find a compiler that doesn't honor that syntax.

    – Jason Mock
    Oct 22 '10 at 18:48






  • 24





    @Jason: it was not necessarily a part of pre-C89 compilers, but it is defined in C89 and therefore is supported essentially everywhere.

    – Jonathan Leffler
    Apr 4 '11 at 14:25






  • 3





    Also, if you really want the string formatted on multiple lines in c++98 just substitute n for the terminating space on each quoted string fragment. C++11 raw literals are still my favorite.

    – emsr
    Sep 22 '11 at 3:46






  • 3





    @unwind Note that newline at the end of source line is not made part of the string, it's just skipped. If you want a newline as part of the string, you need to have n at the end of the line.

    – hyde
    Apr 21 '13 at 7:39








  • 1





    There's nasty bug in Microsoft Visual Studio. If you use backslashes at the end of lines, then it automatically indents the text inside the string.

    – palota
    Jan 19 '14 at 21:18



















302














In C++11 you have raw string literals. Sort of like here-text in shells and script languages like Python and Perl and Ruby.



const char * vogon_poem = R"V0G0N(
O freddled gruntbuggly thy micturations are to me
As plured gabbleblochits on a lurgid bee.
Groop, I implore thee my foonting turlingdromes.
And hooptiously drangle me with crinkly bindlewurdles,
Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.

(by Prostetnic Vogon Jeltz; see p. 56/57)
)V0G0N";


All the spaces and indentation and the newlines in the string are preserved.



These can also be utf-8|16|32 or wchar_t (with the usual prefixes).



I should point out that the escape sequence, V0G0N, is not actually needed here. Its presence would allow putting )" inside the string. In other words, I could have put



                "(by Prostetnic Vogon Jeltz; see p. 56/57)"


(note extra quotes) and the string above would still be correct. Otherwise I could just as well have used



const char * vogon_poem = R"( ... )";


The parens just inside the quotes are still needed.






share|improve this answer





















  • 16





    This is really what I want, the ability to avoid quotes, backslash-Ns, escapes, and still have newlines appear in the actual string. This is handy for embedded code (e.g. shaders or Lua). Unfortunately, we aren't all using C++-0x yet. :-(

    – mlepage
    Jul 18 '12 at 14:40






  • 1





    I was considering this for embedded SQL and Python scripts myself. I was hoping for your sake if maybe gcc would let it slide through in C++98 mode but, alas, no.

    – emsr
    Jul 18 '12 at 15:16






  • 3





    I am more used to clang and gcc. In thise compilers you have to set a flag for C++0x or c++11. Lookin n a MS website it looks like they don't have raw literals yet. i understand that MS will release new compiler updates more quickly as C++ features get implemented. Look for Visual C++ Compiler November 2012 CTP [microsoft.com/en-us/download/details.aspx?id=35515] for the latest bleeding edge.

    – emsr
    Jan 31 '13 at 1:17






  • 4





    This should be the accepted answer. This rules.

    – Alex Gray
    Feb 18 '16 at 15:45






  • 3





    @rsethc Just use #if 0#endif to comment out blocks of code. Nests too.

    – bobbogo
    Sep 12 '17 at 17:23





















25














#define MULTILINE(...) #__VA_ARGS__

Consumes everything between the parentheses.

Replaces any number of consecutive whitespace characters by a single space.






share|improve this answer



















  • 1





    You can add n if you need newlines

    – Simon
    Jul 1 '13 at 14:11











  • Note that ` (and hence n) is copied literally, but "` is converted into ". So MULTILINE(1, "2" 3) yields "1, "2" 3".

    – Andreas Spindler
    Sep 11 '13 at 12:58













  • @AndreasSpindler Quotes and backslashes alike are escaped by (additional) backslashes as long as they appear inside a string or character literal token. Not sure what's your point. It is illegal to have an unmatched quote (double or single), so contractions don't work, or an odd number of them anyway, which is probably the biggest downside. +1 anyway. "Real programmers" always use contractions in pairs with no intervening newline so the single quotes balance.

    – Potatoswatter
    Oct 14 '13 at 4:28













  • The point is that he wrote "consumes everything between parentheses".

    – Andreas Spindler
    Oct 14 '13 at 14:33



















23














A probably convenient way to enter multi-line strings is by using macro's. This only works if quotes and parentheses are balanced and it does not contain 'top level' comma's:



#define MULTI_LINE_STRING(a) #a
const char *text = MULTI_LINE_STRING(
Using this trick(,) you don't need to use quotes.
Though newlines and multiple white spaces
will be replaced by a single whitespace.
);
printf("[[%s]]n",text);


Compiled with gcc 4.6 or g++ 4.6, this produces: [[Using this trick(,) you don't need to use quotes. Though newlines and multiple white spaces will be replaced by a single whitespace.]]



Note that the , cannot be in the string, unless it is contained within parenthesis or quotes. Single quotes is possible, but creates compiler warnings.



Edit: As mentioned in the comments, #define MULTI_LINE_STRING(...) #__VA_ARGS__ allows the use of ,.






share|improve this answer


























  • For a project in which I wanted to include some lua code snippets in c++, I ended up writing a small python script, in which I entered the multiline strings, and let that generate a c++ source file.

    – bcmpinc
    Jul 16 '12 at 16:39











  • Perfect for me, adding a huge multi-line float-list string from a collada file for unit testing. I didn't fancy putting quotes everywhere, I needed a copy&paste solution.

    – Soylent Graham
    Aug 1 '12 at 11:39






  • 6





    You can use #define MULTILINE(...) #__VA_ARGS__ if you want your string to contain commas.

    – Simon
    Jul 1 '13 at 14:09






  • 2





    Note this will strip out most extra whitesapce (including all n and r), which is kind of handy for some cases and fatal for others.

    – BCS
    Oct 8 '13 at 21:35



















12














You can just do this:



const char *text = "This is my string it is "
"very long";





share|improve this answer

































    9














    Since an ounce of experience is worth a ton of theory, I tried a little test program for MULTILINE:



    #define MULTILINE(...) #__VA_ARGS__

    const char *mstr =
    {
    MULTILINE(1, 2, 3), // "1, 2, 3"
    MULTILINE(1,2,3), // "1,2,3"
    MULTILINE(1 , 2 , 3), // "1 , 2 , 3"
    MULTILINE( 1 , 2 , 3 ), // "1 , 2 , 3"
    MULTILINE((1, 2, 3)), // "(1, 2, 3)"
    MULTILINE(1
    2
    3), // "1 2 3"
    MULTILINE(1n2n3n), // "1n2n3n"
    MULTILINE(1n
    2n
    3n), // "1n 2n 3n"
    MULTILINE(1, "2" 3) // "1, "2" 3"
    };


    Compile this fragment with cpp -P -std=c++11 filename to reproduce.



    The trick behind #__VA_ARGS__ is that __VA_ARGS__ does not process the comma separator. So you can pass it to the stringizing operator. Leading and trailing spaces are trimmed, and spaces (including newlines) between words are compressed to a single space then. Parentheses need to be balanced. I think these shortcomings explain why the designers of C++11, despite #__VA_ARGS__, saw the need for raw string literals.






    share|improve this answer

































      7














      Just to elucidate a bit on @emsr's comment in @unwind's answer, if one is not fortunate enough to have a C++11 compiler (say GCC 4.2.1), and one wants to embed the newlines in the string (either char * or class string), one can write something like this:



      const char *text =
      "This text is pretty long, but will ben"
      "concatenated into just a single string.n"
      "The disadvantage is that you have to quoten"
      "each part, and newlines must be literal asn"
      "usual.";


      Very obvious, true, but @emsr's short comment didn't jump out at me when I read this the first time, so I had to discover this for myself. Hopefully, I've saved someone else a few minutes.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1135841%2fc-multiline-string-literal%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        504














        Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler:



        const char *text =
        "This text is pretty long, but will be "
        "concatenated into just a single string. "
        "The disadvantage is that you have to quote "
        "each part, and newlines must be literal as "
        "usual.";


        The indentation doesn't matter, since it's not inside the quotes.



        You can also do this, as long as you take care to escape the embedded newline. Failure to do so, like my first answer did, will not compile:




        const char *text2 =
        "Here, on the other hand, I've gone crazy
        and really let the literal span several lines,
        without bothering with quoting each line's
        content. This works, but you can't indent.";


        Again, note those backslashes at the end of each line, they must be immediately before the line ends, they are escaping the newline in the source, so that everything acts as if the newline wasn't there. You don't get newlines in the string at the locations where you had backslashes. With this form, you obviously can't indent the text since the indentation would then become part of the string, garbling it with random spaces.






        share|improve this answer





















        • 2





          I have been told in the past that the first option can be up to implementation, however I've yet to find a compiler that doesn't honor that syntax.

          – Jason Mock
          Oct 22 '10 at 18:48






        • 24





          @Jason: it was not necessarily a part of pre-C89 compilers, but it is defined in C89 and therefore is supported essentially everywhere.

          – Jonathan Leffler
          Apr 4 '11 at 14:25






        • 3





          Also, if you really want the string formatted on multiple lines in c++98 just substitute n for the terminating space on each quoted string fragment. C++11 raw literals are still my favorite.

          – emsr
          Sep 22 '11 at 3:46






        • 3





          @unwind Note that newline at the end of source line is not made part of the string, it's just skipped. If you want a newline as part of the string, you need to have n at the end of the line.

          – hyde
          Apr 21 '13 at 7:39








        • 1





          There's nasty bug in Microsoft Visual Studio. If you use backslashes at the end of lines, then it automatically indents the text inside the string.

          – palota
          Jan 19 '14 at 21:18
















        504














        Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler:



        const char *text =
        "This text is pretty long, but will be "
        "concatenated into just a single string. "
        "The disadvantage is that you have to quote "
        "each part, and newlines must be literal as "
        "usual.";


        The indentation doesn't matter, since it's not inside the quotes.



        You can also do this, as long as you take care to escape the embedded newline. Failure to do so, like my first answer did, will not compile:




        const char *text2 =
        "Here, on the other hand, I've gone crazy
        and really let the literal span several lines,
        without bothering with quoting each line's
        content. This works, but you can't indent.";


        Again, note those backslashes at the end of each line, they must be immediately before the line ends, they are escaping the newline in the source, so that everything acts as if the newline wasn't there. You don't get newlines in the string at the locations where you had backslashes. With this form, you obviously can't indent the text since the indentation would then become part of the string, garbling it with random spaces.






        share|improve this answer





















        • 2





          I have been told in the past that the first option can be up to implementation, however I've yet to find a compiler that doesn't honor that syntax.

          – Jason Mock
          Oct 22 '10 at 18:48






        • 24





          @Jason: it was not necessarily a part of pre-C89 compilers, but it is defined in C89 and therefore is supported essentially everywhere.

          – Jonathan Leffler
          Apr 4 '11 at 14:25






        • 3





          Also, if you really want the string formatted on multiple lines in c++98 just substitute n for the terminating space on each quoted string fragment. C++11 raw literals are still my favorite.

          – emsr
          Sep 22 '11 at 3:46






        • 3





          @unwind Note that newline at the end of source line is not made part of the string, it's just skipped. If you want a newline as part of the string, you need to have n at the end of the line.

          – hyde
          Apr 21 '13 at 7:39








        • 1





          There's nasty bug in Microsoft Visual Studio. If you use backslashes at the end of lines, then it automatically indents the text inside the string.

          – palota
          Jan 19 '14 at 21:18














        504












        504








        504







        Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler:



        const char *text =
        "This text is pretty long, but will be "
        "concatenated into just a single string. "
        "The disadvantage is that you have to quote "
        "each part, and newlines must be literal as "
        "usual.";


        The indentation doesn't matter, since it's not inside the quotes.



        You can also do this, as long as you take care to escape the embedded newline. Failure to do so, like my first answer did, will not compile:




        const char *text2 =
        "Here, on the other hand, I've gone crazy
        and really let the literal span several lines,
        without bothering with quoting each line's
        content. This works, but you can't indent.";


        Again, note those backslashes at the end of each line, they must be immediately before the line ends, they are escaping the newline in the source, so that everything acts as if the newline wasn't there. You don't get newlines in the string at the locations where you had backslashes. With this form, you obviously can't indent the text since the indentation would then become part of the string, garbling it with random spaces.






        share|improve this answer















        Well ... Sort of. The easiest is to just use the fact that adjacent string literals are concatenated by the compiler:



        const char *text =
        "This text is pretty long, but will be "
        "concatenated into just a single string. "
        "The disadvantage is that you have to quote "
        "each part, and newlines must be literal as "
        "usual.";


        The indentation doesn't matter, since it's not inside the quotes.



        You can also do this, as long as you take care to escape the embedded newline. Failure to do so, like my first answer did, will not compile:




        const char *text2 =
        "Here, on the other hand, I've gone crazy
        and really let the literal span several lines,
        without bothering with quoting each line's
        content. This works, but you can't indent.";


        Again, note those backslashes at the end of each line, they must be immediately before the line ends, they are escaping the newline in the source, so that everything acts as if the newline wasn't there. You don't get newlines in the string at the locations where you had backslashes. With this form, you obviously can't indent the text since the indentation would then become part of the string, garbling it with random spaces.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 15 '17 at 12:01

























        answered Jul 16 '09 at 7:00









        unwindunwind

        323k52396527




        323k52396527








        • 2





          I have been told in the past that the first option can be up to implementation, however I've yet to find a compiler that doesn't honor that syntax.

          – Jason Mock
          Oct 22 '10 at 18:48






        • 24





          @Jason: it was not necessarily a part of pre-C89 compilers, but it is defined in C89 and therefore is supported essentially everywhere.

          – Jonathan Leffler
          Apr 4 '11 at 14:25






        • 3





          Also, if you really want the string formatted on multiple lines in c++98 just substitute n for the terminating space on each quoted string fragment. C++11 raw literals are still my favorite.

          – emsr
          Sep 22 '11 at 3:46






        • 3





          @unwind Note that newline at the end of source line is not made part of the string, it's just skipped. If you want a newline as part of the string, you need to have n at the end of the line.

          – hyde
          Apr 21 '13 at 7:39








        • 1





          There's nasty bug in Microsoft Visual Studio. If you use backslashes at the end of lines, then it automatically indents the text inside the string.

          – palota
          Jan 19 '14 at 21:18














        • 2





          I have been told in the past that the first option can be up to implementation, however I've yet to find a compiler that doesn't honor that syntax.

          – Jason Mock
          Oct 22 '10 at 18:48






        • 24





          @Jason: it was not necessarily a part of pre-C89 compilers, but it is defined in C89 and therefore is supported essentially everywhere.

          – Jonathan Leffler
          Apr 4 '11 at 14:25






        • 3





          Also, if you really want the string formatted on multiple lines in c++98 just substitute n for the terminating space on each quoted string fragment. C++11 raw literals are still my favorite.

          – emsr
          Sep 22 '11 at 3:46






        • 3





          @unwind Note that newline at the end of source line is not made part of the string, it's just skipped. If you want a newline as part of the string, you need to have n at the end of the line.

          – hyde
          Apr 21 '13 at 7:39








        • 1





          There's nasty bug in Microsoft Visual Studio. If you use backslashes at the end of lines, then it automatically indents the text inside the string.

          – palota
          Jan 19 '14 at 21:18








        2




        2





        I have been told in the past that the first option can be up to implementation, however I've yet to find a compiler that doesn't honor that syntax.

        – Jason Mock
        Oct 22 '10 at 18:48





        I have been told in the past that the first option can be up to implementation, however I've yet to find a compiler that doesn't honor that syntax.

        – Jason Mock
        Oct 22 '10 at 18:48




        24




        24





        @Jason: it was not necessarily a part of pre-C89 compilers, but it is defined in C89 and therefore is supported essentially everywhere.

        – Jonathan Leffler
        Apr 4 '11 at 14:25





        @Jason: it was not necessarily a part of pre-C89 compilers, but it is defined in C89 and therefore is supported essentially everywhere.

        – Jonathan Leffler
        Apr 4 '11 at 14:25




        3




        3





        Also, if you really want the string formatted on multiple lines in c++98 just substitute n for the terminating space on each quoted string fragment. C++11 raw literals are still my favorite.

        – emsr
        Sep 22 '11 at 3:46





        Also, if you really want the string formatted on multiple lines in c++98 just substitute n for the terminating space on each quoted string fragment. C++11 raw literals are still my favorite.

        – emsr
        Sep 22 '11 at 3:46




        3




        3





        @unwind Note that newline at the end of source line is not made part of the string, it's just skipped. If you want a newline as part of the string, you need to have n at the end of the line.

        – hyde
        Apr 21 '13 at 7:39







        @unwind Note that newline at the end of source line is not made part of the string, it's just skipped. If you want a newline as part of the string, you need to have n at the end of the line.

        – hyde
        Apr 21 '13 at 7:39






        1




        1





        There's nasty bug in Microsoft Visual Studio. If you use backslashes at the end of lines, then it automatically indents the text inside the string.

        – palota
        Jan 19 '14 at 21:18





        There's nasty bug in Microsoft Visual Studio. If you use backslashes at the end of lines, then it automatically indents the text inside the string.

        – palota
        Jan 19 '14 at 21:18













        302














        In C++11 you have raw string literals. Sort of like here-text in shells and script languages like Python and Perl and Ruby.



        const char * vogon_poem = R"V0G0N(
        O freddled gruntbuggly thy micturations are to me
        As plured gabbleblochits on a lurgid bee.
        Groop, I implore thee my foonting turlingdromes.
        And hooptiously drangle me with crinkly bindlewurdles,
        Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.

        (by Prostetnic Vogon Jeltz; see p. 56/57)
        )V0G0N";


        All the spaces and indentation and the newlines in the string are preserved.



        These can also be utf-8|16|32 or wchar_t (with the usual prefixes).



        I should point out that the escape sequence, V0G0N, is not actually needed here. Its presence would allow putting )" inside the string. In other words, I could have put



                        "(by Prostetnic Vogon Jeltz; see p. 56/57)"


        (note extra quotes) and the string above would still be correct. Otherwise I could just as well have used



        const char * vogon_poem = R"( ... )";


        The parens just inside the quotes are still needed.






        share|improve this answer





















        • 16





          This is really what I want, the ability to avoid quotes, backslash-Ns, escapes, and still have newlines appear in the actual string. This is handy for embedded code (e.g. shaders or Lua). Unfortunately, we aren't all using C++-0x yet. :-(

          – mlepage
          Jul 18 '12 at 14:40






        • 1





          I was considering this for embedded SQL and Python scripts myself. I was hoping for your sake if maybe gcc would let it slide through in C++98 mode but, alas, no.

          – emsr
          Jul 18 '12 at 15:16






        • 3





          I am more used to clang and gcc. In thise compilers you have to set a flag for C++0x or c++11. Lookin n a MS website it looks like they don't have raw literals yet. i understand that MS will release new compiler updates more quickly as C++ features get implemented. Look for Visual C++ Compiler November 2012 CTP [microsoft.com/en-us/download/details.aspx?id=35515] for the latest bleeding edge.

          – emsr
          Jan 31 '13 at 1:17






        • 4





          This should be the accepted answer. This rules.

          – Alex Gray
          Feb 18 '16 at 15:45






        • 3





          @rsethc Just use #if 0#endif to comment out blocks of code. Nests too.

          – bobbogo
          Sep 12 '17 at 17:23


















        302














        In C++11 you have raw string literals. Sort of like here-text in shells and script languages like Python and Perl and Ruby.



        const char * vogon_poem = R"V0G0N(
        O freddled gruntbuggly thy micturations are to me
        As plured gabbleblochits on a lurgid bee.
        Groop, I implore thee my foonting turlingdromes.
        And hooptiously drangle me with crinkly bindlewurdles,
        Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.

        (by Prostetnic Vogon Jeltz; see p. 56/57)
        )V0G0N";


        All the spaces and indentation and the newlines in the string are preserved.



        These can also be utf-8|16|32 or wchar_t (with the usual prefixes).



        I should point out that the escape sequence, V0G0N, is not actually needed here. Its presence would allow putting )" inside the string. In other words, I could have put



                        "(by Prostetnic Vogon Jeltz; see p. 56/57)"


        (note extra quotes) and the string above would still be correct. Otherwise I could just as well have used



        const char * vogon_poem = R"( ... )";


        The parens just inside the quotes are still needed.






        share|improve this answer





















        • 16





          This is really what I want, the ability to avoid quotes, backslash-Ns, escapes, and still have newlines appear in the actual string. This is handy for embedded code (e.g. shaders or Lua). Unfortunately, we aren't all using C++-0x yet. :-(

          – mlepage
          Jul 18 '12 at 14:40






        • 1





          I was considering this for embedded SQL and Python scripts myself. I was hoping for your sake if maybe gcc would let it slide through in C++98 mode but, alas, no.

          – emsr
          Jul 18 '12 at 15:16






        • 3





          I am more used to clang and gcc. In thise compilers you have to set a flag for C++0x or c++11. Lookin n a MS website it looks like they don't have raw literals yet. i understand that MS will release new compiler updates more quickly as C++ features get implemented. Look for Visual C++ Compiler November 2012 CTP [microsoft.com/en-us/download/details.aspx?id=35515] for the latest bleeding edge.

          – emsr
          Jan 31 '13 at 1:17






        • 4





          This should be the accepted answer. This rules.

          – Alex Gray
          Feb 18 '16 at 15:45






        • 3





          @rsethc Just use #if 0#endif to comment out blocks of code. Nests too.

          – bobbogo
          Sep 12 '17 at 17:23
















        302












        302








        302







        In C++11 you have raw string literals. Sort of like here-text in shells and script languages like Python and Perl and Ruby.



        const char * vogon_poem = R"V0G0N(
        O freddled gruntbuggly thy micturations are to me
        As plured gabbleblochits on a lurgid bee.
        Groop, I implore thee my foonting turlingdromes.
        And hooptiously drangle me with crinkly bindlewurdles,
        Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.

        (by Prostetnic Vogon Jeltz; see p. 56/57)
        )V0G0N";


        All the spaces and indentation and the newlines in the string are preserved.



        These can also be utf-8|16|32 or wchar_t (with the usual prefixes).



        I should point out that the escape sequence, V0G0N, is not actually needed here. Its presence would allow putting )" inside the string. In other words, I could have put



                        "(by Prostetnic Vogon Jeltz; see p. 56/57)"


        (note extra quotes) and the string above would still be correct. Otherwise I could just as well have used



        const char * vogon_poem = R"( ... )";


        The parens just inside the quotes are still needed.






        share|improve this answer















        In C++11 you have raw string literals. Sort of like here-text in shells and script languages like Python and Perl and Ruby.



        const char * vogon_poem = R"V0G0N(
        O freddled gruntbuggly thy micturations are to me
        As plured gabbleblochits on a lurgid bee.
        Groop, I implore thee my foonting turlingdromes.
        And hooptiously drangle me with crinkly bindlewurdles,
        Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.

        (by Prostetnic Vogon Jeltz; see p. 56/57)
        )V0G0N";


        All the spaces and indentation and the newlines in the string are preserved.



        These can also be utf-8|16|32 or wchar_t (with the usual prefixes).



        I should point out that the escape sequence, V0G0N, is not actually needed here. Its presence would allow putting )" inside the string. In other words, I could have put



                        "(by Prostetnic Vogon Jeltz; see p. 56/57)"


        (note extra quotes) and the string above would still be correct. Otherwise I could just as well have used



        const char * vogon_poem = R"( ... )";


        The parens just inside the quotes are still needed.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 10 '17 at 2:34









        Drew Dormann

        41.9k979142




        41.9k979142










        answered Mar 28 '11 at 14:11









        emsremsr

        10.3k53556




        10.3k53556








        • 16





          This is really what I want, the ability to avoid quotes, backslash-Ns, escapes, and still have newlines appear in the actual string. This is handy for embedded code (e.g. shaders or Lua). Unfortunately, we aren't all using C++-0x yet. :-(

          – mlepage
          Jul 18 '12 at 14:40






        • 1





          I was considering this for embedded SQL and Python scripts myself. I was hoping for your sake if maybe gcc would let it slide through in C++98 mode but, alas, no.

          – emsr
          Jul 18 '12 at 15:16






        • 3





          I am more used to clang and gcc. In thise compilers you have to set a flag for C++0x or c++11. Lookin n a MS website it looks like they don't have raw literals yet. i understand that MS will release new compiler updates more quickly as C++ features get implemented. Look for Visual C++ Compiler November 2012 CTP [microsoft.com/en-us/download/details.aspx?id=35515] for the latest bleeding edge.

          – emsr
          Jan 31 '13 at 1:17






        • 4





          This should be the accepted answer. This rules.

          – Alex Gray
          Feb 18 '16 at 15:45






        • 3





          @rsethc Just use #if 0#endif to comment out blocks of code. Nests too.

          – bobbogo
          Sep 12 '17 at 17:23
















        • 16





          This is really what I want, the ability to avoid quotes, backslash-Ns, escapes, and still have newlines appear in the actual string. This is handy for embedded code (e.g. shaders or Lua). Unfortunately, we aren't all using C++-0x yet. :-(

          – mlepage
          Jul 18 '12 at 14:40






        • 1





          I was considering this for embedded SQL and Python scripts myself. I was hoping for your sake if maybe gcc would let it slide through in C++98 mode but, alas, no.

          – emsr
          Jul 18 '12 at 15:16






        • 3





          I am more used to clang and gcc. In thise compilers you have to set a flag for C++0x or c++11. Lookin n a MS website it looks like they don't have raw literals yet. i understand that MS will release new compiler updates more quickly as C++ features get implemented. Look for Visual C++ Compiler November 2012 CTP [microsoft.com/en-us/download/details.aspx?id=35515] for the latest bleeding edge.

          – emsr
          Jan 31 '13 at 1:17






        • 4





          This should be the accepted answer. This rules.

          – Alex Gray
          Feb 18 '16 at 15:45






        • 3





          @rsethc Just use #if 0#endif to comment out blocks of code. Nests too.

          – bobbogo
          Sep 12 '17 at 17:23










        16




        16





        This is really what I want, the ability to avoid quotes, backslash-Ns, escapes, and still have newlines appear in the actual string. This is handy for embedded code (e.g. shaders or Lua). Unfortunately, we aren't all using C++-0x yet. :-(

        – mlepage
        Jul 18 '12 at 14:40





        This is really what I want, the ability to avoid quotes, backslash-Ns, escapes, and still have newlines appear in the actual string. This is handy for embedded code (e.g. shaders or Lua). Unfortunately, we aren't all using C++-0x yet. :-(

        – mlepage
        Jul 18 '12 at 14:40




        1




        1





        I was considering this for embedded SQL and Python scripts myself. I was hoping for your sake if maybe gcc would let it slide through in C++98 mode but, alas, no.

        – emsr
        Jul 18 '12 at 15:16





        I was considering this for embedded SQL and Python scripts myself. I was hoping for your sake if maybe gcc would let it slide through in C++98 mode but, alas, no.

        – emsr
        Jul 18 '12 at 15:16




        3




        3





        I am more used to clang and gcc. In thise compilers you have to set a flag for C++0x or c++11. Lookin n a MS website it looks like they don't have raw literals yet. i understand that MS will release new compiler updates more quickly as C++ features get implemented. Look for Visual C++ Compiler November 2012 CTP [microsoft.com/en-us/download/details.aspx?id=35515] for the latest bleeding edge.

        – emsr
        Jan 31 '13 at 1:17





        I am more used to clang and gcc. In thise compilers you have to set a flag for C++0x or c++11. Lookin n a MS website it looks like they don't have raw literals yet. i understand that MS will release new compiler updates more quickly as C++ features get implemented. Look for Visual C++ Compiler November 2012 CTP [microsoft.com/en-us/download/details.aspx?id=35515] for the latest bleeding edge.

        – emsr
        Jan 31 '13 at 1:17




        4




        4





        This should be the accepted answer. This rules.

        – Alex Gray
        Feb 18 '16 at 15:45





        This should be the accepted answer. This rules.

        – Alex Gray
        Feb 18 '16 at 15:45




        3




        3





        @rsethc Just use #if 0#endif to comment out blocks of code. Nests too.

        – bobbogo
        Sep 12 '17 at 17:23







        @rsethc Just use #if 0#endif to comment out blocks of code. Nests too.

        – bobbogo
        Sep 12 '17 at 17:23













        25














        #define MULTILINE(...) #__VA_ARGS__

        Consumes everything between the parentheses.

        Replaces any number of consecutive whitespace characters by a single space.






        share|improve this answer



















        • 1





          You can add n if you need newlines

          – Simon
          Jul 1 '13 at 14:11











        • Note that ` (and hence n) is copied literally, but "` is converted into ". So MULTILINE(1, "2" 3) yields "1, "2" 3".

          – Andreas Spindler
          Sep 11 '13 at 12:58













        • @AndreasSpindler Quotes and backslashes alike are escaped by (additional) backslashes as long as they appear inside a string or character literal token. Not sure what's your point. It is illegal to have an unmatched quote (double or single), so contractions don't work, or an odd number of them anyway, which is probably the biggest downside. +1 anyway. "Real programmers" always use contractions in pairs with no intervening newline so the single quotes balance.

          – Potatoswatter
          Oct 14 '13 at 4:28













        • The point is that he wrote "consumes everything between parentheses".

          – Andreas Spindler
          Oct 14 '13 at 14:33
















        25














        #define MULTILINE(...) #__VA_ARGS__

        Consumes everything between the parentheses.

        Replaces any number of consecutive whitespace characters by a single space.






        share|improve this answer



















        • 1





          You can add n if you need newlines

          – Simon
          Jul 1 '13 at 14:11











        • Note that ` (and hence n) is copied literally, but "` is converted into ". So MULTILINE(1, "2" 3) yields "1, "2" 3".

          – Andreas Spindler
          Sep 11 '13 at 12:58













        • @AndreasSpindler Quotes and backslashes alike are escaped by (additional) backslashes as long as they appear inside a string or character literal token. Not sure what's your point. It is illegal to have an unmatched quote (double or single), so contractions don't work, or an odd number of them anyway, which is probably the biggest downside. +1 anyway. "Real programmers" always use contractions in pairs with no intervening newline so the single quotes balance.

          – Potatoswatter
          Oct 14 '13 at 4:28













        • The point is that he wrote "consumes everything between parentheses".

          – Andreas Spindler
          Oct 14 '13 at 14:33














        25












        25








        25







        #define MULTILINE(...) #__VA_ARGS__

        Consumes everything between the parentheses.

        Replaces any number of consecutive whitespace characters by a single space.






        share|improve this answer













        #define MULTILINE(...) #__VA_ARGS__

        Consumes everything between the parentheses.

        Replaces any number of consecutive whitespace characters by a single space.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 12 '13 at 13:08









        Zlatan StanojevićZlatan Stanojević

        25933




        25933








        • 1





          You can add n if you need newlines

          – Simon
          Jul 1 '13 at 14:11











        • Note that ` (and hence n) is copied literally, but "` is converted into ". So MULTILINE(1, "2" 3) yields "1, "2" 3".

          – Andreas Spindler
          Sep 11 '13 at 12:58













        • @AndreasSpindler Quotes and backslashes alike are escaped by (additional) backslashes as long as they appear inside a string or character literal token. Not sure what's your point. It is illegal to have an unmatched quote (double or single), so contractions don't work, or an odd number of them anyway, which is probably the biggest downside. +1 anyway. "Real programmers" always use contractions in pairs with no intervening newline so the single quotes balance.

          – Potatoswatter
          Oct 14 '13 at 4:28













        • The point is that he wrote "consumes everything between parentheses".

          – Andreas Spindler
          Oct 14 '13 at 14:33














        • 1





          You can add n if you need newlines

          – Simon
          Jul 1 '13 at 14:11











        • Note that ` (and hence n) is copied literally, but "` is converted into ". So MULTILINE(1, "2" 3) yields "1, "2" 3".

          – Andreas Spindler
          Sep 11 '13 at 12:58













        • @AndreasSpindler Quotes and backslashes alike are escaped by (additional) backslashes as long as they appear inside a string or character literal token. Not sure what's your point. It is illegal to have an unmatched quote (double or single), so contractions don't work, or an odd number of them anyway, which is probably the biggest downside. +1 anyway. "Real programmers" always use contractions in pairs with no intervening newline so the single quotes balance.

          – Potatoswatter
          Oct 14 '13 at 4:28













        • The point is that he wrote "consumes everything between parentheses".

          – Andreas Spindler
          Oct 14 '13 at 14:33








        1




        1





        You can add n if you need newlines

        – Simon
        Jul 1 '13 at 14:11





        You can add n if you need newlines

        – Simon
        Jul 1 '13 at 14:11













        Note that ` (and hence n) is copied literally, but "` is converted into ". So MULTILINE(1, "2" 3) yields "1, "2" 3".

        – Andreas Spindler
        Sep 11 '13 at 12:58







        Note that ` (and hence n) is copied literally, but "` is converted into ". So MULTILINE(1, "2" 3) yields "1, "2" 3".

        – Andreas Spindler
        Sep 11 '13 at 12:58















        @AndreasSpindler Quotes and backslashes alike are escaped by (additional) backslashes as long as they appear inside a string or character literal token. Not sure what's your point. It is illegal to have an unmatched quote (double or single), so contractions don't work, or an odd number of them anyway, which is probably the biggest downside. +1 anyway. "Real programmers" always use contractions in pairs with no intervening newline so the single quotes balance.

        – Potatoswatter
        Oct 14 '13 at 4:28







        @AndreasSpindler Quotes and backslashes alike are escaped by (additional) backslashes as long as they appear inside a string or character literal token. Not sure what's your point. It is illegal to have an unmatched quote (double or single), so contractions don't work, or an odd number of them anyway, which is probably the biggest downside. +1 anyway. "Real programmers" always use contractions in pairs with no intervening newline so the single quotes balance.

        – Potatoswatter
        Oct 14 '13 at 4:28















        The point is that he wrote "consumes everything between parentheses".

        – Andreas Spindler
        Oct 14 '13 at 14:33





        The point is that he wrote "consumes everything between parentheses".

        – Andreas Spindler
        Oct 14 '13 at 14:33











        23














        A probably convenient way to enter multi-line strings is by using macro's. This only works if quotes and parentheses are balanced and it does not contain 'top level' comma's:



        #define MULTI_LINE_STRING(a) #a
        const char *text = MULTI_LINE_STRING(
        Using this trick(,) you don't need to use quotes.
        Though newlines and multiple white spaces
        will be replaced by a single whitespace.
        );
        printf("[[%s]]n",text);


        Compiled with gcc 4.6 or g++ 4.6, this produces: [[Using this trick(,) you don't need to use quotes. Though newlines and multiple white spaces will be replaced by a single whitespace.]]



        Note that the , cannot be in the string, unless it is contained within parenthesis or quotes. Single quotes is possible, but creates compiler warnings.



        Edit: As mentioned in the comments, #define MULTI_LINE_STRING(...) #__VA_ARGS__ allows the use of ,.






        share|improve this answer


























        • For a project in which I wanted to include some lua code snippets in c++, I ended up writing a small python script, in which I entered the multiline strings, and let that generate a c++ source file.

          – bcmpinc
          Jul 16 '12 at 16:39











        • Perfect for me, adding a huge multi-line float-list string from a collada file for unit testing. I didn't fancy putting quotes everywhere, I needed a copy&paste solution.

          – Soylent Graham
          Aug 1 '12 at 11:39






        • 6





          You can use #define MULTILINE(...) #__VA_ARGS__ if you want your string to contain commas.

          – Simon
          Jul 1 '13 at 14:09






        • 2





          Note this will strip out most extra whitesapce (including all n and r), which is kind of handy for some cases and fatal for others.

          – BCS
          Oct 8 '13 at 21:35
















        23














        A probably convenient way to enter multi-line strings is by using macro's. This only works if quotes and parentheses are balanced and it does not contain 'top level' comma's:



        #define MULTI_LINE_STRING(a) #a
        const char *text = MULTI_LINE_STRING(
        Using this trick(,) you don't need to use quotes.
        Though newlines and multiple white spaces
        will be replaced by a single whitespace.
        );
        printf("[[%s]]n",text);


        Compiled with gcc 4.6 or g++ 4.6, this produces: [[Using this trick(,) you don't need to use quotes. Though newlines and multiple white spaces will be replaced by a single whitespace.]]



        Note that the , cannot be in the string, unless it is contained within parenthesis or quotes. Single quotes is possible, but creates compiler warnings.



        Edit: As mentioned in the comments, #define MULTI_LINE_STRING(...) #__VA_ARGS__ allows the use of ,.






        share|improve this answer


























        • For a project in which I wanted to include some lua code snippets in c++, I ended up writing a small python script, in which I entered the multiline strings, and let that generate a c++ source file.

          – bcmpinc
          Jul 16 '12 at 16:39











        • Perfect for me, adding a huge multi-line float-list string from a collada file for unit testing. I didn't fancy putting quotes everywhere, I needed a copy&paste solution.

          – Soylent Graham
          Aug 1 '12 at 11:39






        • 6





          You can use #define MULTILINE(...) #__VA_ARGS__ if you want your string to contain commas.

          – Simon
          Jul 1 '13 at 14:09






        • 2





          Note this will strip out most extra whitesapce (including all n and r), which is kind of handy for some cases and fatal for others.

          – BCS
          Oct 8 '13 at 21:35














        23












        23








        23







        A probably convenient way to enter multi-line strings is by using macro's. This only works if quotes and parentheses are balanced and it does not contain 'top level' comma's:



        #define MULTI_LINE_STRING(a) #a
        const char *text = MULTI_LINE_STRING(
        Using this trick(,) you don't need to use quotes.
        Though newlines and multiple white spaces
        will be replaced by a single whitespace.
        );
        printf("[[%s]]n",text);


        Compiled with gcc 4.6 or g++ 4.6, this produces: [[Using this trick(,) you don't need to use quotes. Though newlines and multiple white spaces will be replaced by a single whitespace.]]



        Note that the , cannot be in the string, unless it is contained within parenthesis or quotes. Single quotes is possible, but creates compiler warnings.



        Edit: As mentioned in the comments, #define MULTI_LINE_STRING(...) #__VA_ARGS__ allows the use of ,.






        share|improve this answer















        A probably convenient way to enter multi-line strings is by using macro's. This only works if quotes and parentheses are balanced and it does not contain 'top level' comma's:



        #define MULTI_LINE_STRING(a) #a
        const char *text = MULTI_LINE_STRING(
        Using this trick(,) you don't need to use quotes.
        Though newlines and multiple white spaces
        will be replaced by a single whitespace.
        );
        printf("[[%s]]n",text);


        Compiled with gcc 4.6 or g++ 4.6, this produces: [[Using this trick(,) you don't need to use quotes. Though newlines and multiple white spaces will be replaced by a single whitespace.]]



        Note that the , cannot be in the string, unless it is contained within parenthesis or quotes. Single quotes is possible, but creates compiler warnings.



        Edit: As mentioned in the comments, #define MULTI_LINE_STRING(...) #__VA_ARGS__ allows the use of ,.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '13 at 18:40

























        answered Apr 3 '12 at 18:21









        bcmpincbcmpinc

        2,0772026




        2,0772026













        • For a project in which I wanted to include some lua code snippets in c++, I ended up writing a small python script, in which I entered the multiline strings, and let that generate a c++ source file.

          – bcmpinc
          Jul 16 '12 at 16:39











        • Perfect for me, adding a huge multi-line float-list string from a collada file for unit testing. I didn't fancy putting quotes everywhere, I needed a copy&paste solution.

          – Soylent Graham
          Aug 1 '12 at 11:39






        • 6





          You can use #define MULTILINE(...) #__VA_ARGS__ if you want your string to contain commas.

          – Simon
          Jul 1 '13 at 14:09






        • 2





          Note this will strip out most extra whitesapce (including all n and r), which is kind of handy for some cases and fatal for others.

          – BCS
          Oct 8 '13 at 21:35



















        • For a project in which I wanted to include some lua code snippets in c++, I ended up writing a small python script, in which I entered the multiline strings, and let that generate a c++ source file.

          – bcmpinc
          Jul 16 '12 at 16:39











        • Perfect for me, adding a huge multi-line float-list string from a collada file for unit testing. I didn't fancy putting quotes everywhere, I needed a copy&paste solution.

          – Soylent Graham
          Aug 1 '12 at 11:39






        • 6





          You can use #define MULTILINE(...) #__VA_ARGS__ if you want your string to contain commas.

          – Simon
          Jul 1 '13 at 14:09






        • 2





          Note this will strip out most extra whitesapce (including all n and r), which is kind of handy for some cases and fatal for others.

          – BCS
          Oct 8 '13 at 21:35

















        For a project in which I wanted to include some lua code snippets in c++, I ended up writing a small python script, in which I entered the multiline strings, and let that generate a c++ source file.

        – bcmpinc
        Jul 16 '12 at 16:39





        For a project in which I wanted to include some lua code snippets in c++, I ended up writing a small python script, in which I entered the multiline strings, and let that generate a c++ source file.

        – bcmpinc
        Jul 16 '12 at 16:39













        Perfect for me, adding a huge multi-line float-list string from a collada file for unit testing. I didn't fancy putting quotes everywhere, I needed a copy&paste solution.

        – Soylent Graham
        Aug 1 '12 at 11:39





        Perfect for me, adding a huge multi-line float-list string from a collada file for unit testing. I didn't fancy putting quotes everywhere, I needed a copy&paste solution.

        – Soylent Graham
        Aug 1 '12 at 11:39




        6




        6





        You can use #define MULTILINE(...) #__VA_ARGS__ if you want your string to contain commas.

        – Simon
        Jul 1 '13 at 14:09





        You can use #define MULTILINE(...) #__VA_ARGS__ if you want your string to contain commas.

        – Simon
        Jul 1 '13 at 14:09




        2




        2





        Note this will strip out most extra whitesapce (including all n and r), which is kind of handy for some cases and fatal for others.

        – BCS
        Oct 8 '13 at 21:35





        Note this will strip out most extra whitesapce (including all n and r), which is kind of handy for some cases and fatal for others.

        – BCS
        Oct 8 '13 at 21:35











        12














        You can just do this:



        const char *text = "This is my string it is "
        "very long";





        share|improve this answer






























          12














          You can just do this:



          const char *text = "This is my string it is "
          "very long";





          share|improve this answer




























            12












            12








            12







            You can just do this:



            const char *text = "This is my string it is "
            "very long";





            share|improve this answer















            You can just do this:



            const char *text = "This is my string it is "
            "very long";






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 21 '13 at 7:33

























            answered Jul 16 '09 at 6:58









            EricEric

            5,37812244




            5,37812244























                9














                Since an ounce of experience is worth a ton of theory, I tried a little test program for MULTILINE:



                #define MULTILINE(...) #__VA_ARGS__

                const char *mstr =
                {
                MULTILINE(1, 2, 3), // "1, 2, 3"
                MULTILINE(1,2,3), // "1,2,3"
                MULTILINE(1 , 2 , 3), // "1 , 2 , 3"
                MULTILINE( 1 , 2 , 3 ), // "1 , 2 , 3"
                MULTILINE((1, 2, 3)), // "(1, 2, 3)"
                MULTILINE(1
                2
                3), // "1 2 3"
                MULTILINE(1n2n3n), // "1n2n3n"
                MULTILINE(1n
                2n
                3n), // "1n 2n 3n"
                MULTILINE(1, "2" 3) // "1, "2" 3"
                };


                Compile this fragment with cpp -P -std=c++11 filename to reproduce.



                The trick behind #__VA_ARGS__ is that __VA_ARGS__ does not process the comma separator. So you can pass it to the stringizing operator. Leading and trailing spaces are trimmed, and spaces (including newlines) between words are compressed to a single space then. Parentheses need to be balanced. I think these shortcomings explain why the designers of C++11, despite #__VA_ARGS__, saw the need for raw string literals.






                share|improve this answer






























                  9














                  Since an ounce of experience is worth a ton of theory, I tried a little test program for MULTILINE:



                  #define MULTILINE(...) #__VA_ARGS__

                  const char *mstr =
                  {
                  MULTILINE(1, 2, 3), // "1, 2, 3"
                  MULTILINE(1,2,3), // "1,2,3"
                  MULTILINE(1 , 2 , 3), // "1 , 2 , 3"
                  MULTILINE( 1 , 2 , 3 ), // "1 , 2 , 3"
                  MULTILINE((1, 2, 3)), // "(1, 2, 3)"
                  MULTILINE(1
                  2
                  3), // "1 2 3"
                  MULTILINE(1n2n3n), // "1n2n3n"
                  MULTILINE(1n
                  2n
                  3n), // "1n 2n 3n"
                  MULTILINE(1, "2" 3) // "1, "2" 3"
                  };


                  Compile this fragment with cpp -P -std=c++11 filename to reproduce.



                  The trick behind #__VA_ARGS__ is that __VA_ARGS__ does not process the comma separator. So you can pass it to the stringizing operator. Leading and trailing spaces are trimmed, and spaces (including newlines) between words are compressed to a single space then. Parentheses need to be balanced. I think these shortcomings explain why the designers of C++11, despite #__VA_ARGS__, saw the need for raw string literals.






                  share|improve this answer




























                    9












                    9








                    9







                    Since an ounce of experience is worth a ton of theory, I tried a little test program for MULTILINE:



                    #define MULTILINE(...) #__VA_ARGS__

                    const char *mstr =
                    {
                    MULTILINE(1, 2, 3), // "1, 2, 3"
                    MULTILINE(1,2,3), // "1,2,3"
                    MULTILINE(1 , 2 , 3), // "1 , 2 , 3"
                    MULTILINE( 1 , 2 , 3 ), // "1 , 2 , 3"
                    MULTILINE((1, 2, 3)), // "(1, 2, 3)"
                    MULTILINE(1
                    2
                    3), // "1 2 3"
                    MULTILINE(1n2n3n), // "1n2n3n"
                    MULTILINE(1n
                    2n
                    3n), // "1n 2n 3n"
                    MULTILINE(1, "2" 3) // "1, "2" 3"
                    };


                    Compile this fragment with cpp -P -std=c++11 filename to reproduce.



                    The trick behind #__VA_ARGS__ is that __VA_ARGS__ does not process the comma separator. So you can pass it to the stringizing operator. Leading and trailing spaces are trimmed, and spaces (including newlines) between words are compressed to a single space then. Parentheses need to be balanced. I think these shortcomings explain why the designers of C++11, despite #__VA_ARGS__, saw the need for raw string literals.






                    share|improve this answer















                    Since an ounce of experience is worth a ton of theory, I tried a little test program for MULTILINE:



                    #define MULTILINE(...) #__VA_ARGS__

                    const char *mstr =
                    {
                    MULTILINE(1, 2, 3), // "1, 2, 3"
                    MULTILINE(1,2,3), // "1,2,3"
                    MULTILINE(1 , 2 , 3), // "1 , 2 , 3"
                    MULTILINE( 1 , 2 , 3 ), // "1 , 2 , 3"
                    MULTILINE((1, 2, 3)), // "(1, 2, 3)"
                    MULTILINE(1
                    2
                    3), // "1 2 3"
                    MULTILINE(1n2n3n), // "1n2n3n"
                    MULTILINE(1n
                    2n
                    3n), // "1n 2n 3n"
                    MULTILINE(1, "2" 3) // "1, "2" 3"
                    };


                    Compile this fragment with cpp -P -std=c++11 filename to reproduce.



                    The trick behind #__VA_ARGS__ is that __VA_ARGS__ does not process the comma separator. So you can pass it to the stringizing operator. Leading and trailing spaces are trimmed, and spaces (including newlines) between words are compressed to a single space then. Parentheses need to be balanced. I think these shortcomings explain why the designers of C++11, despite #__VA_ARGS__, saw the need for raw string literals.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 11 '13 at 13:21

























                    answered Sep 11 '13 at 13:14









                    Andreas SpindlerAndreas Spindler

                    4,47923230




                    4,47923230























                        7














                        Just to elucidate a bit on @emsr's comment in @unwind's answer, if one is not fortunate enough to have a C++11 compiler (say GCC 4.2.1), and one wants to embed the newlines in the string (either char * or class string), one can write something like this:



                        const char *text =
                        "This text is pretty long, but will ben"
                        "concatenated into just a single string.n"
                        "The disadvantage is that you have to quoten"
                        "each part, and newlines must be literal asn"
                        "usual.";


                        Very obvious, true, but @emsr's short comment didn't jump out at me when I read this the first time, so I had to discover this for myself. Hopefully, I've saved someone else a few minutes.






                        share|improve this answer




























                          7














                          Just to elucidate a bit on @emsr's comment in @unwind's answer, if one is not fortunate enough to have a C++11 compiler (say GCC 4.2.1), and one wants to embed the newlines in the string (either char * or class string), one can write something like this:



                          const char *text =
                          "This text is pretty long, but will ben"
                          "concatenated into just a single string.n"
                          "The disadvantage is that you have to quoten"
                          "each part, and newlines must be literal asn"
                          "usual.";


                          Very obvious, true, but @emsr's short comment didn't jump out at me when I read this the first time, so I had to discover this for myself. Hopefully, I've saved someone else a few minutes.






                          share|improve this answer


























                            7












                            7








                            7







                            Just to elucidate a bit on @emsr's comment in @unwind's answer, if one is not fortunate enough to have a C++11 compiler (say GCC 4.2.1), and one wants to embed the newlines in the string (either char * or class string), one can write something like this:



                            const char *text =
                            "This text is pretty long, but will ben"
                            "concatenated into just a single string.n"
                            "The disadvantage is that you have to quoten"
                            "each part, and newlines must be literal asn"
                            "usual.";


                            Very obvious, true, but @emsr's short comment didn't jump out at me when I read this the first time, so I had to discover this for myself. Hopefully, I've saved someone else a few minutes.






                            share|improve this answer













                            Just to elucidate a bit on @emsr's comment in @unwind's answer, if one is not fortunate enough to have a C++11 compiler (say GCC 4.2.1), and one wants to embed the newlines in the string (either char * or class string), one can write something like this:



                            const char *text =
                            "This text is pretty long, but will ben"
                            "concatenated into just a single string.n"
                            "The disadvantage is that you have to quoten"
                            "each part, and newlines must be literal asn"
                            "usual.";


                            Very obvious, true, but @emsr's short comment didn't jump out at me when I read this the first time, so I had to discover this for myself. Hopefully, I've saved someone else a few minutes.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 15 '13 at 22:01









                            CXJCXJ

                            2,80422142




                            2,80422142






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1135841%2fc-multiline-string-literal%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith