为什么不支持连接 std::string 和 std::string_view?

Why is there no support for concatenating std::string and std::string_view?

从 C++17 开始,我们有了 std::string_view,一种对连续字符序列的轻量级视图,可以避免不必要的数据复制。现在通常建议使用 std::string_view.

而不是 const std::string& 参数

但是,很快就会发现从 const std::string& 切换到 std::string_view 会破坏使用字符串连接的代码,因为不支持连接 std::stringstd::string_view

std::string{"abc"} + std::string_view{"def"}; // ill-formed (fails to compile)
std::string_view{"abc"} + std::string{"def"}; // ill-formed (fails to compile)

为什么标准中不支持连接 std::stringstd::string_view

Jeffrey Yasskin 在 n3512 string_ref: a non-owning reference to a string, revision 2 中给出了这样做的原因:

I also omitted operator+(basic_string, basic_string_ref) because LLVM returns a lightweight object from this overload and only performs the concatenation lazily. If we define this overload, we'll have a hard time introducing that lightweight concatenation later.

后来有人在 std-proposals 邮件列表上建议将这些运算符重载添加到标准中。

我已经提交 P2591: Concatenation of strings and string views,链接到这个 SO 问题。这篇论文针对的是C++26 minimum