通过Value或Reference传递std :: string [英] Passing std::string by Value or Reference

查看:112
本文介绍了通过Value或Reference传递std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我应该通过 std :: string 按值或通过引用(对于未内联函数)如果支持移动语义?使用小字符串优化(SSO)的实现怎么样?

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

推荐答案

1)使用字符串作为id(不会被修改)。在const引用中传递它可能是最好的想法:(std :: string const&)

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&)

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

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

3)修改字符串,调用者看到这个变化。通过引用传递它是优选的:(std :: string&)

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

4)该函数的函数和调用者将不再使用该字符串。使用移动语义可能是选项(std :: string&&&)

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

这篇关于通过Value或Reference传递std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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