gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:07:38 25 4
gpt4 key购买 nike

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

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

最佳答案

根据您对字符串所做的操作,有多个答案。

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

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

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

4) 将字符串发送到函数中,函数的调用者将不再使用该字符串。使用 move semantics可能是一个选项 (std::string &&)

关于c++ - 通过值或引用传递 std::string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10789740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com