What do we call a constructor like this C::C(const C&&)? And is there any use of it other than prohibit move from const?

Viewed 132

I can have declaration of three constructors like

  • copy constructor C::C(const C&)
  • move constructor C::C(C&&)
  • C::C(const C&&)

in a class without ambiguity.

What do we call a constructor like C::C(const C&&)? Does it have some kind of nickname?

I can come up with =delete to explicitly prohibit users from unknowingly trying to move from const and restricting the automatic copy fallback effect. This reminds users what they are doing.

And is there any other use of it?

1 Answers
C::C(C const&&);

is what I call a const-qualified move constructor.

It informs the constructor that it (A) is coming from a discarded object, and (B) you are not allowed to mutate non-const components of the source object (as the object may actually be const, in which case mutating them is undefined behavior).

In the event that C contains mutable state, that state may be safely moved-from in some cases. In the event that it does not, it behaves much like a const&, unless the components of C have their own const&& move constructors.

In other circumstances, you may want to =delete it. I personally wouldn't.

Related