从 C++17 开始,我们有std::string_view http://en.cppreference.com/w/cpp/string/basic_string_view,连续字符序列的轻量级视图,可避免不必要的数据复制。而不是有一个const std::string&
参数,现在经常推荐使用std::string_view
.
然而,人们很快就会发现,从const std::string&
to std::string_view
破坏使用字符串连接的代码,因为不支持连接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
and std::string_view
在标准中?
其原因在n3512 string_ref:对字符串的非拥有引用,修订版 2 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3512.html作者:杰弗里·亚斯金:
我还省略了operator+(basic_string, basic_string_ref),因为LLVM从此重载中返回一个轻量级对象,并且仅延迟执行连接。如果我们定义这个重载,那么稍后我们将很难引入这种轻量级串联。
后来有人在网上提出了建议标准提案 https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/1RcShRhrmRc邮件列表将这些运算符重载添加到标准中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)