I do really wonder why this program compiles and run correctly:
int main(){
std::string(foo)("strange" " string " "declaration");
std::cout << foo << std::endl;
return 0;
}
It will print:
strange string declaration
I do really wonder why this program compiles and run correctly:
int main(){
std::string(foo)("strange" " string " "declaration");
std::cout << foo << std::endl;
return 0;
}
It will print:
strange string declaration
This is because the strings are being processed by the C preprocessor. When you have strings like this:
"foo" "bar"
the preprocessor turns them into
"foobar"
which is then passed on to the C or C++ compiler. This is handy if you want to have long string literals, and still keep them readable:
"this is a very long string literal that goes on and on and on and on"
" and then stops"
It is because declarations and definitions can have extraneous parenthesis. In this case the parens around foo don't do anything.