[libcxx-commits] [libcxx] [libc++][numeric] Modernized `std::midpoint` (PR #174596)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jan 7 09:13:31 PST 2026


https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/174596

>From cd94663230cb2c6ed297345ef367ed1cffa48445 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 6 Jan 2026 16:16:08 +0200
Subject: [PATCH 1/5] [libc++][numerics] Modernized `std::midpoint`

https://wg21.link/numeric.ops.midpoint

Towards #172124
---
 libcxx/include/__numeric/midpoint.h           | 14 ++++++----
 .../test/libcxx/numerics/nodiscard.verify.cpp | 13 +++++++++
 .../numeric.ops.midpoint/midpoint.verify.cpp  | 28 +++++++++----------
 3 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 2ba80e5cca07d..2a8558b3a3bc4 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -12,7 +12,6 @@
 
 #include <__config>
 #include <__cstddef/ptrdiff_t.h>
-#include <__type_traits/enable_if.h>
 #include <__type_traits/is_floating_point.h>
 #include <__type_traits/is_integral.h>
 #include <__type_traits/is_null_pointer.h>
@@ -35,8 +34,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
 template <class _Tp>
-_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>, _Tp>
-midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
+  requires(is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>)
+[[nodiscard]]
+_LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
   using _Up                = make_unsigned_t<_Tp>;
   constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
 
@@ -48,8 +48,9 @@ midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
   return __a + __half_diff;
 }
 
