简体   繁体   中英

c++ string literal concatenation by juxtaposition

The code

std::string str = "Hello " "world";
std::cout << str << std::endl;

compiles and prints Hello world . Similarly,

char chr[] = "abc" "def" "ghi";
std::cout << chr << std::endl;

prints abcdefghi . How and why does this work?

This is behavior covered by [lex.phases]/6

Adjacent string literal tokens are concatenated.

So before the compiler actually starts to compile the code all string literals that are only separated why white space are concatenated together.

"hello " "world";
"hello "                  "world";
"hello " 
"world";

All produce "hello world";

仅由空格分隔的字符串文字会自动连接起来, 请参阅cppreference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM