简体   繁体   English

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

[英]why std::move uses std::remove_reference?

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

std::move is declared as follows: std::move声明如下:

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

As far as my understanding goes, when code is templated, the deduction of T in typename T looses information about reference, so following: 至于我的理解去,当代码为模板,在扣除Ttypename T失去有关参考信息,因此以下内容:

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

when used like: 当像这样使用时:

int five=5;
someFunction(five);

then 然后

  • value is of type int& value的类型为int&
  • T is int Tint

or 要么

const float value = 5.25;
someFunction(value);

then 然后

  • value is of type const float& value类型为const float&
  • T is const float . Tconst float

If this is so, then there is no point in move declaration to declare returned type as: std::remove_reference<T>::type&& , because T is already not a reference. 如果是这样,那么在move声明中就没有必要将返回的类型声明为: std::remove_reference<T>::type&& ,因为T已经不是引用。

Moreover, if std::move takes as argument a reference (l-value reference in practice), then returning static_cast<T&&>(t) in std::move in fact due to reference collapsing will return l-value reference or r-value reference, so it will behave more like std::forward not move. 此外,如果std::move将引用作为参数(实际上是l值引用),则实际上由于引用折叠而在std::move中返回static_cast<T&&>(t)会返回l值引用或r-值引用,因此其行为将更像std::forward不动。 So what is a trick, that makes it work properly, that I do not understand? 那么,什么使我无法理解的窍门能够正常工作呢?

Your examples are incorrect: 您的示例不正确:

int five=5;
someFunction(five);

In this case, T is deduced as int& , not int . 在这种情况下, T推导为int& ,而不是int The same goes for the second example; 第二个例子也是如此。 T is deduced as const int& . T推导为const int&

As such, returning just T&& would mean T&& & , which is T& due to reference collapsing rules. 因此,仅返回T&&就意味着T&& & ,由于引用折叠规则,因此T&& &就是T&

This is why the std::remove_reference is required, to ensure that there are no references on the type to prevent reference collapsing from taking place. 这就是为什么需要std::remove_reference原因,以确保类型上没有引用以防止引用崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 std::move 中使用 std::remove_reference ? - Why is std::remove_reference used in std::move? 为什么没有_Remove_reference,std :: move()不起作用? - Why does std::move() not work without _Remove_reference? std::remove_reference 解释了吗? - std::remove_reference explained? 用decltype和/或std :: remove_reference调用析构函数 - Calling destructor with decltype and\or std::remove_reference std :: remove_reference有什么意义呢? - What's the point of std::remove_reference 带有代码片段的 std::remove_reference 说明 - std::remove_reference clarification with code snippet 为什么以不同的顺序使用std :: remove_reference和std :: remove_const会产生不同的结果? - Why does using std::remove_reference and std::remove_const in different order produce different results? 首先是std :: remove_reference还是std :: remove_cv? - std::remove_reference or std::remove_cv first? `typename std::remove_reference 之间有什么区别<t> ` 和 `constexpr 类型名 std::remove_reference<t> `?</t></t> - What are the differences between `typename std::remove_reference<T>` and `constexpr typename std::remove_reference<T>`? std :: decay和std :: remove_reference之间的区别 - Difference between std::decay and std::remove_reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM