From 5dc7fae1199e90b875decfae7b91e83d27881c81 Mon Sep 17 00:00:00 2001 From: Dmitry Kobets <89153909+dmitrykobets-msft@users.noreply.github.com> Date: Tue, 9 May 2023 09:06:53 -0700 Subject: [PATCH] Use the implementation-defined strict total order for pointer comparisons with `not_null` (#1106) Using `<`,`<=`,`>`,`>=` to compare unrelated pointers gives an unspecified result according to the standard. This PR replaces the usage of these operators in `gsl::not_null` with the STL counterparts, which would leverage any implementation-defined strict total ordering for pointers. Resolves #880 --- include/gsl/pointers | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/include/gsl/pointers b/include/gsl/pointers index 5cf1cf7..9c65a96 100644 --- a/include/gsl/pointers +++ b/include/gsl/pointers @@ -175,34 +175,34 @@ auto operator!=(const not_null& lhs, template auto operator<(const not_null& lhs, - const not_null& rhs) noexcept(noexcept(lhs.get() < rhs.get())) - -> decltype(lhs.get() < rhs.get()) + const not_null& rhs) noexcept(noexcept(std::less<>{}(lhs.get(), rhs.get()))) + -> decltype(std::less<>{}(lhs.get(), rhs.get())) { - return lhs.get() < rhs.get(); + return std::less<>{}(lhs.get(), rhs.get()); } template auto operator<=(const not_null& lhs, - const not_null& rhs) noexcept(noexcept(lhs.get() <= rhs.get())) - -> decltype(lhs.get() <= rhs.get()) + const not_null& rhs) noexcept(noexcept(std::less_equal<>{}(lhs.get(), rhs.get()))) + -> decltype(std::less_equal<>{}(lhs.get(), rhs.get())) { - return lhs.get() <= rhs.get(); + return std::less_equal<>{}(lhs.get(), rhs.get()); } template auto operator>(const not_null& lhs, - const not_null& rhs) noexcept(noexcept(lhs.get() > rhs.get())) - -> decltype(lhs.get() > rhs.get()) + const not_null& rhs) noexcept(noexcept(std::greater<>{}(lhs.get(), rhs.get()))) + -> decltype(std::greater<>{}(lhs.get(), rhs.get())) { - return lhs.get() > rhs.get(); + return std::greater<>{}(lhs.get(), rhs.get()); } template auto operator>=(const not_null& lhs, - const not_null& rhs) noexcept(noexcept(lhs.get() >= rhs.get())) - -> decltype(lhs.get() >= rhs.get()) + const not_null& rhs) noexcept(noexcept(std::greater_equal<>{}(lhs.get(), rhs.get()))) + -> decltype(std::greater_equal<>{}(lhs.get(), rhs.get())) { - return lhs.get() >= rhs.get(); + return std::greater_equal<>{}(lhs.get(), rhs.get()); } // more unwanted operators