From e02baff283daee6b4c715c871a575a5f360eb2b6 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Fri, 18 Dec 2020 12:34:07 -0800 Subject: [PATCH] Remove minor code duplication --- include/gsl/assert | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/include/gsl/assert b/include/gsl/assert index 94c9967..58a4516 100644 --- a/include/gsl/assert +++ b/include/gsl/assert @@ -109,14 +109,11 @@ public: using handler = void (*)(); #endif - constexpr contract_group (handler h) : chandler(h ? h : []()noexcept{}) { } + constexpr contract_group (handler h) : chandler(sanitize(h)) { } #if __cplusplus >= 202002L - constexpr auto set_handler(handler h) -> handler { return std::exchange(chandler, h ? h : []()noexcept{}); } + constexpr auto set_handler(handler h) -> handler { return std::exchange(chandler, sanitize(h)); } #else // VESTIGIAL, remove when no longer needed for downlevel compilers - constexpr auto set_handler(handler h) -> handler { auto old = chandler; - chandler = h ? h : []()noexcept{}; - return old; - } + constexpr auto set_handler(handler h) -> handler { auto old = chandler; chandler = sanitize(h); return old; } #endif constexpr auto get_handler() -> handler { return chandler; } @@ -124,6 +121,7 @@ public: constexpr void ensures (bool b) { assertion(b); } private: constexpr void assertion(bool b) { if (!b) chandler(); } + constexpr auto sanitize(handler h) -> handler { return h ? h : []()noexcept{}; } handler chandler; };