简体   繁体   English

c ++字符串文字并置

[英]c++ string literal concatenation by juxtaposition

The code 编码

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

compiles and prints Hello world . 编译并打印Hello world Similarly, 同样,

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

prints abcdefghi . 打印abcdefghi How and why does this work? 这如何以及为什么起作用?

This is behavior covered by [lex.phases]/6 这是[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"; 全部产生"hello world";

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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