const字符串在最后附加时不起作用

sam*_*tha 1 c++ string

可能重复:
连接两个字符串文字

为什么这不起作用?

const std::string exclam = "!";          
const std::string message = "Hello" + ", world" + exclam;
Run Code Online (Sandbox Code Playgroud)

但这很好用

const std::string exclam = "!";       
const std::string message = exclam +
"Hello" + ", world" ;     
Run Code Online (Sandbox Code Playgroud)

请向我解释.

谢谢

Bo *_*son 5

原因是没有operator+添加两个字符串文字,并且不需要它.如果你只是删除了第一个例子+.

const std::string message = "Hello"  ", world" + exclam;
Run Code Online (Sandbox Code Playgroud)

因为预处理器编译器魔术*)会将两个相邻的文字加在一起.

第二个例子有效,因为std::string它有一个operator+添加字符串文字.结果是另一个字符串,可以连接下一个文字.


*)翻译阶段6 - 连接相邻的字符串文字标记.