Skip to content

Commit 9503536

Browse files
committed
[libc++] reuse allocator validitity check logic and message
1 parent 1539989 commit 9503536

File tree

10 files changed

+24
-46
lines changed

10 files changed

+24
-46
lines changed

libcxx/include/__memory/allocator_traits.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <__type_traits/enable_if.h>
1717
#include <__type_traits/is_constructible.h>
1818
#include <__type_traits/is_empty.h>
19+
#include <__type_traits/is_same.h>
1920
#include <__type_traits/make_unsigned.h>
2021
#include <__type_traits/remove_reference.h>
2122
#include <__type_traits/void_t.h>
@@ -372,6 +373,14 @@ template <class _Traits, class _Tp>
372373
using __rebind_alloc = typename _Traits::template rebind_alloc<_Tp>::other;
373374
#endif
374375

376+
template <class _Alloc>
377+
struct __check_valid_allocator : true_type {
378+
using _Traits = std::allocator_traits<_Alloc>;
379+
static_assert(is_same<_Alloc, __rebind_alloc<_Traits, typename _Traits::value_type> >::value,
380+
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
381+
"original allocator");
382+
};
383+
375384
// __is_default_allocator
376385
template <class _Tp>
377386
struct __is_default_allocator : false_type {};

libcxx/include/deque

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,11 +448,11 @@ public:
448448

449449
using value_type = _Tp;
450450

451-
static_assert((is_same<typename _Allocator::value_type, value_type>::value),
452-
"Allocator::value_type must be same type as value_type");
453-
454451
using allocator_type = _Allocator;
455452
using __alloc_traits = allocator_traits<allocator_type>;
453+
static_assert(__check_valid_allocator<allocator_type>::value, "");
454+
static_assert((is_same<typename allocator_type::value_type, value_type>::value),
455+
"Allocator::value_type must be same type as value_type");
456456

457457
using size_type = typename __alloc_traits::size_type;
458458
using difference_type = typename __alloc_traits::difference_type;
@@ -487,9 +487,6 @@ public:
487487
deque,
488488
void>;
489489

490-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
491-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
492-
"original allocator");
493490
static_assert(is_nothrow_default_constructible<allocator_type>::value ==
494491
is_nothrow_default_constructible<__pointer_allocator>::value,
495492
"rebinding an allocator should not change exception guarantees");

libcxx/include/forward_list

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,13 +650,11 @@ public:
650650
typedef _Tp value_type;
651651
typedef _Alloc allocator_type;
652652

653+
static_assert(__check_valid_allocator<allocator_type>::value, "");
654+
653655
static_assert(is_same<value_type, typename allocator_type::value_type>::value,
654656
"Allocator::value_type must be same type as value_type");
655657

656-
static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value,
657-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
658-
"original allocator");
659-
660658
static_assert((!is_same<allocator_type, __node_allocator>::value),
661659
"internal allocator type must differ from user-specified "
662660
"type; otherwise overload resolution breaks");

libcxx/include/list

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ class _LIBCPP_TEMPLATE_VIS list : private __list_imp<_Tp, _Alloc> {
674674
public:
675675
typedef _Tp value_type;
676676
typedef _Alloc allocator_type;
677+
static_assert(__check_valid_allocator<allocator_type>::value);
677678
static_assert((is_same<value_type, typename allocator_type::value_type>::value),
678679
"Allocator::value_type must be same type as value_type");
679680
typedef value_type& reference;
@@ -692,10 +693,6 @@ public:
692693
typedef void __remove_return_type;
693694
#endif
694695

695-
static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value,
696-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
697-
"original allocator");
698-
699696
_LIBCPP_HIDE_FROM_ABI list() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) {}
700697
_LIBCPP_HIDE_FROM_ABI explicit list(const allocator_type& __a) : base(__a) {}
701698
_LIBCPP_HIDE_FROM_ABI explicit list(size_type __n);

libcxx/include/map

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -999,9 +999,7 @@ private:
999999
typedef typename __base::__node_traits __node_traits;
10001000
typedef allocator_traits<allocator_type> __alloc_traits;
10011001

1002-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
1003-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
1004-
"original allocator");
1002+
static_assert(__check_valid_allocator<allocator_type>::value, "");
10051003

10061004
__base __tree_;
10071005

@@ -1659,6 +1657,7 @@ public:
16591657
typedef value_type& reference;
16601658
typedef const value_type& const_reference;
16611659

