[libcxx-commits] [libcxx] [libc++][numeric] Modernized `std::midpoint` (PR #174596)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jan 6 06:50:45 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Hristo Hristov (H-G-Hristov)
<details>
<summary>Changes</summary>
Some improvements to `std::midpoint` implementation and tests as a per-requisite for LWG4265 #<!-- -->171324 / PR #<!-- -->174579
https://wg21.link/numeric.ops.midpoint
Towards #<!-- -->172124
---
Full diff: https://github.com/llvm/llvm-project/pull/174596.diff
3 Files Affected:
- (modified) libcxx/include/__numeric/midpoint.h (+8-6)
- (modified) libcxx/test/libcxx/numerics/nodiscard.verify.cpp (+13)
- (modified) libcxx/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.verify.cpp (+13-15)
``````````diff
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'}}
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/174596
More information about the libcxx-commits
mailing list