Non-const copy constructors

In my last post I tried to show the general use of move-semantics. While writing it, I figured “Wait a minute, couldn’t I just use a non-const copy-constructor instead?”.

As can be seen in the following code snippet, yes it does work. Perfectly.

copy1
So why all the hazzle with these weird new move-semantics then? Short anser: principle of least astonishment (POLA).
Long anser: Consider the following piece of code:

Copy2Given the above definition, this code will crash. A regular programmer will say “Huh? But I just copied one image and multiplied with itself!”. And here we are. There is a reason we call it “copy constructor” – we expect it to copy. not to move.

Code should do exactly what expected and only what is expected.

Leave a comment