[libcxx-commits] [libcxx] 49be23a - [libcxx] Support allocators with explicit c-tors in vector<bool>

Mikhail Maltsev via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 21 02:39:21 PDT 2021


Author: Mikhail Maltsev
Date: 2021-10-21T10:38:56+01:00
New Revision: 49be23a1eb5743fa92a8367b6081674a8a392da3

URL: https://github.com/llvm/llvm-project/commit/49be23a1eb5743fa92a8367b6081674a8a392da3
DIFF: https://github.com/llvm/llvm-project/commit/49be23a1eb5743fa92a8367b6081674a8a392da3.diff

LOG: [libcxx] Support allocators with explicit c-tors in vector<bool>

std::vector<bool> rebinds the supplied allocator to construct objects
of type '__storage_type' rather than 'bool'. Allocators are allowed to
use explicit conversion constructors, so care must be taken when
performing conversions.

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D112150

Added: 
    

Modified: 
    libcxx/include/vector
    libcxx/test/std/containers/sequences/vector.bool/insert_iter_iter_iter.pass.cpp
    libcxx/test/std/containers/sequences/vector.bool/insert_iter_size_value.pass.cpp
    libcxx/test/std/containers/sequences/vector.bool/insert_iter_value.pass.cpp
    libcxx/test/std/containers/sequences/vector.bool/reserve.pass.cpp
    libcxx/test/std/containers/sequences/vector.bool/resize_size.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/vector b/libcxx/include/vector
