可能重复:
两个字符串文字的连接
案例 1 "hello" "world"
(无错误)
案例 2 "hello"+"world"
(错误)
我知道+
运算符必须至少将其操作数之一设为string type
,not string literal
。
问题是,这两种情况都没有意义,因为我们可以将它包含在single literal
!
那么为什么允许案例1!
可能重复:
两个字符串文字的连接
案例 1 "hello" "world"
(无错误)
案例 2 "hello"+"world"
(错误)
我知道+
运算符必须至少将其操作数之一设为string type
,not string literal
。
问题是,这两种情况都没有意义,因为我们可以将它包含在single literal
!
那么为什么允许案例1!
情况 1 是允许的,因为 C++ 基本上认为相邻的文字是相同的文字,即代码在解析的早期就"hello" "world"
被转换为。"helloworld"
Case 1, as you call it, is a convenient way to let string constants span several lines or be broken up into easily identified parts.
It's there to make your life easier.
Assuming that +
should have the same semantics when used with string constants is a mistake. Especially in C, where string
isn't an available type.