Skip to content

Commit 316d336

Browse files
committed
[libc++] Un-deprecate and un-remove some members of std::allocator
This implements the part of P0619R4 related to the default allocator. This is incredibly important, since otherwise there is an ABI break between C++17 and C++20 w.r.t. the default allocator's size_type on platforms where std::size_t is not the same as std::make_unsigned<std::ptrdiff_t>.
1 parent 4ca6091 commit 316d336

File tree

9 files changed

+94
-55
lines changed

9 files changed

+94
-55
lines changed

libcxx/include/memory

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ template <class T>
115115
class allocator
116116
{
117117
public:
118-
typedef size_t size_type; // deprecated in C++17, removed in C++20
119-
typedef ptrdiff_t difference_type; // deprecated in C++17, removed in C++20
118+
typedef size_t size_type;
119+
typedef ptrdiff_t difference_type;
120120
typedef T* pointer; // deprecated in C++17, removed in C++20
121121
typedef const T* const_pointer; // deprecated in C++17, removed in C++20
122122
typedef typename add_lvalue_reference<T>::type
@@ -1748,9 +1748,9 @@ template <class _Tp>
17481748
class _LIBCPP_TEMPLATE_VIS allocator
17491749
{
17501750
public:
1751+
typedef size_t size_type;
1752+
typedef ptrdiff_t difference_type;
17511753
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1752-
_LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1753-
_LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
17541754
_LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp* pointer;
17551755
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
17561756
_LIBCPP_DEPRECATED_IN_CXX17 typedef _Tp& reference;
@@ -1816,9 +1816,9 @@ template <class _Tp>
18161816
class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
18171817
{
18181818
public:
1819+
typedef size_t size_type;
1820+
typedef ptrdiff_t difference_type;
18191821
#if _LIBCPP_STD_VER <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS)
1820-
_LIBCPP_DEPRECATED_IN_CXX17 typedef size_t size_type;
1821-
_LIBCPP_DEPRECATED_IN_CXX17 typedef ptrdiff_t difference_type;
18221822
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* pointer;
18231823
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp* const_pointer;
18241824
_LIBCPP_DEPRECATED_IN_CXX17 typedef const _Tp& reference;

libcxx/test/libcxx/depr/depr.default.allocator/allocator_types.cxx2a.pass.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
// <memory>
1010

11-
// check nested types:
11+
// Check that the following types are provided regardless of the Standard when
12+
// we request them from libc++.
1213

1314
// template <class T>
1415
// class allocator
@@ -45,5 +46,5 @@ int main(int, char**)
4546
static_assert((std::is_same<std::allocator<char>::rebind<int>::other,
4647
std::allocator<int> >::value), "");
4748

48-
return 0;
49+
return 0;
4950
}

libcxx/test/libcxx/depr/depr.default.allocator/allocator_void.cxx2a.pass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
// <memory>
1010

11+
// Check that the following member types of allocator<void> are provided
12+
// regardless of the Standard when we request them from libc++.
13+
1114
// template <>
1215
// class allocator<void>
1316
// {

libcxx/test/libcxx/depr/depr.default.allocator/allocator_types.depr_in_cxx17.verify.cpp renamed to libcxx/test/std/utilities/memory/default.allocator/allocator_types.deprecated_in_cxx17.verify.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,42 @@
88

99
// <memory>
1010

11-
// check nested types:
11+
// Check that the following nested types are deprecated in C++17:
1212

1313
// template <class T>
1414
// class allocator
1515
// {
1616
// public:
17-
// typedef size_t size_type;
18-
// typedef ptrdiff_t difference_type;
19-
// typedef T* pointer;
20-
// typedef const T* const_pointer;
17+
// typedef T* pointer;
18+
// typedef const T* const_pointer;
2119
// typedef typename add_lvalue_reference<T>::type reference;
2220
// typedef typename add_lvalue_reference<const T>::type const_reference;
2321
//
2422
// template <class U> struct rebind {typedef allocator<U> other;};
2523
// ...
2624
// };
2725

28-
// Deprecated in C++17
29-
30-
// UNSUPPORTED: c++03, c++11, c++14
26+
// REQUIRES: c++17
3127

3228
// Clang 6 does not handle the deprecated attribute on template members properly,
3329
// so the rebind<int> check below fails.
3430
// UNSUPPORTED: clang-6
3531

36-
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
37-
3832
#include <memory>
3933
#include "test_macros.h"
4034

4135
int main(int, char**)
4236
{
43-
typedef std::allocator<char>::size_type AST; // expected-warning {{'size_type' is deprecated}}
44-
typedef std::allocator<char>::difference_type ADT; // expected-warning {{'difference_type' is deprecated}}
4537
typedef std::allocator<char>::pointer AP; // expected-warning {{'pointer' is deprecated}}
4638
typedef std::allocator<char>::const_pointer ACP; // expected-warning {{'const_pointer' is deprecated}}
4739
typedef std::allocator<char>::reference AR; // expected-warning {{'reference' is deprecated}}
4840
typedef std::allocator<char>::const_reference ACR; // expected-warning {{'const_reference' is deprecated}}
4941
typedef std::allocator<char>::rebind<int>::other ARO; // expected-warning {{'rebind<int>' is deprecated}}
5042

51-
return 0;
43+
typedef std::allocator<char const>::pointer AP2; // expected-warning {{'pointer' is deprecated}}
44+
typedef std::allocator<char const>::const_pointer ACP2; // expected-warning {{'const_pointer' is deprecated}}
45+
typedef std::allocator<char const>::reference AR2; // expected-warning {{'reference' is deprecated}}
46+
typedef std::allocator<char const>::const_reference ACR2; // expected-warning {{'const_reference' is deprecated}}
47+
typedef std::allocator<char const>::rebind<int>::other ARO2; // expected-warning {{'rebind<int>' is deprecated}}
48+
return 0;
5249
}

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
// <memory>
1010

11-
// check nested types:
11+
// Check that the nested types of std::allocator are provided:
1212

1313
// template <class T>
1414
// class allocator
1515
// {
1616
// public:
17+
// typedef size_t size_type;
18+
// typedef ptrdiff_t difference_type;
1719
// typedef T value_type;
1820
//
1921
// typedef true_type propagate_on_container_move_assignment;
@@ -27,21 +29,25 @@
2729

2830
#include "test_macros.h"
2931

30-
int main(int, char**)
32+
template <typename T, typename U>
33+
void check()
3134
{
32-
static_assert((std::is_same<std::allocator<char>::value_type, char>::value), "");
33-
34-
static_assert((std::is_same<std::allocator<char>::propagate_on_container_move_assignment, std::true_type>::value), "");
35-
LIBCPP_STATIC_ASSERT((std::is_same<std::allocator<const char>::propagate_on_container_move_assignment, std::true_type>::value), "");
36-
37-
static_assert((std::is_same<std::allocator<char>::is_always_equal, std::true_type>::value), "");
38-
LIBCPP_STATIC_ASSERT((std::is_same<std::allocator<const char>::is_always_equal, std::true_type>::value), "");
39-
40-
std::allocator<char> a;
41-
std::allocator<char> a2 = a;
35+
static_assert((std::is_same<typename std::allocator<T>::size_type, std::size_t>::value), "");
36+
static_assert((std::is_same<typename std::allocator<T>::difference_type, std::ptrdiff_t>::value), "");
37+
static_assert((std::is_same<typename std::allocator<T>::value_type, T>::value), "");
38+
static_assert((std::is_same<typename std::allocator<T>::propagate_on_container_move_assignment, std::true_type>::value), "");
39+
static_assert((std::is_same<typename std::allocator<T>::is_always_equal, std::true_type>::value), "");
40+
41+
std::allocator<T> a;
42+
std::allocator<T> a2 = a;
4243
a2 = a;
43-
std::allocator<int> a3 = a2;
44-
((void)a3);
44+
std::allocator<U> a3 = a2;
45+
(void)a3;
46+
}
4547

46-
return 0;
48+
int main(int, char**)
49+
{
50+
check<char, int>();
51+
check<char const, int const>();
52+
return 0;
4753
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// <memory>
10+
11+
// Check that the following nested types are removed in C++20:
12+
13+
// template <class T>
14+
// class allocator
15+
// {
16+
// public:
17+
// typedef T* pointer;
18+
// typedef const T* const_pointer;
19+
// typedef typename add_lvalue_reference<T>::type reference;
20+
// typedef typename add_lvalue_reference<const T>::type const_reference;
21+
//
22+
// template <class U> struct rebind {typedef allocator<U> other;};
23+
// ...
24+
// };
25+
26+
// UNSUPPORTED: c++03, c++11, c++14, c++17
27+
28+
#include <memory>
29+
#include "test_macros.h"
30+
31+
template <typename T>
32+
void check()
33+
{
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'}}
39+
}
40+
41+
int main(int, char**)
42+
{
43+
check<char>();
44+
check<char const>();
45+
return 0;
46+
}

libcxx/test/libcxx/depr/depr.default.allocator/allocator_void.depr_in_cxx17.verify.cpp renamed to libcxx/test/std/utilities/memory/default.allocator/allocator_void.deprecated_in_cxx17.verify.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
// <memory>
10-
//
11-
// template <>
12-
// class allocator<void>
13-
// {
14-
// public:
15-
// typedef void* pointer;
16-
// typedef const void* const_pointer;
17-
// typedef void value_type;
18-
//
19-
// template <class _Up> struct rebind {typedef allocator<_Up> other;};
20-
// };
21-
//
22-
// Deprecated in C++17
2310

24-
// UNSUPPORTED: c++03, c++11, c++14
11+
// Check that allocator<void> is deprecated in C++17.
2512

26-
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ENABLE_CXX20_REMOVED_ALLOCATOR_MEMBERS
13+
// REQUIRES: c++17
2714

2815
#include <memory>
2916
#include "test_macros.h"
@@ -33,6 +20,5 @@ int main(int, char**)
3320
typedef std::allocator<void>::pointer AP; // expected-warning {{'allocator<void>' is deprecated}}
3421
typedef std::allocator<void>::const_pointer ACP; // expected-warning {{'allocator<void>' is deprecated}}
3522
typedef std::allocator<void>::rebind<int>::other ARO; // expected-warning {{'allocator<void>' is deprecated}}
36-
37-
return 0;
23+
return 0;
3824
}

libcxx/www/cxx1z_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ <h3>Paper Status</h3>
105105
<tr><td><a href="https://wg21.link/p0088r3">p0088r3</a></td><td>LWG</td><td>Variant: a type-safe union for C++17</td><td>Oulu</td><td>Complete</td><td>4.0</td></tr>
106106
<tr><td><a href="https://wg21.link/p0137r1">p0137r1</a></td><td>CWG</td><td>Core Issue 1776: Replacement of class objects containing reference members</td><td>Oulu</td><td>Complete</td><td>6.0</td></tr>
107107
<tr><td><a href="https://wg21.link/p0163r0">p0163r0</a></td><td>LWG</td><td>shared_ptr::weak_type</td><td>Oulu</td><td>Complete</td><td>3.9</td></tr>
108-
<tr><td><a href="https://wg21.link/p0174r2">p0174r2</a></td><td>LWG</td><td>Deprecating Vestigial Library Parts in C++17</td><td>Oulu</td><td></td><td></td></tr>
108+
<tr><td><a href="https://wg21.link/p0174r2">p0174r2</a></td><td>LWG</td><td>Deprecating Vestigial Library Parts in C++17</td><td>Oulu</td><td>Partial</td><td></td></tr>
109109
<tr><td><a href="https://wg21.link/p0175r1">p0175r1</a></td><td>LWG</td><td>Synopses for the C library</td><td>Oulu</td><td></td><td></td></tr>
110110
<tr><td><a href="https://wg21.link/p0180r2">p0180r2</a></td><td>LWG</td><td>Reserve a New Library Namespace for Future Standardization</td><td>Oulu</td><td><i>Nothing to do</i></td><td>n/a</td></tr>
111111
<tr><td><a href="https://wg21.link/p0181r1">p0181r1</a></td><td>LWG</td><td>Ordered by Default</td><td>Oulu</td><td><i>Removed in Kona</i></td><td>n/a</td></tr>

libcxx/www/cxx2a_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ <h3>Paper Status</h3>
8989
<tr><td><a href="https://wg21.link/P0528R3">P0528R3</a></td><td>CWG</td><td>The Curious Case of Padding Bits, Featuring Atomic Compare-and-Exchange</td><td>Rapperswil</td><td></td><td></td></tr>
9090
<tr><td><a href="https://wg21.link/P0542R5">P0542R5</a></td><td>CWG</td><td>Support for contract based programming in C++</td><td>Rapperswil</td><td></td><td></td></tr>
9191
<tr><td><a href="https://wg21.link/P0556R3">P0556R3</a></td><td>LWG</td><td>Integral power-of-2 operations</td><td>Rapperswil</td><td>Complete</td><td>9.0</td></tr>
92-
<tr><td><a href="https://wg21.link/P0619R4">P0619R4</a></td><td>LWG</td><td>Reviewing Deprecated Facilities of C++17 for C++20</td><td>Rapperswil</td><td></td><td></td></tr>
92+
<tr><td><a href="https://wg21.link/P0619R4">P0619R4</a></td><td>LWG</td><td>Reviewing Deprecated Facilities of C++17 for C++20</td><td>Rapperswil</td><td>Partial<br>(only std::allocator part is implemented)</td><td></td></tr>
9393
<tr><td><a href="https://wg21.link/P0646R1">P0646R1</a></td><td>LWG</td><td>Improving the Return Value of Erase-Like Algorithms</td><td>Rapperswil</td><td>Complete</td><td>10.0</td></tr>
9494
<tr><td><a href="https://wg21.link/P0722R3">P0722R3</a></td><td>CWG</td><td>Efficient sized delete for variable sized classes</td><td>Rapperswil</td><td>Complete</td><td>9.0</td></tr>
9595
<tr><td><a href="https://wg21.link/P0758R1">P0758R1</a></td><td>LWG</td><td>Implicit conversion traits and utility functions</td><td>Rapperswil</td><td>Complete</td><td></td></tr>

0 commit comments

Comments
 (0)