index beec1b735185..eeca81dfabc1 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -2963,7 +2963,7 @@ vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
             __size_ = __n;
         else
         {
-            vector __v(__alloc());
+            vector __v(get_allocator());
             __v.reserve(__recommend(__n));
             __v.__size_ = __n;
             swap(__v);
@@ -3018,7 +3018,7 @@ vector<bool, _Allocator>::reserve(size_type __n)
 {
     if (__n > capacity())
     {
-        vector __v(this->__alloc());
+        vector __v(this->get_allocator());
         __v.__vallocate(__n);
         __v.__construct_at_end(this->begin(), this->end());
         swap(__v);
@@ -3088,7 +3088,7 @@ vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __
     }
     else
     {
-        vector __v(__alloc());
+        vector __v(get_allocator());
         __v.reserve(__recommend(__size_ + 1));
         __v.__size_ = __size_ + 1;
         __r = _VSTD::copy(cbegin(), __position, __v.begin());
@@ -3114,7 +3114,7 @@ vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const
     }
     else
     {
-        vector __v(__alloc());
+        vector __v(get_allocator());
         __v.reserve(__recommend(__size_ + __n));
         __v.__size_ = __size_ + __n;
         __r = _VSTD::copy(cbegin(), __position, __v.begin());
@@ -3143,7 +3143,7 @@ vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __fir
         ++this->__size_;
         back() = *__first;
     }
-    vector __v(__alloc());
+    vector __v(get_allocator());
     if (__first != __last)
     {
 #ifndef _LIBCPP_NO_EXCEPTIONS
@@ -3193,7 +3193,7 @@ vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __f
     }
     else
     {
-        vector __v(__alloc());
+        vector __v(get_allocator());
         __v.reserve(__recommend(__size_ + __n));
         __v.__size_ = __size_ + __n;
         __r = _VSTD::copy(cbegin(), __position, __v.begin());
@@ -3260,7 +3260,7 @@ vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
         }
         else
         {
-            vector __v(__alloc());
+            vector __v(get_allocator());
             __v.reserve(__recommend(__size_ + __n));
             __v.__size_ = __size_ + __n;
             __r = _VSTD::copy(cbegin(), cend(), __v.begin());

diff  --git a/libcxx/test/std/containers/sequences/vector.bool/insert_iter_iter_iter.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/insert_iter_iter_iter.pass.cpp
index 05c209a2f0bd..4ee9ea5d7f35 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/insert_iter_iter_iter.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/insert_iter_iter_iter.pass.cpp
@@ -92,6 +92,22 @@ int main(int, char**)
             assert(v[j] == 0);
     }
 #if TEST_STD_VER >= 11
+    {
+        std::vector<bool, explicit_allocator<bool>> v(100);
+        bool a[] = {1, 0, 0, 1, 1};
+        const unsigned N = sizeof(a)/sizeof(a[0]);
+        std::vector<bool, explicit_allocator<bool>>::iterator i = v.insert(v.cbegin() + 10, cpp17_input_iterator<const bool*>(a),
+                                        cpp17_input_iterator<const bool*>(a+N));
+        assert(v.size() == 100 + N);
+        assert(i == v.begin() + 10);
+        std::size_t j;
+        for (j = 0; j < 10; ++j)
+            assert(v[j] == 0);
+        for (std::size_t k = 0; k < N; ++j, ++k)
+            assert(v[j] == a[k]);
+        for (; j < v.size(); ++j)
+            assert(v[j] == 0);
+    }
     {
         std::vector<bool, min_allocator<bool>> v(100);
         bool a[] = {1, 0, 0, 1, 1};

diff  --git a/libcxx/test/std/containers/sequences/vector.bool/insert_iter_size_value.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/insert_iter_size_value.pass.cpp
index 7019b6d05d52..84cafc90cc77 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/insert_iter_size_value.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/insert_iter_size_value.pass.cpp
@@ -65,6 +65,13 @@ int main(int, char**)
             assert(v[j] == 0);
     }
 #if TEST_STD_VER >= 11
+    {
+        std::vector<bool, explicit_allocator<bool>> v(10);
+        std::vector<bool, explicit_allocator<bool>>::iterator i
+            = v.insert(v.cbegin() + 10, 5, 1);
+        assert(v.size() == 15);
+        assert(i == v.begin() + 10);
+    }
     {
         std::vector<bool, min_allocator<bool>> v(100);
         std::vector<bool, min_allocator<bool>>::iterator i = v.insert(v.cbegin() + 10, 5, 1);

diff  --git a/libcxx/test/std/containers/sequences/vector.bool/insert_iter_value.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/insert_iter_value.pass.cpp
index 593b41d8bef6..4cc48988fce7 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/insert_iter_value.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/insert_iter_value.pass.cpp
@@ -62,6 +62,14 @@ int main(int, char**)
             assert(v[j] == 0);
     }
 #if TEST_STD_VER >= 11
+    {
+        std::vector<bool, explicit_allocator<bool>> v(10);
+        std::vector<bool, explicit_allocator<bool>>::iterator i
+            = v.insert(v.cbegin() + 10, 1);
+        assert(v.size() == 11);
+        assert(i == v.begin() + 10);
+        assert(*i == 1);
+    }
     {
         std::vector<bool, min_allocator<bool>> v(100);
         std::vector<bool, min_allocator<bool>>::iterator i = v.insert(v.cbegin() + 10, 1);

diff  --git a/libcxx/test/std/containers/sequences/vector.bool/reserve.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/reserve.pass.cpp
index 23bd49d9730e..c36a779bdc14 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/reserve.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/reserve.pass.cpp
@@ -40,6 +40,11 @@ int main(int, char**)
         v.reserve(10);
         assert(v.capacity() >= 10);
     }
+    {
+        std::vector<bool, explicit_allocator<bool>> v;
+        v.reserve(10);
+        assert(v.capacity() >= 10);
+    }
     {
         std::vector<bool, min_allocator<bool>> v(100);
         assert(v.capacity() >= 100);

diff  --git a/libcxx/test/std/containers/sequences/vector.bool/resize_size.pass.cpp b/libcxx/test/std/containers/sequences/vector.bool/resize_size.pass.cpp
index c9d7f3dbb9ee..d032247a97a8 100644
--- a/libcxx/test/std/containers/sequences/vector.bool/resize_size.pass.cpp
+++ b/libcxx/test/std/containers/sequences/vector.bool/resize_size.pass.cpp
@@ -33,6 +33,12 @@ int main(int, char**)
         assert(v.capacity() >= 400);
     }
 #if TEST_STD_VER >= 11
+    {
+        std::vector<bool, explicit_allocator<bool>> v;
+        v.resize(10);
+        assert(v.size() == 10);
+        assert(v.capacity() >= 10);
+    }
     {
         std::vector<bool, min_allocator<bool>> v(100);
         v.resize(50);


        


More information about the libcxx-commits mailing list