From 0cbb9e221d0f80deaa9420b6b7a63ceae8058072 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Thu, 17 Dec 2020 11:36:23 -0800 Subject: [PATCH] Removed double `.load` Require `chandler` to be never null by installing `[]()noexcept{}` as the handler if given a null pointer. This lets us remove the double test in `assertion`. --- include/gsl/assert | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/gsl/assert b/include/gsl/assert index 76e5344..9d0d3da 100644 --- a/include/gsl/assert +++ b/include/gsl/assert @@ -104,15 +104,16 @@ class contract_group { public: using handler = void (*)() noexcept; - contract_group(handler h) : chandler{h} { } - auto set_handler(handler h) -> handler { return chandler.exchange(h); } + contract_group(handler h) { set_handler(h); } + auto set_handler(handler h) -> handler { if (!h) h = []()noexcept{}; + return chandler.exchange(h); } auto get_handler() -> handler { return chandler; } auto expects(bool b) { assertion(b); } auto ensures(bool b) { assertion(b); } private: - auto assertion(bool b) { if (!b && chandler.load()) chandler.load()(); } + auto assertion(bool b) { if (!b) chandler.load()(); } std::atomic chandler; };