为什么不支持串联std :: string和std :: string_view? [英] Why is there no support for concatenating std::string and std::string_view?

查看:153
本文介绍了为什么不支持串联std :: string和std :: string_view?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从C ++ 17开始,我们有了 std :: string_view ,这是一个连续字符序列的轻量级视图,可避免不必要的数据复制。现在通常建议使用 std :: string_view 而不是使用 const std :: string& 参数。

Since C++17, we have std::string_view, a light-weight view into a contiguous sequence of characters that avoids unnecessary copying of data. Instead of having a const std::string& parameter, it is now often recommended to use std::string_view.

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

However, one quickly finds out that switching from const std::string& to std::string_view breaks code that uses string concatenation as there is no support for concatenating std::string and std::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串联: :string std :: string_view 在标准中?

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

推荐答案

n3512 string_ref:杰弗里·亚斯金(Jeffrey Yasskin)对字符串版本2的非所有权引用


我也省略了运算符+ (basic_string,basic_string_ref),因为LLVM从此重载返回轻量级对象,并且仅延迟执行串联。如果定义了此重载,稍后将很难引入这种轻量级的连接。

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.

稍后在< a href = https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/1RcShRhrmRc rel = noreferrer> std-proposals 邮件列表中添加这些操作符重载到标准。

It has been later suggested on the std-proposals mailing list to add these operator overloads to the standard.

这篇关于为什么不支持串联std :: string和std :: string_view?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