From: Giuliano Procida Date: Mon, 13 Jun 2022 14:25:31 +0000 (+0100) Subject: optional: minor improvements X-Git-Tag: libabigail-2.1~47 X-Git-Url: https://sourceware.org/git/?a=commitdiff_plain;h=ce7bd9f595960d05c4cf12bf2250907db792d00e;p=libabigail.git optional: minor improvements This change makes minor improvements to the optional class used with pre-C++17 compilers. - adds operator== and operator!= - adds various missing noexcept (but not constexpr) decorations - defines operator bool in terms of has_value Note that some constexpr decorations would require C++17 anyway. * include/abg-cxx-compat.h (optional): Add operator== and operator!=. Add noexcept decorations. Tweak operator bool. Reviewed-by: Matthias Maennich Signed-off-by: Giuliano Procida --- diff --git a/include/abg-cxx-compat.h b/include/abg-cxx-compat.h index 77418944..b5579220 100644 --- a/include/abg-cxx-compat.h +++ b/include/abg-cxx-compat.h @@ -45,7 +45,7 @@ public: optional(const T& value) : has_value_(true), value_(value) {} bool - has_value() const + has_value() const noexcept { return has_value_; } @@ -67,19 +67,19 @@ public: } const T& - operator*() const + operator*() const& noexcept { return value_; } T& - operator*() + operator*() & noexcept { return value_; } const T* - operator->() const + operator->() const noexcept { return &value_; } T* - operator->() + operator->() noexcept { return &value_; } optional& @@ -90,9 +90,27 @@ public: return *this; } - explicit operator bool() const { return has_value_; } + explicit operator bool() const noexcept { return has_value(); } }; +template +bool +operator==(const optional& lhs, const optional& rhs) +{ + if (!lhs.has_value() && !rhs.has_value()) + return true; + if (!lhs.has_value() || !rhs.has_value()) + return false; + return lhs.value() == rhs.value(); +} + +template +bool +operator!=(const optional& lhs, const optional& rhs) +{ + return !(lhs == rhs); +} + #endif }