1660+
static_assert(__check_valid_allocator<allocator_type>::value, "");
16621661
static_assert((is_same<typename allocator_type::value_type, value_type>::value),
16631662
"Allocator::value_type must be same type as value_type");
16641663

@@ -1684,10 +1683,6 @@ private:
16841683
typedef typename __base::__node_traits __node_traits;
16851684
typedef allocator_traits<allocator_type> __alloc_traits;
16861685

1687-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
1688-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
1689-
"original allocator");
1690-
16911686
__base __tree_;
16921687

16931688
public:

libcxx/include/set

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,7 @@ private:
578578
typedef __tree<value_type, value_compare, allocator_type> __base;
579579
typedef allocator_traits<allocator_type> __alloc_traits;
580580

581-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
582-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
583-
"original allocator");
581+
static_assert(__check_valid_allocator<allocator_type>::value, "");
584582

585583
__base __tree_;
586584

@@ -1035,9 +1033,7 @@ private:
10351033
typedef __tree<value_type, value_compare, allocator_type> __base;
10361034
typedef allocator_traits<allocator_type> __alloc_traits;
10371035

1038-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
1039-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
1040-
"original allocator");
1036+
static_assert(__check_valid_allocator<allocator_type>::value, "");
10411037

10421038
__base __tree_;
10431039

libcxx/include/string

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,10 +781,7 @@ public:
781781
"traits_type::char_type must be the same type as CharT");
782782
static_assert((is_same<typename allocator_type::value_type, value_type>::value),
783783
"Allocator::value_type must be same type as value_type");
784-
785-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
786-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
787-
"original allocator");
784+
static_assert(__check_valid_allocator<allocator_type>::value, "");
788785

789786
// TODO: Implement iterator bounds checking without requiring the global database.
790787
typedef __wrap_iter<pointer> iterator;

libcxx/include/unordered_map

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,9 +1059,7 @@ private:
10591059
typedef unique_ptr<__node, _Dp> __node_holder;
10601060
typedef allocator_traits<allocator_type> __alloc_traits;
10611061

1062-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
1063-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
1064-
"original allocator");
1062+
static_assert(__check_valid_allocator<allocator_type>::value, "");
10651063

10661064
static_assert((is_same<typename __table::__container_value_type, value_type>::value), "");
10671065
static_assert((is_same<typename __table::__node_value_type, __value_type>::value), "");
@@ -1843,6 +1841,7 @@ public:
18431841
typedef pair<const key_type, mapped_type> value_type;
18441842
typedef value_type& reference;
18451843
typedef const value_type& const_reference;
1844+
static_assert(__check_valid_allocator<allocator_type>::value, "");
18461845
static_assert((is_same<value_type, typename allocator_type::value_type>::value),
18471846
"Allocator::value_type must be same type as value_type");
18481847

@@ -1866,10 +1865,6 @@ private:
18661865
static_assert((is_same<typename __node_traits::size_type, typename __alloc_traits::size_type>::value),
18671866
"Allocator uses different size_type for different types");
18681867

1869-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
1870-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
1871-
"original allocator");
1872-
18731868
public:
18741869
typedef typename __alloc_traits::pointer pointer;
18751870
typedef typename __alloc_traits::const_pointer const_pointer;

libcxx/include/unordered_set

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,10 @@ public:
588588
typedef __type_identity_t<_Alloc> allocator_type;
589589
typedef value_type& reference;
590590
typedef const value_type& const_reference;
591+
static_assert(__check_valid_allocator<allocator_type>::value, "");
591592
static_assert((is_same<value_type, typename allocator_type::value_type>::value),
592593
"Allocator::value_type must be same type as value_type");
593594

594-
static_assert(is_same<allocator_type, __rebind_alloc<allocator_traits<allocator_type>, value_type> >::value,
595-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
596-
"original allocator");
597-
598595
private:
599596
typedef __hash_table<value_type, hasher, key_equal, allocator_type> __table;
600597

libcxx/include/vector

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,10 @@ public:
415415
vector,
416416
void>;
417417

418+
static_assert(__check_valid_allocator<allocator_type>::value, "");
418419
static_assert((is_same<typename allocator_type::value_type, value_type>::value),
419420
"Allocator::value_type must be same type as value_type");
420421

421-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
422-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
423-
"original allocator");
424-
425422
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector()
426423
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) {}
427424
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI explicit vector(const allocator_type& __a)

0 commit comments

Comments
 (0)