#include <iostream>
#include <string>
int main() {
std::string str = "hello " "world" "!";
std::cout << str;
}
The following commands compile, run, and print:
Hello World!
watch live
It seems that string literals are concatenated together, but it is interesting that this cannot be done with operator +:
#include <iostream>
#include <string>
int main() {
std::string str = "hello " + "world";
std::cout << str;
}
This does not compile.
watch live
Why is this behavior in language? My theory is that it allows you to build strings with multiple operators #include, because the operators #includemust be in their own string. Is it possible that this behavior is possible due to the grammar of the language, or is it an exception that was added to solve the problem?
source
share