diff options
author | MarcoFalke <falke.marco@gmail.com> | 2020-07-15 09:03:37 +0200 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2020-07-15 09:03:41 +0200 |
commit | 3d57015aa228f3504ce056243c8b8dfd74fd8c76 (patch) | |
tree | 7257e64f2bb45f65367f6be30e17f0e2eb71282f /src | |
parent | 032cfdc9a24a603de4e746d199f69263cb3028d1 (diff) | |
parent | fa5363538125d996ae5cede55f7f05e88701ace2 (diff) |
Merge #19491: util: Make Assert work with any value
fa5363538125d996ae5cede55f7f05e88701ace2 util: Make Assert work with any value (MarcoFalke)
Pull request description:
Goal is to avoid compile failures
ACKs for top commit:
jonatack:
ACK fa5363538125d996ae5cede55f7f05e88701ace2
ryanofsky:
Code review ACK fa5363538125d996ae5cede55f7f05e88701ace2. Looks like if argument is an lvalue this effectively does:
Tree-SHA512: a5cf47a8bb2fa1bd8b8895774f33de50ad803165d6f7b520351be1cfcd5612d5d97c51d118461331d30640186c470879e5ad19e3333e09e72685c5e4e4f23079
Diffstat (limited to 'src')
-rw-r--r-- | src/test/util_tests.cpp | 10 | ||||
-rw-r--r-- | src/util/check.h | 2 |
2 files changed, 11 insertions, 1 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 257328974b..e247c09a97 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -41,6 +41,16 @@ namespace BCLog { BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup) +BOOST_AUTO_TEST_CASE(util_check) +{ + // Check that Assert can forward + const std::unique_ptr<int> p_two = Assert(MakeUnique<int>(2)); + // Check that Assert works on lvalues and rvalues + const int two = *Assert(p_two); + Assert(two == 2); + Assert(true); +} + BOOST_AUTO_TEST_CASE(util_criticalsection) { RecursiveMutex cs; diff --git a/src/util/check.h b/src/util/check.h index 3d534fd33e..9edf394492 100644 --- a/src/util/check.h +++ b/src/util/check.h @@ -54,6 +54,6 @@ T get_pure_r_value(T&& val) } /** Identity function. Abort if the value compares equal to zero */ -#define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }() +#define Assert(val) [&]() -> decltype(get_pure_r_value(val)) { auto&& check = (val); assert(#val && check); return std::forward<decltype(get_pure_r_value(val))>(check); }() #endif // BITCOIN_UTIL_CHECK_H |