Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385224

Why is `T(const T&&)` called a move constructor?

[C++11: 12.8/3]: A non-template constructor for class X is a move constructor if its first parameter is of typeX&&, const X&&, volatile X&&, or const volatile X&&, and either there are no other parameters or else all other parameters have default arguments (8.3.6). [..]

Why is a constructor that takes a const rvalue reference called a "move constructor" by the standard? Surely it's self-evident that this prohibits meaningful move semantics in all but the most fringey cases?

"According to me", as the SO saying goes, T(const T&&) shouldn't be deemed a "move constructor" as such, since it's basically useless.

If anything, shouldn't it be called a copy constructor?

Upvotes: 6

Views: 965

Answers (1)

user743382
user743382

Reputation:

Here are some of the differences between move constructors and other constructors:

  • Move constructors can be defaulted
  • Move constructors don't prevent a type from being a "literal type"
  • Non-trivial move constructors prevent a type from being a "trivially copyable type"
  • Move constructors prevent the implicit move constructor from being generated
  • Move constructors may be automatically called by standard library functions

As far as I can tell, for all of those, not calling X(const X &&) a move constructor gives undesirable results.

You give an alternative: it might be called a copy constructor instead. That too seems to have undesirable results: it would suppress the implicit copy constructor.

Whether a move constructor actually moves doesn't matter. A POD type may have a move constructor too. It'll just be making a copy, but it's still called a move constructor.

Upvotes: 9

Related Questions