-1

This question of how to break up a (wide) string constant along multiple lines in code has been asked and answered multiple times on this platform (here, for example). I have been using the following approach to declare a static wide string in the class definition itself:

#include <Windows.h>    
#include <iostream>

class Test
{
    public:
        static constexpr WCHAR TEST[] = L"The quick brown fox" \
                                        L"jumped over the" \
                                        L"lazy dog!";
    /* ... */
};

int main ()
{
    wprintf(Test::TEST);
    return 1;
}

However, this approach does not work very well if you want to start commenting each line of your multi-line string. The only way you can achieve this is if you stick a multi-line comment in between the string and the backslash (\) like so:

class Test
{
    public:
        static constexpr WCHAR TEST[] = L"The quick brown fox" /* A comment */\
                                        L"jumped over the" /* Another comment*/\
                                        L"lazy dog!";
    /* ... */
};

If you put a comment after the backslash (\), then a compiler error is raised. This means, you cannot use the traditional single line comment (//) with this method. As a matter of fact, if you put a single space after the backslash (\), then a compiler error is generated.

My question is as follows, is there a way to declare a wide string over multiple lines where you can use single line comments (//) to comment each line?

I am idling looking for something like this (similar to Java):

static constexpr ... = L"The quick brown fox" +  // A little comment here...
                       L"jumped over the" +      // A little comment there.
                       L"lazy dog!";

I understand things are incredibly different in Java (i.e. namely everything is an object), but I am just giving this as an example of what I am after.

Code Doggo
  • 2,146
  • 6
  • 33
  • 58
  • 1
    In your first snippet, you don't need the backslashes. Adjacent string literals are concatenated, even if they're on separate lines. (This doesn't answer your question.) – Keith Thompson Feb 14 '20 at 01:06

1 Answers1

1

Simply drop the backslashes:

    static constexpr WCHAR TEST[] = L"The quick brown fox" // a comment
                                    L"jumped over the"     // another comment
                                    L"lazy dog!";          // yet another comment

Comments are replaced by space characters in translation phase 3. Adjacent string literals are concatenated in translation phase 6.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Yeah, I took your comment and gave it a shot and it work with single line comments. I appreciate it. Although, I do have a question, were you the one that down voted my question? Was there something specific you thought merited a down vote? – Code Doggo Feb 14 '20 at 01:13
  • 1
    @CodeDoggo: No, it wasn't me. Only a guess, but maybe someone objected to your referring to `\` as "forward slash" and downvoted rather than fixing it. – Keith Thompson Feb 14 '20 at 01:39
  • Yeah, that was a mix up in my head. It was an honest mistake - I appreciate the catch as you had in your edits. – Code Doggo Feb 14 '20 at 18:20