Add additional constraints on midpoint(pointer, pointer). Fixes PR#42037.

llvm-svn: 361970
This commit is contained in:
Marshall Clow 2019-05-29 15:17:55 +00:00
parent c450874cb8
commit 6b03a1b423
2 changed files with 17 additions and 5 deletions

View File

@ -527,7 +527,7 @@ lcm(_Tp __m, _Up __n)
#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 @@ _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
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);

View File

@ -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;
}