c++ - 性能比较:f(std::string&&) vs f(T&&)
问题描述
我试图了解使用WidgetURef::setName
(URef
作为通用参考,Scott Meyers 创造的术语)与WidgedRRef::setName
(RRef
作为 R 值参考)的性能影响:
#include <string>
class WidgetURef {
public:
template<typename T>
void setName(T&& newName)
{
name = std::move(newName);
}
private:
std::string name;
};
class WidgetRRef {
public:
void setName(std::string&& newName)
{
name = std::move(newName);
}
private:
std::string name;
};
int main() {
WidgetURef w_uref;
w_uref.setName("Adela Novak");
WidgetRRef w_rref;
w_rref.setName("Adela Novak");
}
我确实很欣赏应该使用通用引用来std::forward
代替,但这只是一个(不完美的)示例来突出有趣的部分。
问题
在这个特定示例中,使用一种实现与另一种实现的性能影响是什么?虽然WidgetURef
需要类型推导,但在其他方面与 相同WidgetRRef
,不是吗?至少在这种特殊情况下,在这两种情况下,参数都是r-value
引用,因此不会创建临时对象。这个推理正确吗?
上下文
该示例取自 Scott Meyers 的“Effective Modern C++”(第 170 页)的 Item25。根据这本书(前提是我的理解是正确的!),采用通用引用的版本T&&
不需要临时对象,而另一个版本则需要std::string&&
。我真的不明白为什么。
解决方案
setName(T&& newName)
with 参数"Adela Novak"
得到 T 推断const char (&)[12]
,然后分配给std::string
.
setName(std::string&& newName)
with 参数"Adela Novak"
创建一个临时std::string
对象,然后将其分配给std::string
.
第一个在这里更有效,因为不涉及移动。
推荐阅读
- angular - 带有 rxjs 问题的 Angular 11 - TS2769:没有重载匹配此调用
- android - Android撰写自定义主题颜色 - 文本颜色未选择主题颜色
- java - 如何在 Redis 中缓存嵌套字典以供用 Python 和 Java 编写的应用程序使用
- angular - Angular 9:放置文件未触发
- java - 从文件列表创建 zip(包括路径文件夹)
- asp.net-core - ASP.NET Core 中的开发证书身份验证问题
- reactjs - 在“POST”请求中,我的 createPost Thunk 函数给出错误 {type: "post/createPost/pending", payload: undefined}
- javascript - 切换到另一个后如何更改文本输入边框颜色
- python - 如何在 docker 中运行 pytorch 模型服务器?
- angular - Angular 导航丢失 main.js 等