Skip to content

Commit 9138666

Browse files
committed
Revert "[libc++] Remove extension to support allocator<const T>"
This reverts commit bed3240. I will need to add more tests for std::shared_ptr<T const> before re-landing this.
1 parent 5160447 commit 9138666

File tree

16 files changed

+166
-23
lines changed

16 files changed

+166
-23
lines changed

libcxx/docs/ReleaseNotes.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,6 @@ API Changes
6969
- The C++14 function ``std::quoted(const char*)`` is no longer supported in
7070
C++03 or C++11 modes.
7171

72-
- libc++ no longer supports containers of ``const``-qualified element type,
73-
such as ``vector<const T>`` and ``list<const T>``. This used to be supported
74-
as an extension. Likewise, ``std::allocator<const T>`` is no longer supported.
75-
If you were using ``vector<const T>``, replace it with ``vector<T>`` instead.
76-
7772
ABI Changes
7873
-----------
7974

libcxx/include/__memory/allocator.h

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ class _LIBCPP_TEMPLATE_VIS allocator<void>
3737

3838
template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
3939
};
40+
41+
template <>
42+
class _LIBCPP_TEMPLATE_VIS allocator<const void>
43+
{
44+
public:
45+
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* pointer;
46+
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void* const_pointer;
47+
_LIBCPP_DEPRECATED_IN_CXX17 typedef const void value_type;
48+
49+
template <class _Up> struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {typedef allocator<_Up> other;};
50+
};
4051
#endif
4152

4253
// This class provides a non-trivial default constructor to the class that derives from it
@@ -69,7 +80,6 @@ template <class _Tp>
6980
class _LIBCPP_TEMPLATE_VIS allocator
7081
: private __non_trivial_if<!is_void<_Tp>::value, allocator<_Tp> >
7182
{
72-
static_assert(!is_const<_Tp>::value, "std::allocator does not support const types");
7383
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
7484
public:
7585
typedef size_t size_type;
@@ -148,6 +158,84 @@ class _LIBCPP_TEMPLATE_VIS allocator
148158
#endif
149159
};
150160

