是传递const std :: string&作为参数呢? [英] Are the days of passing const std::string & as a parameter over?

查看:301
本文介绍了是传递const std :: string&作为参数呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说了Herb Sutter最近的一个讲话,他建议通过 std :: vector std :: string 通过 const& 很大程度上消失了。他建议编写一个如下所示的函数:

  std :: string do_something(std :: string inval) 
{
std :: string return_val;
// ... do stuff ...
return return_val;
}



我理解 return_val 将是函数返回时的右值,因此可以使用move semantics返回,这非常便宜。然而, inval 仍然大大大于引用(通常实现为指针)的大小。这是因为 std :: string 有各种组件,包括指向堆的指针和成员 char [] 短字符串优化。所以在我看来,通过引用仍然是一个好主意。



任何人都可以解释为什么Herb可能会这样说?



让我们说,我有函数

code> C ,它调用函数 B ,它调用函数 C 通过 B 将字符串传递到 C A 不知道或关心 C ;所有 A 知道是 B 。也就是说, C B 的实现细节。



假设A定义如下:

  void A()
{
B 值);
}



如果B和C采用 const& ,那么它看起来像这样:

  void B(const std :: string& str )
{
C(str);
}

void C(const std :: string& str)
{
//使用`str'做某事。不存储它。
}

一切都很好。你只是传递指针,没有复制,没有移动,大家的快乐。 C 需要一个 const& ,因为它不存储字符串。它只是使用它。



现在,我想做一个简单的更改: C 需要将字符串存储在某处。

  void C(const std :: string& str)
{
//用`str`。
m_str = str;
}



您好,复制构造函数和潜在的内存分配(忽略短字符串优化(SSO))。 C ++ 11的移动语义应该是可以删除不必要的拷贝构造,对吗?和 A 传递临时;没有理由 C 应该必须复制数据。



除了它不能。因为它需要一个 const&



如果我改变 C 通过值取其参数,只是导致 B 对该参数进行复制;



所以如果我只是通过所有的函数传递 str code> std :: move 来清洗数据,我们不会有这个问题。如果有人想要坚持它,他们可以。如果他们不,哦,好。



是否更贵?是;移动到一个值比使用引用更昂贵。它比副本便宜吗?不适用于使用SSO的小字符串。这是值得吗?



这取决于你的用例。你讨厌内存分配多少?


I heard a recent talk by Herb Sutter who suggested that the reasons to pass std::vector and std::string by const & are largely gone. He suggested that writing a function such as the following is now preferable:

std::string do_something ( std::string inval )
{
   std::string return_val;
   // ... do stuff ...
   return return_val;
}

I understand that the return_val will be an rvalue at the point the function returns and can therefore be returned using move semantics, which are very cheap. However, inval is still much larger than the size of a reference (which is usually implemented as a pointer). This is because a std::string has various components including a pointer into the heap and a member char[] for short string optimization. So it seems to me that passing by reference is still a good idea.

Can anyone explain why Herb might have said this?

解决方案

The reason Herb said what he said is because of cases like this.

Let's say I have function A which calls function B, which calls function C. And A passes a string through B and into C. A does not know or care about C; all A knows about is B. That is, C is an implementation detail of B.

Let's say that A is defined as follows:

void A()
{
  B("value");
}

If B and C take the string by const&, then it looks something like this:

void B(const std::string &str)
{
  C(str);
}

void C(const std::string &str)
{
  //Do something with `str`. Does not store it.
}

All well and good. You're just passing pointers around, no copying, no moving, everyone's happy. C takes a const& because it doesn't store the string. It simply uses it.

Now, I want to make one simple change: C needs to store the string somewhere.

void C(const std::string &str)
{
  //Do something with `str`.
  m_str = str;
}

Hello, copy constructor and potential memory allocation (ignore the Short String Optimization (SSO)). C++11's move semantics are supposed to make it possible to remove needless copy-constructing, right? And A passes a temporary; there's no reason why C should have to copy the data. It should just abscond with what was given to it.

Except it can't. Because it takes a const&.

If I change C to take its parameter by value, that just causes B to do the copy into that parameter; I gain nothing.

So if I had just passed str by value through all of the functions, relying on std::move to shuffle the data around, we wouldn't have this problem. If someone wants to hold on to it, they can. If they don't, oh well.

Is it more expensive? Yes; moving into a value is more expensive than using references. Is it less expensive than the copy? Not for small strings with SSO. Is it worth doing?

It depends on your use case. How much do you hate memory allocations?

这篇关于是传递const std :: string&作为参数呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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