Add additional constraints on midpoint(pointer, pointer). Fixes PR#42037.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@361970 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/numeric b/include/numeric
index 4b08611..62cc29c 100644
--- a/include/numeric
+++ b/include/numeric
@@ -527,7 +527,7 @@
#if _LIBCPP_STD_VER > 17
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
-enable_if_t<is_integral_v<_Tp> && !is_same_v<bool, _Tp>, _Tp>
+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
{
@@ -548,7 +548,10 @@
template <class _TPtr>
_LIBCPP_INLINE_VISIBILITY constexpr
-enable_if_t<is_pointer_v<_TPtr>, _TPtr>
+enable_if_t<is_pointer_v<_TPtr>
+ && is_object_v<remove_pointer_t<_TPtr>>
+ && ! is_void_v<remove_pointer_t<_TPtr>>
+ && (sizeof(remove_pointer_t<_TPtr>) > 0), _TPtr>
midpoint(_TPtr __a, _TPtr __b) noexcept
{
return __a + _VSTD::midpoint(ptrdiff_t(0), __b - __a);
diff --git a/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp b/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp
index 2352ce7..c757610 100644
--- a/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp
+++ b/test/std/numerics/numeric.ops/numeric.ops.midpoint/midpoint.fail.cpp
@@ -18,13 +18,22 @@
#include "test_macros.h"
+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'}}
+ (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((void *)0, (void *)0); // expected-error@numeric:* {{arithmetic on pointers to void}}
+ (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;
}