Why do I need static_cast and std::remove_reference in std::move if I pass an rvalue?

214 views Asked by At

I'm trying to understand more move semantic. Now this is a possible implementation of std::move:

template <typename T>
typename std::remove_reference<T>::type&& move_(T&& obj)
{
    return static_cast<typename std::remove_reference<T>::type&&>(obj);
}
  • But I've edited it a bit to understand its workings:

      template <typename T>
      typename std::remove_reference<T>::type&& move_(T&& obj)
      {
          if(std::is_lvalue_reference<decltype(obj)>::value)
              return static_cast<typename std::remove_reference<T>::type&&>(obj); 
          return obj; // dones't work?
          // return (string&&)obj; // ok works
      }
    
      auto s = move_(std::string("Hi there!")); // passing an rvalue cause instantiating: `string&&(string&&){}`. 
    

But why I can't directly return obj which is of rvalue-reference type after instantiation?

0

There are 0 answers