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

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 8 02:05:32 PST 2026


Author: Hristo Hristov
Date: 2026-01-08T12:05:27+02:00
New Revision: 6c2c0b9da2b96e06048d52b4308636e667e08cf2

URL: https://github.com/llvm/llvm-project/commit/6c2c0b9da2b96e06048d52b4308636e667e08cf2
DIFF: https://github.com/llvm/llvm-project/commit/6c2c0b9da2b96e06048d52b4308636e667e08cf2.diff

LOG: [libc++][numeric]  Modernized `std::midpoint` (#174596)

- Some improvements to `std::midpoint` implementation: replaces
`enable_if` with concepts.

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

Towards #172124

---------

Co-authored-by: Hristo Hristov <zingam at outlook.com>

Added: 
    

Modified: 
    libcxx/include/__numeric/midpoint.h
    libcxx/test/libcxx/numerics/nodiscard.verify.cpp
    libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 2ba80e5cca07d..dc40dac47f273 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -12,16 +12,12 @@
 
 #include <__config>
 #include <__cstddef/ptr
diff _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>
 #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 <limits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -35,8 +31,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<_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>;
   constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
 
@@ -48,8 +45,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> && (sizeof(_Tp) > 0))
+[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp* midpoint(_Tp* __a, _Tp* __b) noexcept {
   return __a + std::midpoint(ptr
diff _t(0), __b - __a);
 }
 
@@ -64,7 +62,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..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
@@ -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,20 @@
 
 #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'}}
-
-//  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'}}
-
-    return 0;
+Incomplete* ip = nullptr;
+void* vp       = nullptr;
+
+void test() {
+  // expected-error at +1 {{no matching function for call to 'midpoint'}}
+  (void)std::midpoint(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'}}
+  (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'}}
 }


        


More information about the libcxx-commits mailing list