C++ 通过值或引用传递 std::string

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10789740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:27:40  来源:igfitidea点击:

Passing std::string by Value or Reference

c++stringstdmove-semantics

提问by Nordl?w

Possible Duplicate:
Are the days of passing const std::string & as a parameter over?

可能的重复:
将 const std::string & 作为参数传递的日子结束了吗?

Should I pass std::stringby value or by reference (to a un-inlined function) if move semantics is supported? And what about implementations using small string optimization (SSO)?

如果std::string支持移动语义,我应该通过值还是通过引用(到非内联函数)传递?那么使用小字符串优化 (SSO) 的实现呢?

回答by linuxuser27

There are multiple answers based on what you are doing with the string.

根据您对字符串的处理,有多种答案。

1) Using the string as an id (will not be modified). Passing it in by const reference is probably the best idea here: (std::string const&)

1)使用字符串作为id(不会被修改)。通过 const 引用传递它可能是这里最好的主意:(std::string const&)

2) Modifying the string but not wanting the caller to see that change. Passing it in by value is preferable: (std::string)

2) 修改字符串但不希望调用者看到该更改。按值传递它是可取的:(std::string)

3) Modifying the string but wanting the caller to see that change. Passing it in by reference is preferable: (std::string &)

3) 修改字符串但希望调用者看到该更改。通过引用传递它是可取的:(std::string &)

4) Sending the string into the function and the caller of the function will never use the string again. Using move semanticsmight be an option (std::string &&)

4) 将字符串发送到函数中,函数的调用者将永远不会再次使用该字符串。使用移动语义可能是一种选择(std::string &&)

回答by emsr

Check this answer for C++11. Basically, if you pass an lvalue the rvalue reference

检查C++11 这个答案。基本上,如果你传递一个左值右值引用

From this article:

这篇文章

void f1(String s) {
    vector<String> v;
    v.push_back(std::move(s));
}
void f2(const String &s) {
    vector<String> v;
    v.push_back(s);
}

"For lvalue argument, ‘f1' has one extra copy to pass the argument because it is by-value, while ‘f2' has one extra copy to call push_back. So no difference; for rvalue argument, the compiler has to create a temporary ‘String(L“”)' and pass the temporary to ‘f1' or ‘f2' anyway. Because ‘f2' can take advantage of move ctor when the argument is a temporary (which is an rvalue), the costs to pass the argument are the same now for ‘f1' and ‘f2'."

“对于左值参数,'f1' 有一个额外的副本来传递参数,因为它是按值传递的,而 'f2' 有一个额外的副本来调用 push_back。所以没有区别;对于右值参数,编译器必须创建一个临时的'String(L“”)' 并将临时值传递给 'f1' 或 'f2'。因为当参数是临时的(这是一个右值)时,'f2' 可以利用 move ctor,传递'f1' 和 'f2' 的参数现在相同。”

Continuing: " This means in C++11 we can get better performance by using pass-by-value approach when:

继续:“这意味着在 C++11 中,我们可以在以下情况下使用传值方法获得更好的性能:

  1. The parameter type supports move semantics - All standard library components do in C++11
  2. The cost of move constructor is much cheaper than the copy constructor (both the time and stack usage).
  3. Inside the function, the parameter type will be passed to another function or operation which supports both copy and move.
  4. It is common to pass a temporary as the argument - You can organize you code to do this more.
  1. 参数类型支持移动语义 - 所有标准库组件在 C++11 中都支持
  2. 移动构造函数的成本比复制构造函数便宜得多(时间和堆栈使用)。
  3. 在函数内部,参数类型将传递给另一个支持复制和移动的函数或操作。
  4. 传递一个临时变量作为参数是很常见的——你可以组织你的代码来做更多的事情。

"

OTOH, for C++98 it is best to pass by reference - less data gets copied around. Passing const or non const depend of whether you need to change the argument or not.

OTOH,对于 C++98,最好通过引用传递——更少的数据被复制。传递 const 或非 const 取决于您是否需要更改参数。

回答by Vaughn Cato

I believe the normal answer is that it should be passed by value if you need to make a copy of it in your function. Pass it by const reference otherwise.

我相信正常的答案是,如果您需要在函数中复制它,它应该按值传递。否则通过 const 引用传递它。

Here is a good discussion: http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

这是一个很好的讨论:http: //cpp-next.com/archive/2009/08/want-speed-pass-by-value/