#include <iostream>
#include <string>
int main() {
std::string str = "hello " "world" "!";
std::cout << str;
}
以下代码可以编译、运行并打印:
你好,世界!
似乎字符串字面量正在被串联在一起,但有趣的是这不能使用
operator +
实现:#include <iostream>
#include <string>
int main() {
std::string str = "hello " + "world";
std::cout << str;
}
这将编译失败。
查看实时
为什么语言会有这种行为?我的理论是,这使得可以使用多个
#include
语句构造字符串,因为#include
语句必须单独成行。这种行为是由于语言语法的简单可能实现的,还是因为为解决问题而添加的异常情况?