为什么 std::move 使用 std::remove_reference?

why std::move uses std::remove_reference?

根据http://en.cppreference.com/w/cpp/utility/move

std::move声明如下:

template <typename T>
std::remove_reference<T>::type&& move(T&& t);

据我了解,当代码被模板化时,typename T 中的 T 的推导会丢失有关引用的信息,因此如下:

template <typename T>
void someFunction(T&& value);

像这样使用时:

int five=5;
someFunction(five);

然后

const float value = 5.25;
someFunction(value);

然后

如果是这样,那么在移动声明中将 returned 类型声明为: std::remove_reference<T>::type&&,因为T已经不是引用了

此外,如果 std::move 将引用作为参数(实际上是左值引用),那么 returning static_cast<T&&>(t) in std::move 实际上是由于引用折叠将 return 左值引用或右值引用,因此它的行为更像是 std::forward 不动。那么什么是技巧,使它正常工作,我不明白?

您的示例不正确:

int five=5;
someFunction(five);

在这种情况下,T 被推断为 int&,而不是 int。第二个例子也是如此; T 推导为 const int&.

因此,仅返回 T&& 将意味着 T&& &,这是 T& 由于引用折叠规则。

这就是为什么需要 std::remove_reference 的原因,以确保没有对该类型的引用以防止发生引用崩溃。