Copy, move, and assignment operators with a derived class

Started by
22 comments, last by Bregma 6 years, 6 months ago
6 hours ago, Bregma said:

If you absolutely must blindly follow rigid ideology

 

6 hours ago, Bregma said:

Your coworker's blind obedience to cargo-cult pass-by-const-reference

 

6 hours ago, Bregma said:

Really, other than deleting the copy assignment operator entirely or adding debug printfs, why would you ever use anything other than the two-line copy-and-swap I wrote above for all your classes?

Certainly you must see the irony among these statements?

Passing by value may often elide the copy for R-values, but not L-values. Passing by reference will always copy R-values, but L-values don't necessarily have to be copied if there are (exception-safe) optimizations that can be done during assignment. While I concede that a self-check isn't a strong case for preferring the latter approach, and I'll definitely be preferring pass-by-value in the general case going forward as it's more idiomatic, it should be obvious enough that both approaches are valid depending on the usage and implementation details of specific types.

It's a fool's errand to try an make an argument for either based on contrived usage and performance conditions, especially at the micro-optimization level. I wouldn't even entertain such an argument coming from a coworker. If this code ever does become an issue, I guarantee it's because your software has much bigger issues than assignment operators performing extra copies. But again, without real data there's no way to know.

Advertisement
On 14/10/2017 at 11:47 PM, Bregma said:

A copy assignment operator should always look like this.

Before I start blindly following this rigid ideology, what about cases where swapping is just as expensive as copying? 

1 minute ago, Hodgman said:

Before I start blindly following this rigid ideology, what about cases where swapping is just as expensive as copying? 

Under no circumstances should you question my obviously superior intellect.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement