为什么没有从std :: string_view到std :: string的隐式转换? [英] Why is there no implicit conversion from std::string_view to std::string?

查看:1773
本文介绍了为什么没有从std :: string_view到std :: string的隐式转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: string 隐式转换为 std :: string_view ,这并不认为是不安全的,即使程序员不注意,即使这样肯定会导致很多悬挂引用。

There is an implicit conversion from std::string to std::string_view and it's not considered unsafe, even though this surely may cause a lot of dangling references if programmer is not careful.

另一方面,从 std没有隐式转换: :string_view std :: string 使用相同的参数,但方式完全相反:因为程序员可能不小心

On the other hand, there's no implicit conversion from std::string_view to std::string using same argument but in the completely opposite fashion: because programmer may be not careful.

很高兴C ++替换了原始的 const char * 指针,同时使其变得超级混乱和剥去骨头:

It's lovely that C++ has a replacement for a raw const char* pointer, while making it super confusing and stripped to the bone:


  • 隐式 const char * -> std :: string 确定

  • 隐式 std :: string_view -> std :: string NOPE

  • 分配 std ::字符串 = const char * 确定

  • 分配 std :: string = std :: s tring_view 确定

  • 附加 std :: string + = const char * 确定

  • 附加 std :: string + = std :: string_view 确定

  • 串联 const char * + std :: string 确定

  • 并置 std :: string_view + std :: string NOPE

  • 串联 std :: string + const char * 确定

  • 串联 std :: string + std :: string_view NOPE

  • Implicit const char* -> std::string: OK
  • Implicit std::string_view -> std::string: NOPE
  • Assignment std::string = const char* : OK
  • Assignment std::string = std::string_view: OK
  • Appending std::string += const char* : OK
  • Appending std::string += std::string_view: OK
  • Concatenation const char* + std::string: OK
  • Concatenation std::string_view + std::string: NOPE
  • Concatenation std::string + const char*: OK
  • Concatenation std::string + std::string_view: NOPE

我错过了什么吗?或者这完全是胡说?

Am I missing something or is this a total nonsense?

最后,此字符串视图在没有使它类似于 const char * ?将其集成到 stdlib 的生态系统中又有什么意义呢?毕竟,如果我们需要一个代表字符串的对象,我们可以编写自己的对象。实际上,很多年前,很多图书馆已经做到了。制定某种标准的全部目的是使其在最广泛的用例中有用,不是吗?

In the end, how useful is this string view without all the crucial pieces that make it similar to const char*? What's the point of integrating it into the ecosystem of stdlib while not making the last step to make it complete? After all, if we need an object that represents a piece of a string we could write our own. Actually, a lot of libraries already have done that, years ago. The whole point of making something standard is to make it useful for widest range of use cases, isn't it?

他们要在 C中修复此问题吗? ++ 23

推荐答案

问题是 std :: string_view -> std :: string 对基础内存进行复制,并完成堆分配,而隐式 std :: string -> std :: string_view 不会。如果您一开始就讨厌使用 std :: string_view ,那么您显然会在意副本,因此您不希望隐式地发生副本。

The problem is that std::string_view -> std::string makes a copy of the underlying memory, complete with heap allocation, whereas the implicit std::string -> std::string_view does not. If you've bothered to use a std::string_view in the first place then you obviously care about copies, so you don't want one to happen implicitly.

请考虑以下示例:

void foo1(const std::string& x)
{
    foo2(x);
}
void foo2(std::string_view x)
{
    foo3(x);
}
void foo3(const std::string& x)
{
    // Use x...
}

函数 foo2 可以使用 const std: :string& 参数,但使用了 std :: string_view ,因此,如果传入的字符串不是 std :: string ;没有惊喜。但这要比您只给它一个 const std :: string& 参数要低!

The function foo2 could've used a const std::string& parameter, but used a std::string_view so that it is more efficient if you pass in a string that isn't a std::string; no surprises there. But it's less efficient than if you'd just given it a const std::string& parameter!


  • 使用 std :: string 参数调用 foo2 时(例如,通过 foo1 ):当 foo2 调用 foo3 时,它将创建字符串的副本。如果它具有 const std :: string& 参数,则它可以使用已经拥有的对象。

  • foo2 const char * 参数调用:A std :: string 迟早要复印;使用 const std :: string& 参数可以更早地完成创建,但总体而言,这两种方式都只有一个副本。

  • When foo2 is called with a std::string argument (e.g. by foo1): When foo2 calls foo3, it creates a copy of the string. If it had a const std::string& argument, it could've used the object it already had.
  • When foo2 is called with a const char* argument: A std::string copy has to be made sooner or later; with a const std::string& parameter it gets made earlier, but overall there's exactly one copy either way.

现在想象 foo2 调用多个函数,例如 foo3 或调用 foo3 循环;一遍又一遍地制作完全相同的 std :: string 对象。您希望编译器对此进行通知。

Now imagine foo2 calls multiple functions like foo3, or calls foo3 in a loop; it's making exactly the same std::string object over and over. You'd want the compiler to notify you about this.

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

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