Created
August 14, 2024 14:57
-
-
Save l0rinc/66f13938d867d82893d7ab7a2ebc5717 to your computer and use it in GitHub Desktop.
::move ternary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See related discussions: | |
* https://github.com/bitcoin/bitcoin/pull/28280#discussion_r1676800505 | |
* https://github.com/bitcoin/bitcoin/pull/28280#discussion_r1676801434 | |
* https://github.com/bitcoin/bitcoin/pull/17487#discussion_r402037193 | |
```diff | |
--- | |
diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp | |
--- a/src/test/coins_tests.cpp (revision 9f3bf64d29c497fb90406974548a717ba5918b25) | |
+++ b/src/test/coins_tests.cpp (date 1723646168861) | |
@@ -1114,4 +1114,33 @@ | |
PoolResourceTester::CheckAllDataAccountedFor(resource); | |
} | |
+void test_move_and_copy(bool move_condition) { | |
+ Coin coin1, coin2; | |
+ std::cout << "\nTesting " << (move_condition ? "Move" : "Copy") << " operations:\n"; | |
+ std::cout << "----------------------------------------\n"; | |
+ | |
+ std::cout << "1. Using if-else statement:\n"; | |
+ if (move_condition) { | |
+ coin1 = std::move(coin2); | |
+ } else { | |
+ coin1 = coin2; | |
+ } | |
+ | |
+ std::cout << "\n2. Using ternary operator with direct move:\n"; | |
+ coin1 = move_condition ? std::move(coin2) : coin2; | |
+ | |
+ std::cout << "\n3. Using ternary with parenthesized assignments:\n"; | |
+ move_condition ? (coin1 = std::move(coin2)) : (coin1 = coin2); | |
+ | |
+ std::cout << "----------------------------------------\n"; | |
+} | |
+ | |
+BOOST_AUTO_TEST_CASE(move_is_used) | |
+{ | |
+ bool move_condition = FastRandomContext().randbool(); | |
+ | |
+ test_move_and_copy(move_condition); | |
+ test_move_and_copy(!move_condition); | |
+} | |
+ | |
BOOST_AUTO_TEST_SUITE_END() | |
IDEA additional info: | |
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
<+>UTF-8 | |
=================================================================== | |
diff --git a/src/coins.h b/src/coins.h | |
--- a/src/coins.h (revision 9f3bf64d29c497fb90406974548a717ba5918b25) | |
+++ b/src/coins.h (date 1723646270511) | |
@@ -21,6 +21,7 @@ | |
#include <functional> | |
#include <unordered_map> | |
+#include <iostream> | |
/** | |
* A UTXO entry. | |
@@ -42,8 +43,44 @@ | |
uint32_t nHeight : 31; | |
//! construct a Coin from a CTxOut and height/coinbase information. | |
- Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {} | |
- Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {} | |
+ Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) { | |
+ std::cout << "CTxOut rvalue constructor\n"; | |
+ } | |
+ | |
+ Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) { | |
+ std::cout << "CTxOut lvalue constructor\n"; | |
+ } | |
+ | |
+ Coin(Coin&& other) noexcept { | |
+ out = std::move(other.out); | |
+ fCoinBase = other.fCoinBase; | |
+ nHeight = other.nHeight; | |
+ std::cout << "Move constructor\n"; | |
+ } | |
+ | |
+ Coin& operator=(Coin&& other) noexcept { | |
+ if (this != &other) { | |
+ out = std::move(other.out); | |
+ fCoinBase = other.fCoinBase; | |
+ nHeight = other.nHeight; | |
+ std::cout << "Move assignment\n"; | |
+ } | |
+ return *this; | |
+ } | |
+ | |
+ Coin(const Coin& other) noexcept : out(other.out), fCoinBase(other.fCoinBase), nHeight(other.nHeight) { | |
+ std::cout << "Copy constructor\n"; | |
+ } | |
+ | |
+ Coin& operator=(const Coin& other) noexcept { | |
+ if (this != &other) { | |
+ out = other.out; | |
+ fCoinBase = other.fCoinBase; | |
+ nHeight = other.nHeight; | |
+ std::cout << "Copy assignment\n"; | |
+ } | |
+ return *this; | |
+ } | |
``` | |
Applying the patch and running the new test: | |
``` | |
Testing Copy operations: | |
---------------------------------------- | |
1. Using if-else statement: | |
Copy assignment | |
2. Using ternary operator with direct move: | |
Copy constructor | |
Move assignment | |
3. Using ternary with parenthesized assignments: | |
Copy assignment | |
---------------------------------------- | |
Testing Move operations: | |
---------------------------------------- | |
1. Using if-else statement: | |
Move assignment | |
2. Using ternary operator with direct move: | |
Move constructor | |
Move assignment | |
3. Using ternary with parenthesized assignments: | |
Move assignment | |
---------------------------------------- | |
``` | |
i.e. `move_condition ? (coin1 = std::move(coin2)) : (coin1 = coin2)` behaves the same way as the explicit if expression. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment