Difference between type and use of std :: move ()?

I'm just curious about the new std :: move () function, which has just been added to the latest C ++ standard.

Having finished reading the article about this and found out that the definition of function

 namespace std {
      template <class T>
      inline typename remove_reference<T>::type&& move(T&& x)
      {
           return x;
      }
 }

There seems to be no difference between calling std :: move and using casting.

For instance,

class NPC{
    int m_number1;
    int m_number2;
public:
    NPC() : m_number1(1), m_number2(2) {
        cout << "NPC Constructor called!" << endl;
    }

    NPC(NPC&& _npc) : m_number1(_npc.m_number1), m_number2(_npc.m_number2) {
        _npc.m_number1 = 0;
        _npc.m_number2 = 0;

        cout << "NPC Move Constructor called!" << endl;
    }
};


int main() {
    NPC n1;
    NPC n2 = std::move(n1);

    cout << "----------------" << endl;
    NPC n3;
    NPC n4 = (NPC&&)n3;
    return 0;
}

Is it right to think that in principle there is no difference? Well, I'm pretty much sure that I'm right, but I also know that being too confident always causes unpleasant consequences.

Thanks in advance!

+5
source share
3 answers

std::move defined as a specialized composition:

static_cast<typename remove_reference<T>::type&&>(t)

, . , , std::move. && , move forward, .

+9

, .

, while goto s, ++ C, std::move , , , .

+5

Yes, std::moveit does nothing in terms of functionality. Its purpose is to convert the lvalue value to the x value, which makes the result movable. This is better than using a throw because it makes the goal understandable.

+2
source

All Articles