Skip to content

Commit 3d57015

Browse files
author
MarcoFalke
committed
Merge #19491: util: Make Assert work with any value
fa53635 util: Make Assert work with any value (MarcoFalke) Pull request description: Goal is to avoid compile failures ACKs for top commit: jonatack: ACK fa53635 ryanofsky: Code review ACK fa53635. Looks like if argument is an lvalue this effectively does: Tree-SHA512: a5cf47a8bb2fa1bd8b8895774f33de50ad803165d6f7b520351be1cfcd5612d5d97c51d118461331d30640186c470879e5ad19e3333e09e72685c5e4e4f23079
2 parents 032cfdc + fa53635 commit 3d57015

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/test/util_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ namespace BCLog {
4141

4242
BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
4343

44+
BOOST_AUTO_TEST_CASE(util_check)
45+
{
46+
// Check that Assert can forward
47+
const std::unique_ptr<int> p_two = Assert(MakeUnique<int>(2));
48+
// Check that Assert works on lvalues and rvalues
49+
const int two = *Assert(p_two);
50+
Assert(two == 2);
51+
Assert(true);
52+
}
53+
4454
BOOST_AUTO_TEST_CASE(util_criticalsection)
4555
{
4656
RecursiveMutex cs;

src/util/check.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ T get_pure_r_value(T&& val)
5454
}
5555

5656
/** Identity function. Abort if the value compares equal to zero */
57-
#define Assert(val) [&]() -> decltype(get_pure_r_value(val))& { auto& check = (val); assert(#val && check); return check; }()
57+
#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); }()
5858

5959
#endif // BITCOIN_UTIL_CHECK_H

0 commit comments

Comments
 (0)