Reputation: 41
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 }";
Upvotes: 4
Views: 218
Reputation: 461
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";
Upvotes: 1
Reputation: 26810
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 }";
Upvotes: 4
Reputation: 6125
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.
Upvotes: 0