move语义std :: move [英] move semantics std::move

查看:164
本文介绍了move语义std :: move的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太理解 std :: move 函数

template <class T>
typename remove_reference<T>::type&&
move(T&& a)
{
    return a;
}

为什么 remove_reference

推荐答案

想想如果 T 是一个左值引用,例如 MyClass& 。在这种情况下, T&& 将成为 MyClass& &&&&< / code>,并且由于参考折叠规则,将再次转换为 MyClass& 要实现正确的结果, typename remove_reference< MyClass&> :: type&&& 首先从类型中删除任何引用装饰,因此 MyClass& ; 映射到 MyClass ,然后将右值引用应用于它,产生 MyClass& code>。

Think about what happens if T is an lvalue reference, for example MyClass &. In that case, T && would become MyClass & &&, and due to reference collapsing rules, this would be transformed into MyClass & again. To achieve the right result, typename remove_reference<MyClass&>::type&& first removes any reference decorations from the type, so MyClass & is mapped to MyClass, and then the rvalue reference is applied to it, yielding MyClass &&.

这篇关于move语义std :: move的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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