From 1c37688ccda57ee5649d15f2861bbc635750273f Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Fri, 18 Dec 2020 11:20:03 -0800 Subject: [PATCH] Replaced some of the previous workarounds with feature tests As always with feature tests, they can only be used to aggressively adopt new features even before all compilers we need have the features -- the test lets us write a vestigial workaround for downllevel compilers, to be removed as soon as all compilers support the feature. --- include/gsl/assert | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/include/gsl/assert b/include/gsl/assert index e25e747..201b0d6 100644 --- a/include/gsl/assert +++ b/include/gsl/assert @@ -102,29 +102,32 @@ namespace details class contract_group { public: - // TODO: change this back to unconditionally noexcept - // again once all supported compilers allow it - using handler = void (*)() /*noexcept*/; +#ifdef __cpp_noexcept_function_type + using handler = void (*)() noexcept; +#else + using handler = void (*)(); +#endif contract_group (handler h) { set_handler(h); } - auto set_handler(handler h) -> handler { if (!h) h = []()noexcept{}; - return chandler.exchange(h); } + auto set_handler(handler h) -> handler { return chandler.exchange(h ? h : []()noexcept{}); } auto get_handler() -> handler { return chandler; } - constexpr void expects(bool b) { assertion(b); } - constexpr void ensures(bool b) { assertion(b); } - + constexpr void expects (bool b) { assertion(b); } + constexpr void ensures (bool b) { assertion(b); } private: constexpr void assertion(bool b) { if (!b) chandler.load()(); } - std::atomic chandler; + +#ifdef __cpp_guaranteed_copy_elision + std::atomic chandler; // current handler +#else + handler chandler; +#endif }; -// TODO: change this back to "auto static Blah = contract_group{ ... };" -// again once all supported compilers allow it -static contract_group Default { &gsl::details::terminate }; -static contract_group Bounds { Default.get_handler() }; -static contract_group Null { Default.get_handler() }; -static contract_group Testing { Default.get_handler() }; +auto static Default = contract_group( &gsl::details::terminate ); +auto static Bounds = contract_group( Default.get_handler() ); +auto static Null = contract_group( Default.get_handler() ); +auto static Testing = contract_group( Default.get_handler() ); } // namespace gsl