161+
template <class _Tp>
162+
class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
163+
: private __non_trivial_if<!is_void<_Tp>::value, allocator<const _Tp> >
164+
{
165+
static_assert(!is_volatile<_Tp>::value, "std::allocator does not support volatile types");
166+
public:
167+
typedef size_t size_type;
168+
typedef ptrdiff_t difference_type;
169+
typedef const _Tp value_type;
170+
typedef true_type propagate_on_container_move_assignment;
171+
typedef true_type is_always_equal;
172+
173+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
174+
allocator() _NOEXCEPT = default;
175+
176+
template <class _Up>
177+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
178+
allocator(const allocator<_Up>&) _NOEXCEPT { }
179+
180+
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
181+
const _Tp* allocate(size_t __n) {
182+
if (__n > allocator_traits<allocator>::max_size(*this))
183+
__throw_bad_array_new_length();
184+
if (__libcpp_is_constant_evaluated()) {
185+
return static_cast<const _Tp*>(::operator new(__n * sizeof(_Tp)));
186+
} else {
187+
return static_cast<const _Tp*>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
188+
}
189+
}
190+
191+
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
192+
void deallocate(const _Tp* __p, size_t __n) {
193+
if (__libcpp_is_constant_evaluated()) {
194+
::operator delete(const_cast<_Tp*>(__p));
195+
} else {
196+
_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
197+
}
198+
}
199+
200+
// C++20 Removed members
201+
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
202+
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
203+
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
204+
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;
205+
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& const_reference;
206+
207+
template <class _Up>
208+
struct _LIBCPP_DEPRECATED_IN_CXX17 rebind {
209+
typedef allocator<_Up> other;
210+
};
211+
212+
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
213+
const_pointer address(const_reference __x) const _NOEXCEPT {
214+
return _VSTD::addressof(__x);
215+
}
216+
217+
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_DEPRECATED_IN_CXX17
218+
const _Tp* allocate(size_t __n, const void*) {
219+
return allocate(__n);
220+
}
221+
222+
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT {
223+
return size_type(~0) / sizeof(_Tp);
224+
}
225+
226+
template <class _Up, class... _Args>
227+
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
228+
void construct(_Up* __p, _Args&&... __args) {
229+
::new ((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
230+
}
231+
232+
_LIBCPP_DEPRECATED_IN_CXX17 _LIBCPP_INLINE_VISIBILITY
233+
void destroy(pointer __p) {
234+
__p->~_Tp();
235+
}
236+
#endif
237+
};
238+
151239
template <class _Tp, class _Up>
152240
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
153241
bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}

libcxx/include/__memory/shared_ptr.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ struct __shared_ptr_emplace
288288
: __storage_(_VSTD::move(__a))
289289
{
290290
#if _LIBCPP_STD_VER > 17
291-
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, remove_cv_t<_Tp>>::type;
291+
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
292292
_TpAlloc __tmp(*__get_alloc());
293293
allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
294294
#else
@@ -305,7 +305,7 @@ struct __shared_ptr_emplace
305305
private:
306306
virtual void __on_zero_shared() _NOEXCEPT {
307307
#if _LIBCPP_STD_VER > 17
308-
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, remove_cv_t<_Tp>>::type;
308+
using _TpAlloc = typename __allocator_traits_rebind<_Alloc, _Tp>::type;
309309
_TpAlloc __tmp(*__get_alloc());
310310
allocator_traits<_TpAlloc>::destroy(__tmp, __get_elem());
311311
#else
@@ -960,7 +960,7 @@ template<class _Tp, class ..._Args, class = __enable_if_t<!is_array<_Tp>::value>
960960
_LIBCPP_HIDE_FROM_ABI
961961
shared_ptr<_Tp> make_shared(_Args&& ...__args)
962962
{
963-
return _VSTD::allocate_shared<_Tp>(allocator<typename remove_cv<_Tp>::type>(), _VSTD::forward<_Args>(__args)...);
963+
return _VSTD::allocate_shared<_Tp>(allocator<_Tp>(), _VSTD::forward<_Args>(__args)...);
964964
}
965965

966966
template<class _Tp, class _Up>

libcxx/include/memory

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,17 +895,19 @@ void __construct_range_forward(_Alloc& __a, _Iter __begin1, _Iter __end1, _Ptr&
895895
}
896896

897897
template <class _Alloc, class _Source, class _Dest,
898+
class _RawSource = typename remove_const<_Source>::type,
899+
class _RawDest = typename remove_const<_Dest>::type,
898900
class =
899901
typename enable_if<
900902
is_trivially_copy_constructible<_Dest>::value &&
901-
is_same<typename remove_const<_Source>::type, _Dest>::value &&
903+
is_same<_RawSource, _RawDest>::value &&
902904
(__is_default_allocator<_Alloc>::value || !__has_construct<_Alloc, _Dest*, _Source&>::value)
903905
>::type>
904906
_LIBCPP_INLINE_VISIBILITY
905907
void __construct_range_forward(_Alloc&, _Source* __begin1, _Source* __end1, _Dest*& __begin2) {
906908
ptrdiff_t _Np = __end1 - __begin1;
907909
if (_Np > 0) {
908-
_VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Dest));
910+
_VSTD::memcpy(const_cast<_RawDest*>(__begin2), __begin1, _Np * sizeof(_Dest));
909911
__begin2 += _Np;
910912
}
911913
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03
10+
11+
// <vector>
12+
13+
// vector<const int> v; // an extension
14+
15+
#include <vector>
16+
#include <type_traits>
17+
18+
#include "test_macros.h"
19+
20+
int main(int, char**)
21+
{
22+
std::vector<const int> v = {1, 2, 3};
23+
24+
return 0;
25+
}

libcxx/test/libcxx/memory/allocator_void.trivial.compile.pass.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@
1717
#include <type_traits>
1818

1919
typedef std::allocator<void> A1;
20-
struct A2 : std::allocator<void> { };
20+
typedef std::allocator<void const> A2;
21+
struct A3 : std::allocator<void> { };
22+
struct A4 : std::allocator<void const> { };
2123

2224
static_assert(std::is_trivially_default_constructible<A1>::value, "");
2325
static_assert(std::is_trivial<A1>::value, "");
2426

2527
static_assert(std::is_trivially_default_constructible<A2>::value, "");
2628
static_assert(std::is_trivial<A2>::value, "");
29+
30+
static_assert(std::is_trivially_default_constructible<A3>::value, "");
31+
static_assert(std::is_trivial<A3>::value, "");
32+
33+
static_assert(std::is_trivially_default_constructible<A4>::value, "");
34+
static_assert(std::is_trivial<A4>::value, "");

libcxx/test/libcxx/memory/allocator.cv.verify.cpp renamed to libcxx/test/libcxx/memory/allocator_volatile.verify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// http://wg21.link/LWG2447 gives implementors freedom to reject const or volatile types in `std::allocator`.
9+
// http://wg21.link/LWG2447 gives implementors freedom to reject volatile types in `std::allocator`.
1010

1111
#include <memory>
1212

1313
std::allocator<volatile int> A1; // expected-error@*:* {{std::allocator does not support volatile types}}
14-
std::allocator<const int> A2; // expected-error@*:* {{std::allocator does not support const types}}
14+
std::allocator<const volatile int> A2; // expected-error@*:* {{std::allocator does not support volatile types}}

libcxx/test/std/concepts/concepts.lang/concept.default.init/default_initializable.compile.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ void test()
202202
test_not_const<std::array< volatile int, 1>>();
203203
test_false <std::array<const volatile int, 1>>();
204204
test_true <std::deque< int>>();
205+
#ifdef _LIBCPP_VERSION
206+
test_true <std::deque<const int>>();
207+
#endif // _LIBCPP_VERSION
205208
test_true <std::forward_list<int>>();
206209
test_true <std::list<int>>();
207210
test_true <std::vector<int>>();
@@ -220,6 +223,9 @@ void test()
220223

221224
// Container adaptors
222225
test_true <std::stack< int>>();
226+
#ifdef _LIBCPP_VERSION
227+
test_true <std::stack<const int>>();
228+
#endif // _LIBCPP_VERSION
223229
test_true <std::queue<int>>();
224230
test_true <std::priority_queue<int>>();
225231

libcxx/test/std/utilities/memory/default.allocator/allocator.ctor.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ TEST_CONSTEXPR_CXX20 bool test() {
3737

3838
int main(int, char**) {
3939
test<char>();
40-
test<int>();
40+
test<char const>();
4141
test<void>();
4242

4343
#if TEST_STD_VER > 17
4444
static_assert(test<char>());
45-
static_assert(test<int>());
45+
static_assert(test<char const>());
4646
static_assert(test<void>());
4747
#endif
4848
return 0;

libcxx/test/std/utilities/memory/default.allocator/allocator.dtor.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ int main(int, char**)
2626
{
2727
test<int>();
2828
test<void>();
29+
#ifdef _LIBCPP_VERSION // extension
30+
test<int const>();
31+
#endif // _LIBCPP_VERSION
2932

3033
static_assert(test<int>());
3134
static_assert(test<void>());
35+
#ifdef _LIBCPP_VERSION // extension
36+
static_assert(test<int const>());
37+
#endif // _LIBCPP_VERSION
3238

3339
return 0;
3440
}

libcxx/test/std/utilities/memory/default.allocator/allocator.members/allocate.constexpr.size.verify.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ constexpr bool test()
3535
int main(int, char**)
3636
{
3737
static_assert(test<double>()); // expected-error {{static_assert expression is not an integral constant expression}}
38+
LIBCPP_STATIC_ASSERT(test<const double>()); // expected-error {{static_assert expression is not an integral constant expression}}
3839
return 0;
3940
}

libcxx/test/std/utilities/memory/default.allocator/allocator.members/allocate.size.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void test()
4444
int main(int, char**)
4545
{
4646
test<double>();
47+
LIBCPP_ONLY(test<const double>());
4748

4849
return 0;
4950
}

libcxx/test/std/utilities/memory/default.allocator/allocator_types.deprecated_in_cxx17.verify.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ int main(int, char**) {
3535
typedef std::allocator<char>::const_reference ConstReference; // expected-warning {{'const_reference' is deprecated}}
3636
typedef std::allocator<char>::rebind<int>::other Rebind; // expected-warning {{'rebind<int>' is deprecated}}
3737
}
38+
{
39+
typedef std::allocator<char const>::pointer Pointer; // expected-warning {{'pointer' is deprecated}}
40+
typedef std::allocator<char const>::const_pointer ConstPointer; // expected-warning {{'const_pointer' is deprecated}}
41+
typedef std::allocator<char const>::reference Reference; // expected-warning {{'reference' is deprecated}}
42+
typedef std::allocator<char const>::const_reference ConstReference; // expected-warning {{'const_reference' is deprecated}}
43+
typedef std::allocator<char const>::rebind<int>::other Rebind; // expected-warning {{'rebind<int>' is deprecated}}
44+
}
3845
{
3946
typedef std::allocator<void>::pointer Pointer; // expected-warning {{'pointer' is deprecated}}
4047
typedef std::allocator<void>::const_pointer ConstPointer; // expected-warning {{'const_pointer' is deprecated}}

libcxx/test/std/utilities/memory/default.allocator/allocator_types.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,8 @@ void test() {
5959

6060
int main(int, char**) {
6161
test<char>();
62+
#ifdef _LIBCPP_VERSION
63+
test<char const>(); // extension
64+
#endif
6265
return 0;
6366
}

libcxx/test/std/utilities/memory/default.allocator/allocator_types.removed_in_cxx20.verify.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@
3131
template <typename T>
3232
void check()
3333
{
34-
typedef typename std::allocator<T>::pointer AP; // expected-error 2 {{no type named 'pointer'}}
35-
typedef typename std::allocator<T>::const_pointer ACP; // expected-error 2 {{no type named 'const_pointer'}}
36-
typedef typename std::allocator<T>::reference AR; // expected-error 2 {{no type named 'reference'}}
37-
typedef typename std::allocator<T>::const_reference ACR; // expected-error 2 {{no type named 'const_reference'}}
38-
typedef typename std::allocator<T>::template rebind<int>::other ARO; // expected-error 2 {{no member named 'rebind'}}
34+
typedef typename std::allocator<T>::pointer AP; // expected-error 3 {{no type named 'pointer'}}
35+
typedef typename std::allocator<T>::const_pointer ACP; // expected-error 3 {{no type named 'const_pointer'}}
36+
typedef typename std::allocator<T>::reference AR; // expected-error 3 {{no type named 'reference'}}
37+
typedef typename std::allocator<T>::const_reference ACR; // expected-error 3 {{no type named 'const_reference'}}
38+
typedef typename std::allocator<T>::template rebind<int>::other ARO; // expected-error 3 {{no member named 'rebind'}}
3939
}
4040

4141
int main(int, char**)
4242
{
4343
check<char>();
44+
check<char const>();
4445
check<void>();
4546
return 0;
4647
}

libcxx/test/std/utilities/memory/specialized.algorithms/specialized.construct/construct_at.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ constexpr bool test()
8282
}
8383

8484
{
85-
std::allocator<Counted> a;
85+
std::allocator<Counted const> a;
8686
Counted const* p = a.allocate(2);
8787
int count = 0;
8888
std::construct_at(p, count);
@@ -93,7 +93,7 @@ constexpr bool test()
9393
assert(count == 1);
9494
p->~Counted();
9595
assert(count == 0);
96-
a.deallocate(const_cast<Counted*>(p), 2);
96+
a.deallocate(p, 2);
9797
}
9898

9999
return true;

0 commit comments

Comments
 (0)