Search code examples
c++c++11movemove-semantics

can I use std::move with class that doesn't provide a move constructor?


I have a class like this:

class myClass
{
    int x[1000];
 public:
     int &getx(int i)
    {
        return x[i];
    }
}

Please note that I did not provide a move construct here.

if I use the following code:

myClass A;
auto B=std::move(a);

does A moves to B or since I did not provided a move constructor, A is copied to B?

Is there any default move constructor for an object? If yes, how it does work with pointers and dynamically allocated arrays?


Solution

  • Although you did not provide explicit move constructor, the compiler provided implicit move constructor to you.

    If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if

    • X does not have a user-declared copy constructor, and
    • X does not have a user-declared copy assignment operator,
    • X does not have a user-declared move assignment operator,
    • X does not have a user-declared destructor, and
    • the move constructor would not be implicitly defined as deleted.

    So the answer on your question is: no, A is not copied to B.

    It is not clear what you ask in other questions. Please specify more details.