r/cpp_questions • • 21h ago

SOLVED When to use copy/move constructors?

SOLVED: There's already been a lot of good answers. I understand much better now. The video here and the reply here were especially helpful. Thanks everyone, great community I did not expect to get such good answers so quickly :)

I'm sort of new to C++ dev, but not at all new to programming. I have some experience in C and old style C++ (ie writing C++ as though it's C). But now I'm trying to properly learn more modern C++, I'm using C++ 20 at the moment.

I've spent a lot of time recently learning about lvalues and rvalues, copy and move semantics. This has been rough to grasp vs C where everything is by value and the only thing you really think about is whether you pass with a pointer or not.

At this point I think I understand how it works. I know how to implement copy constructors, move constructors, and how to use smart pointers like unique_ptr and shared_ptr. I also learned how to create custom deleters for wrapping C libraries so I can use smart pointers with them if I choose to.

The issue now is I feel like there are so many options it's hard to think of when you need each thing. Specifically I don't really understand the need for all this when you can use a smart pointer like unique_ptr, and my understanding is if you return a unique_ptr from a function it is already moved by default? Is that correct?

So when do you actually need a class to overload the move constructor? This is what I don't understand. And why not just pass by lvalue &reference instead? I know smart pointers don't have that option, but that's what std::move is for, right?

I guess it's like at this point I get the rvalue is a literal, temporary value, but I don't really understand what the benefit of passing in this manner is as opposed to passing by lvalue reference? And I don't understand WHEN to do it, because all the information I find focuses solely on the how but not the why.

EDIT: fixing code snippet formatting

7 Upvotes

13 comments sorted by

View all comments

2

u/IyeOnline 20h ago

Specifically I don't really understand the need for all this when you can use a smart pointer like unique_ptr

Guess how smart pointers are implemented? They are class types with move (and potentially copy) constructors that do things.

Similarly std::string is just a class that has a copy constructor (that creates a new copy of the text) and a move constructor (that takes the source objects owned char array).

nd my understanding is if you return a unique_ptr from a function it is already moved by default?

Yes. Nobably a unique_ptr cannot be copied. It has unique ownership over its pointee. When returning a value from a function, C++ will either elide the return object entirely (RVO/NRVO) or at least return by implicit move.

So when do you actually need a class to overload the move constructor?

You need to do that in the same situations where you would implement a custom destructor or copy constructor: Your class owns some resource that needs to be properly handled. In the case of move, there is two scenarions:

  • Your resource is unique can cannot be shared or copied (unique_ptr, handles, ...).
  • You want to allow for more efficient operations; moving a vector, string, ... if you dont need the old object anymore is much more efficient than copying the contents into a new allocation.

As a practical example, consider

std::vector<std::string> lines;
std::string line;
while ( std::getline( some_file, line ) ) {
  lines.push_back( std::move(line) ); 
  // or better: 
  // lines.push_back( std::exchange(line, {} ) );
}

Sure, you could read in the line, then copy that line variable into the vector. But you dont need this copy of the line anymore: Thus its better to just move it and avoid the copy.

1

u/hyperchompgames 20h ago

Thanks, so move is needed when you want to move struct or class members from an object you no longer need? And so you avoid iteration and maybe creating more memory, and instead just move the reference into the new object.

Thanks this helps I think.

3

u/IyeOnline 20h ago

Yes. But its worth noting that moving

  • still creates a new object (or changes a different one in the case of move-assignment)
  • Only really has any benefit if there is a move constructor or move assignment operator that can actually do such resource stealing. Moving an int or a struct Point { int x; int y } is functionally equivalent to copying. Moving a struct Person { std::string name; } on the other hand has a benefit, because std::string has a move constructor that can "steal resources".