Copy and Move Semantics
Things to understand
-
lvalue, xvalue, prvalue
https://en.cppreference.com/w/cpp/language/value_category
on the same note, std::move : https://en.cppreference.com/w/cpp/utility/move -
implicit declaration/deletion of copy and move constructors
https://en.cppreference.com/w/cpp/language/copy_constructor
https://en.cppreference.com/w/cpp/language/move_constructor
https://en.cppreference.com/w/cpp/language/overload_resolution -
copy elision https://en.cppreference.com/w/cpp/language/copy_elision
-
MUST READ. std::move on const lvalue. ref
What I learned after wasting 2 days on same thing
To summarise:
A const valued argument can not be passed to a ‘non-const’ valued parameterised method
Please refer to the code examples which also include comments for better understanding:
This image is really important:
Ref: https://www.nextptr.com/tutorial/ta1211389378/beware-of-using-stdmove-on-a-const-lvalue
Since Passing by reference is like passing a * const
, same rules are also applicable to pointers
Literals are converted to equivalent primitives
String literals, can be converted to const char*
(recommended) or char *
(deprecated)