Skip to content

Commit 1e4a3c0

Browse files
author
MarcoFalke
committed
Merge #21317: util: Make Assume() usable as unary expression
fa4ceba util: Make Assume() usable as unary expression (MarcoFalke) Pull request description: Assume shouldn't behave different at the call site depending on build flags. Currently compilation fails if it is used as expression. Fix that by using the lambda approach from `Assert()` without the `assert()`. ACKs for top commit: jnewbery: ACK fa4ceba practicalswift: cr ACK fa4ceba: patch looks correct and commit hash starts with `fa` Tree-SHA512: 9ec9ac8d410cdaf5e4e28df571a89e3d23d38e05a7027bb726cae3da6e9314734277e5a218e9e090cc17e10db763da71052c229ad642077ca5824ee42022f3ed
2 parents 786654a + fa4ceba commit 1e4a3c0

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/test/util_tests.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ BOOST_AUTO_TEST_CASE(util_check)
7777
const int two = *Assert(p_two);
7878
Assert(two == 2);
7979
Assert(true);
80+
// Check that Assume can be used as unary expression
81+
const bool result{Assume(two == 2)};
82+
Assert(result);
8083
}
8184

8285
BOOST_AUTO_TEST_CASE(util_criticalsection)

src/util/check.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ T get_pure_r_value(T&& val)
6969
#ifdef ABORT_ON_FAILED_ASSUME
7070
#define Assume(val) Assert(val)
7171
#else
72-
#define Assume(val) ((void)(val))
72+
#define Assume(val) ([&]() -> decltype(get_pure_r_value(val)) { auto&& check = (val); return std::forward<decltype(get_pure_r_value(val))>(check); }())
7373
#endif
7474

7575
#endif // BITCOIN_UTIL_CHECK_H

0 commit comments

Comments
 (0)