Tobias Neukom
Tobias Neukom

Reputation: 245

Explicitly defaulted move constructor

According to the c++11 standard a default move constructor is only generated if:

Can I still explicitly default it? Seems to work correctly in clang. Like this for example:

class MyClass {
private:
  std::vector<int> ints;
public:
  MyClass(MyClass const& other) : ints(other.ints) {}
  MyClass(MyClass&& other) = default;
};

Upvotes: 11

Views: 5619

Answers (2)

Nicol Bolas
Nicol Bolas

Reputation: 474236

Yes, you can always explicitly invoke the default generation for functions that can be automatically generated with = default. That's what the syntax is for.

Upvotes: 4

Steve Jessop
Steve Jessop

Reputation: 279385

The motivation for that rule is that if the default copy constructor doesn't work for your class, then chances are the default move constructor won't work either (rule of 5, or whatever we're up to in C++11). So yes, you can explicitly default it, on your honor as a programmer that it'll work.

In your example code you could instead remove the copy constructor, since it does the same as the default.

Upvotes: 14

Related Questions