util: Make Assert work with any value

This commit is contained in:
MarcoFalke 2020-07-11 15:06:51 +02:00
parent 5f96bce9b7
commit fa53635381
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 11 additions and 1 deletions

View file

@ -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;

View file

@ -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