4

I picked up a C++ code to use in my program but I found in it a string declaration I just couldn't make sense of. A double quote is supposed to mark the beginning and the end of a string, right? but in this string declaration, there are many double quotes. How does the compiler figure it out?

I tried compiling and it compiles

using namespace cv;
using namespace std;

std::string keys = "{ help  h     | | Print help message. }"
"3: VPU }";
1
  • 4
    this is two strings next to each other. the compiler will catenate them Commented Jan 17, 2019 at 4:28

3 Answers 3

4

A character sequence within quotes (or even empty quotes) with or without an encoding prefix is a string-literal as per [lex.string].

So "{ help h | | Print help message. }" is a string literal and so is "3: VPU }".

And as per [lex.string]/13:

...adjacent string-literals are concatenated.

So the result is same as:

std::string keys = "{ help  h     | | Print help message. }3: VPU }";
Sign up to request clarification or add additional context in comments.

1 Comment

@paxdiablo: Thanks for the comment. I try to add references whenever required so as to encourage the asker to check the standard before posting a question next time.
1

When two or multiple strings are next to each other they are concatenated by compiler provided they should not be separated by anything other than space, tab or newline.

Below code will work:

std::string keys = "abc" "def" "ghi";

but below will not:

std::string keys = "abc","def","ghi";

Comments

0

The pre-processor will concatenate string literals to form a single string literal, i.e. "abc" "def" will be concatenated into "abcdef" prior to any assignments or other use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.