Acr*_*imp 5 c++ string move-semantics string-view c++17
我更喜欢const std::string &总是在我需要打std::string秒。但是最近,我突然发现根本不需要使用它const std::string &。该std::string_view会做的更好为只读字符串,并只std::string用move将否则(设置字符串字段等等)做的更好。
// Read-only string example
bool foo(std::string_view bar) {
return bar == "baz";
}
Run Code Online (Sandbox Code Playgroud)
// Non read-only string example
void MyClass::foo(std::string bar) {
this->baz = std::move(bar);
}
Run Code Online (Sandbox Code Playgroud)
在以上两种情况下,const std::string &不是最优的!
因此,我决定删除所有,const std::string &并用其中之一替换它们。所以我的问题是:
const std::string &会更好吗?const std::string &吗?谢谢。
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |