From fa5363538125d996ae5cede55f7f05e88701ace2 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Sat, 11 Jul 2020 15:06:51 +0200 Subject: util: Make Assert work with any value --- src/test/util_tests.cpp | 10 ++++++++++ src/util/check.h | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) 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 p_two = Assert(MakeUnique(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(check); }() #endif // BITCOIN_UTIL_CHECK_H -- cgit v1.2.3