-template <class _Tp, enable_if_t<is_object_v<_Tp> && !is_void_v<_Tp> && (sizeof(_Tp) > 0), int> = 0>
-_LIBCPP_HIDE_FROM_ABI constexpr _Tp* midpoint(_Tp* __a, _Tp* __b) noexcept {
+template <class _Tp>
+  requires(is_object_v<_Tp> && !is_void_v<_Tp> && (sizeof(_Tp) > 0))
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp* midpoint(_Tp* __a, _Tp* __b) noexcept {
   return __a + std::midpoint(ptrdiff_t(0), __b - __a);
 }
 
@@ -64,7 +65,8 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Fp __fp_abs(_Fp __f) {
 }
 
 template <class _Fp>
-_LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<is_floating_point_v<_Fp>, _Fp> midpoint(_Fp __a, _Fp __b) noexcept {
+  requires(is_floating_point_v<_Fp>)
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Fp midpoint(_Fp __a, _Fp __b) noexcept {
   constexpr _Fp __lo = numeric_limits<_Fp>::min() * 2;
   constexpr _Fp __hi = numeric_limits<_Fp>::max() / 2;
 
diff --git a/libcxx/test/libcxx/numerics/nodiscard.verify.cpp b/libcxx/test/libcxx/numerics/nodiscard.verify.cpp
index 10da62feca7c0..fe59e6a6a3fa7 100644
--- a/libcxx/test/libcxx/numerics/nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/numerics/nodiscard.verify.cpp
@@ -32,4 +32,17 @@ void test() {
   std::saturate_cast<signed int>(49); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
 #endif // TEST_STD_VER >= 26
   // clang-format on
+
+#if TEST_STD_VER >= 20
+  {
+    int arr[]{94, 82, 49};
+
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::midpoint(94, 82);
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::midpoint(arr, arr + 2);
+    // expected-warning at +1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+    std::midpoint(94.0, 82.0);
+  }
+#endif
 }
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
index 7028e0f32945c..2d138c8d7fd08 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
@@ -6,7 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// UNSUPPORTED: c++03, c++11, c++14, c++17
+// REQUIRES: std-at-least-c++20
+
 // <numeric>
 
 // template <class _Tp>
@@ -18,22 +19,19 @@
 
 #include "test_macros.h"
 
-int func1 () { return 1; }
-int func2 () { return 2; }
+int func1() { return 1; }
+int func2() { return 2; }
 
 struct Incomplete;
-Incomplete *ip = nullptr;
-void       *vp = nullptr;
-
-int main(int, char**)
-{
-    (void) std::midpoint(false, true);  // expected-error {{no matching function for call to 'midpoint'}}
+Incomplete* ip = nullptr;
+void* vp       = nullptr;
 
-//  A couple of odd pointer types that should fail
-    (void) std::midpoint(nullptr, nullptr);  // expected-error {{no matching function for call to 'midpoint'}}
-    (void) std::midpoint(func1, func2);      // expected-error {{no matching function for call to 'midpoint'}}
-    (void) std::midpoint(ip, ip);            // expected-error {{no matching function for call to 'midpoint'}}
-    (void) std::midpoint(vp, vp);            // expected-error {{no matching function for call to 'midpoint'}}
+void test() {
+  (void)std::midpoint(false, true); // expected-error {{no matching function for call to 'midpoint'}}
 
-    return 0;
+  //  A couple of odd pointer types that should fail
+  (void)std::midpoint(nullptr, nullptr); // expected-error {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint(func1, func2);     // expected-error {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint(ip, ip);           // expected-error {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint(vp, vp);           // expected-error {{no matching function for call to 'midpoint'}}
 }

>From 4230418f9ed74372c138f6707c6207639339dada Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Tue, 6 Jan 2026 22:29:24 +0200
Subject: [PATCH 2/5] Addressed comments

---
 libcxx/include/__numeric/midpoint.h                       | 8 +++-----
 .../numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp  | 3 ++-
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 2a8558b3a3bc4..8b379f47c5684 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -14,13 +14,11 @@
 #include <__cstddef/ptrdiff_t.h>
 #include <__type_traits/is_floating_point.h>
 #include <__type_traits/is_integral.h>
-#include <__type_traits/is_null_pointer.h>
 #include <__type_traits/is_object.h>
-#include <__type_traits/is_pointer.h>
 #include <__type_traits/is_same.h>
 #include <__type_traits/is_void.h>
 #include <__type_traits/make_unsigned.h>
-#include <__type_traits/remove_pointer.h>
+#include <__type_traits/remove_cv.h>
 #include <limits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -34,7 +32,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
 template <class _Tp>
-  requires(is_integral_v<_Tp> && !is_same_v<bool, _Tp> && !is_null_pointer_v<_Tp>)
+  requires(is_integral_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, bool>)
 [[nodiscard]]
 _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
   using _Up                = make_unsigned_t<_Tp>;
@@ -49,7 +47,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_
 }
 
 template <class _Tp>
-  requires(is_object_v<_Tp> && !is_void_v<_Tp> && (sizeof(_Tp) > 0))
+  requires(is_object_v<_Tp> && (sizeof(_Tp) > 0))
 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp* midpoint(_Tp* __a, _Tp* __b) noexcept {
   return __a + std::midpoint(ptrdiff_t(0), __b - __a);
 }
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
index 2d138c8d7fd08..a72067a77d125 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
@@ -27,7 +27,8 @@ Incomplete* ip = nullptr;
 void* vp       = nullptr;
 
 void test() {
-  (void)std::midpoint(false, true); // expected-error {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint(false, true);             // expected-error {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint<const bool>(false, true); // expected-error {{no matching function for call to 'midpoint'}}
 
   //  A couple of odd pointer types that should fail
   (void)std::midpoint(nullptr, nullptr); // expected-error {{no matching function for call to 'midpoint'}}

>From fdfa5588ecbb8db942620a8d1c9f81afa14a2e80 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Wed, 7 Jan 2026 09:52:19 +0200
Subject: [PATCH 3/5] Addressed review comments

---
 libcxx/docs/Status/Cxx2cIssues.csv                              | 2 +-
 .../numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp        | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/libcxx/docs/Status/Cxx2cIssues.csv b/libcxx/docs/Status/Cxx2cIssues.csv
index 9445149a203a2..e2e3caf893ae4 100644
--- a/libcxx/docs/Status/Cxx2cIssues.csv
+++ b/libcxx/docs/Status/Cxx2cIssues.csv
@@ -163,7 +163,7 @@
 "`LWG4255 <https://wg21.link/LWG4255>`__","``move_only_function`` constructor should recognize empty ``copyable_function``\s","2025-11 (Kona)","","","`#171320 <https://github.com/llvm/llvm-project/issues/171320>`__",""
 "`LWG4256 <https://wg21.link/LWG4256>`__","Incorrect constrains for ``function_ref`` constructors from ``nontype_t``","2025-11 (Kona)","","","`#171321 <https://github.com/llvm/llvm-project/issues/171321>`__",""
 "`LWG4257 <https://wg21.link/LWG4257>`__","Stream insertion for ``chrono::local_time`` should be constrained","2025-11 (Kona)","","","`#171322 <https://github.com/llvm/llvm-project/issues/171322>`__",""
-"`LWG4260 <https://wg21.link/LWG4260>`__","Query objects must be default constructible","2025-11 (Kona)","","","`#171323 <https://github.com/llvm/llvm-project/issues/171323>`__",""
+"`LWG4260 <https://wg21.link/LWG4260>`__","Query objects must be default constructible","2025-11 (Kona)","|Complete|","22","`#171323 <https://github.com/llvm/llvm-project/issues/171323>`__",""
 "`LWG4265 <https://wg21.link/LWG4265>`__","``std::midpoint`` should not accept ``const bool``","2025-11 (Kona)","","","`#171324 <https://github.com/llvm/llvm-project/issues/171324>`__",""
 "`LWG4266 <https://wg21.link/LWG4266>`__","``layout_stride::mapping`` should treat empty mappings as exhaustive","2025-11 (Kona)","","","`#171325 <https://github.com/llvm/llvm-project/issues/171325>`__",""
 "`LWG4269 <https://wg21.link/LWG4269>`__","``unique_copy`` passes arguments to its predicate backwards","2025-11 (Kona)","","","`#171326 <https://github.com/llvm/llvm-project/issues/171326>`__",""
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
index a72067a77d125..379bb27d66853 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
@@ -29,6 +29,8 @@ void* vp       = nullptr;
 void test() {
   (void)std::midpoint(false, true);             // expected-error {{no matching function for call to 'midpoint'}}
   (void)std::midpoint<const bool>(false, true); // expected-error {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint<const volatile bool>(false, true); // expected-error {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint<volatile bool>(false, true); // expected-error {{no matching function for call to 'midpoint'}}
 
   //  A couple of odd pointer types that should fail
   (void)std::midpoint(nullptr, nullptr); // expected-error {{no matching function for call to 'midpoint'}}

>From d65ca8daa258db8cd718b6a98abe59cbadedf436 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Wed, 7 Jan 2026 10:03:35 +0200
Subject: [PATCH 4/5] Formatting

---
 .../numeric.ops.midpoint/midpoint.verify.cpp         | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
index 379bb27d66853..62a2a2db0231c 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
@@ -27,10 +27,14 @@ Incomplete* ip = nullptr;
 void* vp       = nullptr;
 
 void test() {
-  (void)std::midpoint(false, true);             // expected-error {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<const bool>(false, true); // expected-error {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<const volatile bool>(false, true); // expected-error {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<volatile bool>(false, true); // expected-error {{no matching function for call to 'midpoint'}}
+  // expected-error at +1 {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint(false, true);
+  // expected-error at +1 {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint<const bool>(false, true);
+  // expected-error at +1 {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint<const volatile bool>(false, true);
+  // expected-error at +1 {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint<volatile bool>(false, true);
 
   //  A couple of odd pointer types that should fail
   (void)std::midpoint(nullptr, nullptr); // expected-error {{no matching function for call to 'midpoint'}}

>From 75028b1c7628de97fed470d1167473cb3bb942f7 Mon Sep 17 00:00:00 2001
From: Hristo Hristov <hghristov.rmm at gmail.com>
Date: Wed, 7 Jan 2026 19:11:44 +0200
Subject: [PATCH 5/5] Revert LWG4265 changes

---
 libcxx/include/__numeric/midpoint.h                         | 3 +--
 .../numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp    | 6 ------
 2 files changed, 1 insertion(+), 8 deletions(-)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 8b379f47c5684..dc40dac47f273 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -18,7 +18,6 @@
 #include <__type_traits/is_same.h>
 #include <__type_traits/is_void.h>
 #include <__type_traits/make_unsigned.h>
-#include <__type_traits/remove_cv.h>
 #include <limits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -32,7 +31,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 #if _LIBCPP_STD_VER >= 20
 template <class _Tp>
-  requires(is_integral_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, bool>)
+  requires(is_integral_v<_Tp> && !is_same_v<_Tp, bool>)
 [[nodiscard]]
 _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
   using _Up                = make_unsigned_t<_Tp>;
diff --git a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
index 62a2a2db0231c..35ef436a29ebb 100644
--- a/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp
@@ -29,12 +29,6 @@ void* vp       = nullptr;
 void test() {
   // expected-error at +1 {{no matching function for call to 'midpoint'}}
   (void)std::midpoint(false, true);
-  // expected-error at +1 {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<const bool>(false, true);
-  // expected-error at +1 {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<const volatile bool>(false, true);
-  // expected-error at +1 {{no matching function for call to 'midpoint'}}
-  (void)std::midpoint<volatile bool>(false, true);
 
   //  A couple of odd pointer types that should fail
   (void)std::midpoint(nullptr, nullptr); // expected-error {{no matching function for call to 'midpoint'}}



More information about the libcxx-commits mailing list