Skip to content

Commit 49be23a

Browse files
committed
[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
1 parent 7bbd7e9 commit 49be23a

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

libcxx/include/vector

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2963,7 +2963,7 @@ vector<bool, _Allocator>::assign(size_type __n, const value_type& __x)
29632963
__size_ = __n;
29642964
else
29652965
{
2966-
vector __v(__alloc());
2966+
vector __v(get_allocator());
29672967
__v.reserve(__recommend(__n));
29682968
__v.__size_ = __n;
29692969
swap(__v);
@@ -3018,7 +3018,7 @@ vector<bool, _Allocator>::reserve(size_type __n)
30183018
{
30193019
if (__n > capacity())
30203020
{
3021-
vector __v(this->__alloc());
3021+
vector __v(this->get_allocator());
30223022
__v.__vallocate(__n);
30233023
__v.__construct_at_end(this->begin(), this->end());
30243024
swap(__v);
@@ -3088,7 +3088,7 @@ vector<bool, _Allocator>::insert(const_iterator __position, const value_type& __
30883088
}
30893089
else
30903090
{
3091-
vector __v(__alloc());
3091+
vector __v(get_allocator());
30923092
__v.reserve(__recommend(__size_ + 1));
30933093
__v.__size_ = __size_ + 1;
30943094
__r = _VSTD::copy(cbegin(), __position, __v.begin());
@@ -3114,7 +3114,7 @@ vector<bool, _Allocator>::insert(const_iterator __position, size_type __n, const
31143114
}
31153115
else
31163116
{
3117-
vector __v(__alloc());
3117+
vector __v(get_allocator());
31183118
__v.reserve(__recommend(__size_ + __n));
31193119
__v.__size_ = __size_ + __n;
31203120
__r = _VSTD::copy(cbegin(), __position, __v.begin());
@@ -3143,7 +3143,7 @@ vector<bool, _Allocator>::insert(const_iterator __position, _InputIterator __fir
31433143
++this->__size_;
31443144
back() = *__first;
31453145
}
3146-
vector __v(__alloc());
3146+
vector __v(get_allocator());
31473147
if (__first != __last)
31483148
{
31493149
#ifndef _LIBCPP_NO_EXCEPTIONS
@@ -3193,7 +3193,7 @@ vector<bool, _Allocator>::insert(const_iterator __position, _ForwardIterator __f
31933193
}
31943194
else
31953195
{
3196-
vector __v(__alloc());
3196+
vector __v(get_allocator());
31973197
__v.reserve(__recommend(__size_ + __n));
31983198
__v.__size_ = __size_ + __n;
31993199
__r = _VSTD::copy(cbegin(), __position, __v.begin());
@@ -3260,7 +3260,7 @@ vector<bool, _Allocator>::resize(size_type __sz, value_type __x)
32603260
}
32613261
else
32623262
{
3263-
vector __v(__alloc());
3263+
vector __v(get_allocator());
32643264
__v.reserve(__recommend(__size_ + __n));
32653265
__v.__size_ = __size_ + __n;
32663266
__r = _VSTD::copy(cbegin(), cend(), __v.begin());

libcxx/test/std/containers/sequences/vector.bool/insert_iter_iter_iter.pass.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,22 @@ int main(int, char**)
9292
assert(v[j] == 0);
9393
}
9494
#if TEST_STD_VER >= 11
95+
{
96+
std::vector<bool, explicit_allocator<bool>> v(100);
97+
bool a[] = {1, 0, 0, 1, 1};
98+
const unsigned N = sizeof(a)/sizeof(a[0]);
99+
std::vector<bool, explicit_allocator<bool>>::iterator i = v.insert(v.cbegin() + 10, cpp17_input_iterator<const bool*>(a),
100+
cpp17_input_iterator<const bool*>(a+N));
101+
assert(v.size() == 100 + N);
102+
assert(i == v.begin() + 10);
103+
std::size_t j;
104+
for (j = 0; j < 10; ++j)
105+
assert(v[j] == 0);
106+
for (std::size_t k = 0; k < N; ++j, ++k)
107+
assert(v[j] == a[k]);
108+
for (; j < v.size(); ++j)
109+
assert(v[j] == 0);
110+
}
95111
{
96112
std::vector<bool, min_allocator<bool>> v(100);
97113
bool a[] = {1, 0, 0, 1, 1};

libcxx/test/std/containers/sequences/vector.bool/insert_iter_size_value.pass.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ int main(int, char**)
6565
assert(v[j] == 0);
6666
}
6767
#if TEST_STD_VER >= 11
68+
{
69+
std::vector<bool, explicit_allocator<bool>> v(10);
70+
std::vector<bool, explicit_allocator<bool>>::iterator i
71+
= v.insert(v.cbegin() + 10, 5, 1);
72+
assert(v.size() == 15);
73+
assert(i == v.begin() + 10);
74+
}
6875
{
6976
std::vector<bool, min_allocator<bool>> v(100);
7077
std::vector<bool, min_allocator<bool>>::iterator i = v.insert(v.cbegin() + 10, 5, 1);

libcxx/test/std/containers/sequences/vector.bool/insert_iter_value.pass.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ int main(int, char**)
6262
assert(v[j] == 0);
6363
}
6464
#if TEST_STD_VER >= 11
65+
{
66+
std::vector<bool, explicit_allocator<bool>> v(10);
67+
std::vector<bool, explicit_allocator<bool>>::iterator i
68+
= v.insert(v.cbegin() + 10, 1);
69+
assert(v.size() == 11);
70+
assert(i == v.begin() + 10);
71+
assert(*i == 1);
72+
}
6573
{
6674
std::vector<bool, min_allocator<bool>> v(100);
6775
std::vector<bool, min_allocator<bool>>::iterator i = v.insert(v.cbegin() + 10, 1);

libcxx/test/std/containers/sequences/vector.bool/reserve.pass.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ int main(int, char**)
4040
v.reserve(10);
4141
assert(v.capacity() >= 10);
4242
}
43+
{
44+
std::vector<bool, explicit_allocator<bool>> v;
45+
v.reserve(10);
46+
assert(v.capacity() >= 10);
47+
}
4348
{
4449
std::vector<bool, min_allocator<bool>> v(100);
4550
assert(v.capacity() >= 100);

libcxx/test/std/containers/sequences/vector.bool/resize_size.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ int main(int, char**)
3333
assert(v.capacity() >= 400);
3434
}
3535
#if TEST_STD_VER >= 11
36+
{
37+
std::vector<bool, explicit_allocator<bool>> v;
38+
v.resize(10);
39+
assert(v.size() == 10);
40+
assert(v.capacity() >= 10);
41+
}
3642
{
3743
std::vector<bool, min_allocator<bool>> v(100);
3844
v.resize(50);

0 commit comments

Comments
 (0)