[libcxx-commits] [libcxx] [libc++] Use __is_pointer_in_range inside vector::insert (PR #80624)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Sun Feb 4 16:53:08 PST 2024


https://github.com/philnik777 created https://github.com/llvm/llvm-project/pull/80624

None

>From cca975f50f09d83915cffaad6c7ba52787fa8201 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 5 Feb 2024 01:52:14 +0100
Subject: [PATCH] [libc++] Use __is_pointer_in_range inside vector::insert

---
 libcxx/include/vector | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/vector b/libcxx/include/vector
index e9615ab4c9a30..e8d8e37ddb756 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -351,6 +351,7 @@ template<class T, class charT> requires is-vector-bool-reference<T> // Since C++
 #include <__type_traits/type_identity.h>
 #include <__utility/exception_guard.h>
 #include <__utility/forward.h>
+#include <__utility/is_pointer_in_range.h>
 #include <__utility/move.h>
 #include <__utility/pair.h>
 #include <__utility/swap.h>
@@ -1580,14 +1581,13 @@ template <class _Tp, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<_Tp, _Allocator>::iterator
 vector<_Tp, _Allocator>::insert(const_iterator __position, const_reference __x) {
   pointer __p = this->__begin_ + (__position - begin());
-  // We can't compare unrelated pointers inside constant expressions
-  if (!__libcpp_is_constant_evaluated() && this->__end_ < this->__end_cap()) {
+  if (this->__end_ < this->__end_cap()) {
     if (__p == this->__end_) {
       __construct_one_at_end(__x);
     } else {
       __move_range(__p, this->__end_, __p + 1);
       const_pointer __xr = pointer_traits<const_pointer>::pointer_to(__x);
-      if (__p <= __xr && __xr < this->__end_)
+      if (std::__is_pointer_in_range(std::addressof(__x), std::__to_address(__p), std::__to_address(__end_)))
         ++__xr;
       *__p = *__xr;
     }



More information about the libcxx-commits mailing list