3 ms·
If you need a deep copy you also need to write a copy() function for the object without RAII. (Although one could argue that that is still RAII, just without op
by pingyong 7y ago
If you need a deep copy you also need to write a copy() function for the object without RAII. (Although one could argue that that is still RAII, just without operator overloading.) But in any case, for deep copies, the copy code needs to be somewhere.
If you don't need a deep copy (which honestly you basically never need once you already have some container classes), you can pretty much always just use an unique_ptr or write a unique_handle class or something to that regard, which means you never need to write any of the operators.
- klodolph 7y agoIf you just want a unique_ptr, then you should at least write the code to delete the relevant operators or make them private. Boilerplate, yes, but that is how idiomatic C++ code is written.
- pingyong 7y agoI'm not sure what you mean. If you have code like this struct Foo { unique_ptr p; }; Foo is automatically not copyable. That code is about is perfectly good idiomatic C++, with zero boilerplate. And that is how most classes actually end up looking in idiomatic C++, there's almost never the need to implement operators.