Skip to content

Commit

Permalink
Use the implementation-defined strict total order for pointer compari…
Browse files Browse the repository at this point in the history
…sons 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
  • Loading branch information
dmitrykobets-msft committed May 9, 2023
1 parent 9face82 commit 5dc7fae
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions include/gsl/pointers
Original file line number Diff line number Diff line change
Expand Up @@ -175,34 +175,34 @@ auto operator!=(const not_null<T>& lhs,

template <class T, class U>
auto operator<(const not_null<T>& lhs,
const not_null<U>& rhs) noexcept(noexcept(lhs.get() < rhs.get()))
-> decltype(lhs.get() < rhs.get())
const not_null<U>& 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 <class T, class U>
auto operator<=(const not_null<T>& lhs,
const not_null<U>& rhs) noexcept(noexcept(lhs.get() <= rhs.get()))
-> decltype(lhs.get() <= rhs.get())
const not_null<U>& 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 <class T, class U>
auto operator>(const not_null<T>& lhs,
const not_null<U>& rhs) noexcept(noexcept(lhs.get() > rhs.get()))
-> decltype(lhs.get() > rhs.get())
const not_null<U>& 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 <class T, class U>
auto operator>=(const not_null<T>& lhs,
const not_null<U>& rhs) noexcept(noexcept(lhs.get() >= rhs.get()))
-> decltype(lhs.get() >= rhs.get())
const not_null<U>& 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
Expand Down

0 comments on commit 5dc7fae

Please sign in to comment.