c++ - 为什么string_view::operator ==按值接受参数

标签 c++ std pass-by-value string-view pass-by-const-reference

我正在阅读string_view的源代码,发现operator ==按值接受参数。

template<typename _CharT, typename _Traits>
    constexpr bool
    operator==(basic_string_view<_CharT, _Traits> __x,
               basic_string_view<_CharT, _Traits> __y) noexcept
    { return __x.size() == __y.size() && __x.compare(__y) == 0; }

为什么它按值而不是const引用接受参数?

最佳答案

Why does string_view::operator== accepts parameters by value



因为这是传递未经修改且复制成本低的参数的推荐方法。

无需支付引用引入的间接费用-也就是说,在大多数情况下,该函数可以内联扩展,在这种情况下,这实际上并不重要。

Isn't passing by reference cheaper?



一般而言:这取决于。在字符串 View 的情况下:可能不是。

关于c++ - 为什么string_view::operator ==按值接受参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62174478/

相关文章:

c++ - C/C++ 风格——修改函数参数

c++ - Qt错误信息 "qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed"

c++ - 将 Windows 套接字程序移植到 Unix : Alternative for winsock32 APIs in unix

c++ - 错误服务 irc mysql

c++ - 错误 : there are no arguments to 'at' that depend on a template parameter, 因此 at 的声明必须可用

c++ - 使用 std::ends() 和 freeze(false)

c++ - 如何将对象传递给 C++ 中的函数?

C通过引用或按值将结构数组传递给函数

c++ - 包括 .cpp 文件?

c++ - 将 String^ 转换为 std::string(基本字符串)-> 错误。我怎样才能解决这个问题?