Skip to content

Conversation

@random-zebra
Copy link

@random-zebra random-zebra commented Dec 20, 2020

Fix the -Wdeprecated-copy warnings listed in #2076

Background
While perfectly valid in C++98, implicit generation of the copy constructor is declared as deprecated in C++11, when there is already a user-defined copy assignment operator, or vice-versa.
The rationale behind this is the "rule of three" (https://en.cppreference.com/w/cpp/language/rule_of_three)

If a class requires a user-defined destructor, a user-defined copy constructor, or a user-defined copy assignment operator, it almost certainly requires all three.

Issue - Fix
We have several places that define redundant copy assignment operators (and then use the implicit copy constructor), or that do need non-default ctors/dtors but don't define copy assignment.
(See also bitcoin#11161, which is also cherry-picked here)
Specifically:

  • CSignedMessage/CMasternodePing/CMasternode use a weird copy-assignment that swaps with the argument (which, of course, cannot be "const" then). Here we remove all ::swap implementations, and provide actual copy assignment operators.
  • CTransaction has an explicit copy-assignment operator, but uses the implicit copy-ctor in CMerkleTx constructor.
  • CFeeRate and CTxMemPoolEntry have explicitly defined copy ctor, which can (and should, for the rule-of-three) be replaced by the default one (Remove redundant explicitly defined copy ctors bitcoin/bitcoin#11161).

Note
Reason why these warnings pop up just now.
Since Clang 10.0 and GCC 9.3, -Wextra enables -Wdeprecated-copy by default, and that is very spammy, especially when compiling with the QT GUI (bitcoin#15822 / bitcoin#18419).
It is so spammy (also with boost-1.72.0) that Bitcoin decided to temporarily suppress this warning (bitcoin#18738) until QT fixes it upstream, and boost is definitely removed in 0.22 (bitcoin#18967)

random-zebra and others added 4 commits December 20, 2020 15:27
Also remove unneeded swap func in SignedMessage
CFeeRate and CTxMemPoolEntry have explicitly defined copy ctors which
has the same functionality as the implicit default copy ctors which
would have been generated otherwise.

Besides being redundant, it violates the rule of three (see
https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming) ).
(Of course, the rule of three doesn't -really- cause a resource
management issue here, but the reason for that is exactly that there is
no need for an explicit copy ctor in the first place since no resources
are being managed).
CFeeRate has an explicitly defined copy ctor which has the same
functionality as the implicit default copy ctor which would have been
generated otherwise.
Copy link

@furszy furszy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK ae41ebe

@random-zebra random-zebra added this to the 5.1.0 milestone Jan 17, 2021
@furszy furszy merged commit f6b14ca into PIVX-Project:master Jan 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants