有什么原因为什么StringPiece / StringRef成语不更受欢迎? [英] Are there any reasons why the StringPiece/StringRef idiom is not more popular?

查看:320
本文介绍了有什么原因为什么StringPiece / StringRef成语不更受欢迎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Chromium的源代码中的StringPiece类文档

  //指向一个大小内存的字符串状对象。 
//
//函数或方法可以使用const StringPiece&参数接受
//一个const char *或一个字符串值,将被隐式转换为
// a StringPiece。
//
//鼓励使用StringPiece的系统,因为它会减少不必要的
//从const char *到string的转换,然后再返回。

使用示例:

  void foo(StringPiece const& str)// Pass by ref。可能不需要
{
// str具有相同的接口const std :: string
}

int main()
{
string bar(bar);
foo(bar); // OK,no mem。 alloc。

//没有mem。 alloc。或者,如果arg。的foo是std :: string
foo(baz);
}

这似乎是一个重要而明显的优化,它不是更广泛,为什么类似于StringPiece的类已经不在标准中了。



有什么我不应该替换使用 string char * 参数?在C ++标准库中已经有类似的东西了吗?



UPDATE 。我知道LLVM的源使用类似的概念: StringRef 类。

解决方案

因为为什么打扰?使用复制elision和/或通过引用传递,通常也可以避免 std :: string 的内存分配。



如果语言是从头开始重新设计的,或者如果向后兼容,那么C ++中的字符串情况就会变得混乱,而不添加更多的字符串类。不是一个问题,那么这是可以做的字符串处理在C ++中的许多可能的改进之一。但现在我们停留在 char * std :: string ,添加一个stringref风格的类这个混合会导致很多的混乱,有限的利益。



除此之外,是不是用一对迭代器更成熟地实现了同样的效果?
如果我想传递一个字符序列,无论它们属于字符串还是 char * ,为什么我不应该使用一对迭代器分隔它们?


From the documentation of the StringPiece class in Chromium's source code:

// A string-like object that points to a sized piece of memory.
//
// Functions or methods may use const StringPiece& parameters to accept either
// a "const char*" or a "string" value that will be implicitly converted to
// a StringPiece.  
//
// Systematic usage of StringPiece is encouraged as it will reduce unnecessary
// conversions from "const char*" to "string" and back again.

Example of use:

void foo(StringPiece const & str) // Pass by ref. is probably not needed
{
   // str has same interface of const std::string
}

int main()
{
    string bar("bar");
    foo(bar); // OK, no mem. alloc.

    // No mem. alloc. either, would be if arg. of "foo" was std::string
    foo("baz");  
}

This seems like such an important and obvious optimization that I can't understand why it's not more widespread, and why a class similar to StringPiece is not already in the standard.

Are there any reasons why I shouldn't replace the use of string and char* parameters in my own code with this class? Is there anything like it already in the C++ standard libraries?

UPDATE. I've learnt that LLVM's source uses a similar concept: the StringRef class.

解决方案

Because why bother? With copy elision and/or pass by reference, memory allocations for std::string can usually be avoided as well.

The string situation in C++ is confusing enough as it is, without adding still more string classes.

If the language was to be redesigned from scratch, or if backwards compatibility wasn't an issue, then this is one of many possible improvements that could be done to string handling in C++. But now that we're stuck with both char* and std::string, adding a stringref-style class into the mix would cause a lot of confusion, with limited benefit.

Apart from this, isn't the same effect achieved more idiomatically with a pair of iterators? If I want to pass a sequence of characters, whether they belong to a string or a char*, why shouldn't I just use a pair of iterators to delimit them?

这篇关于有什么原因为什么StringPiece / StringRef成语不更受欢迎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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