aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2023-01-29 21:27:53 -0800
committerChris Robinson <[email protected]>2023-01-29 21:27:53 -0800
commitc79b633a1ca0ad754c6739ea14b6ef8bc5c66aad (patch)
treebf952b3a0b2a702b3aa9f6e4b750c290459c8f3f
parentab142bddf7bb81802a61e7acc3cffc932d5067ba (diff)
Use std::pointer_traits::to_address when available
-rw-r--r--common/almalloc.h17
1 files changed, 16 insertions, 1 deletions
diff --git a/common/almalloc.h b/common/almalloc.h
index b73b159b..809f4f1f 100644
--- a/common/almalloc.h
+++ b/common/almalloc.h
@@ -106,6 +106,17 @@ template<typename T, std::size_t N, typename U, std::size_t M>
constexpr bool operator!=(const allocator<T,N>&, const allocator<U,M>&) noexcept { return false; }
+namespace detail_ {
+ template<typename... Ts>
+ using void_t = void;
+
+ template<typename T, typename = void>
+ constexpr bool has_to_address = false;
+ template<typename T>
+ constexpr bool has_to_address<T,
+ void_t<decltype(std::pointer_traits<T>::to_address(std::declval<const T&>()))>> = true;
+} // namespace detail_
+
template<typename T>
constexpr T *to_address(T *p) noexcept
{
@@ -113,7 +124,11 @@ constexpr T *to_address(T *p) noexcept
return p;
}
-template<typename T>
+template<typename T, std::enable_if_t<detail_::has_to_address<T>,bool> = true>
+constexpr auto to_address(const T& p) noexcept
+{ return std::pointer_traits<T>::to_address(p); }
+
+template<typename T, std::enable_if_t<!detail_::has_to_address<T>,bool> = true>
constexpr auto to_address(const T& p) noexcept
{ return to_address(p.operator->()); }