Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guaranteed copy elision paper's use of void in a constructor

Tags:

c++

c++17

In the paper P0135R0 there is an example:

struct NonMoveable {
  NonMoveable(int);
  NonMoveable(NonMoveable&) = delete;
  void NonMoveable(NonMoveable&) = delete;
  std::array<int, 1024> arr;
};
NonMoveable make() {
  return NonMoveable(42); // ok, directly constructs returned object
}
auto nm = make(); // ok, directly constructs 'nm'

This confused me:

void NonMoveable(NonMoveable&) = delete;

What is it? How constructor can be void?

UPD. Someone linked probable answer - No! This question is totally different.

like image 640
vladon Avatar asked Jul 16 '16 22:07

vladon


People also ask

Is copy elision guaranteed?

In C++20, the only copy allowed in the example is the one at line 3 (actually, x is implicitly moved from). Copy elision (NRVO) is allowed there and is routinely performed by most compilers, but is still non-guaranteed, and the widget class cannot be non-copyable non-movable.

What is a guaranteed copy?

Guaranteed copy elision redefines a number of C++ concepts, such that certain circumstances where copies/moves could be elided don't actually provoke a copy/move at all. The compiler isn't eliding a copy; the standard says that no such copying could ever happen. Consider this function: T Func() {return T();}


1 Answers

That "void" is what we would call a "typo". The intent was likely to remove the move assignment operator (though not strictly necessary, since deleting the copy constructor would do that). Considering that the person wrote "void", it's not surprising that the person also missed the && part, and forgot the const in the copy constructor's parameter (also not strictly necessary).

Basically, there's a lot wrong there; someone wrote it in a hurry.

like image 153
Nicol Bolas Avatar answered Oct 17 '22 01:10

Nicol Bolas