Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move constructor cannot be defaulted

Tags:

I am new to c++11 and wrote the following class where I would like it to support std::move:

class X {
 public:
  X(int x) : x_(x) {}
  ~X() {
    printf("X(%d) has be released.\n", x_);
  }

  X(X&&) = default;
  X& operator = (X&&) = default;

  X(const X&) = delete;
  X& operator = (const X&) = delete;
 private:
  int x_;
};

However, when I compile it with option -std=c++0x, the compiler complains the following:

queue_test.cc:12: error: ‘X::X(X&&)’ cannot be defaulted
queue_test.cc:13: error: ‘X& X::operator=(X&&)’ cannot be defaulted

My questions are:

  1. Did I do something wrong which disallows class X to have a default move constructor?
  2. In case there exists a class that we really can't default its move constructor, can I know how should I develop its move constructor?

Thank you,