std::move

源码 #

 1// /usr/include/c++/12.2.0/bits/move.h
 2  /**
 3   *  @brief  Convert a value to an rvalue.
 4   *  @param  __t  A thing of arbitrary type.
 5   *  @return The parameter cast to an rvalue-reference to allow moving it.
 6  */
 7  template<typename _Tp>
 8    _GLIBCXX_NODISCARD
 9    constexpr typename std::remove_reference<_Tp>::type&&
10    move(_Tp&& __t) noexcept
11    { return static_cast<typename std::remove_reference<_Tp>::type&&>(__t); }
  • 可以看到std::move仅对类型做了一次转换,变成右值
  • 在调用=时会匹配到移动赋值函数,所以对象内容会被转移(具体看对应的实现)
  • 如果仅使用std::move(xxx)没有任何效果