Skip to content

Commit e886812

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

File tree

10 files changed

+22
-37
lines changed

10 files changed

+22
-37
lines changed

libcxx/include/__memory/allocator_traits.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ template <class _Traits, class _Tp>
372372
using __rebind_alloc = typename _Traits::template rebind_alloc<_Tp>::other;
373373
#endif
374374

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

libcxx/include/deque

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,8 @@ public:
477477
using reverse_iterator = std::reverse_iterator<iterator>;
478478
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
479479

480-
static_assert(is_same<allocator_type, __rebind_alloc<__alloc_traits, value_type> >::value,
481-
"[allocator.requirements] states that rebinding an allocator to the same type should result in the "
482-
"original allocator");
480+
static_assert(__check_valid_allocator<allocator_type>::value, "");
481+
483482
static_assert(is_nothrow_default_constructible<allocator_type>::value ==
484483
is_nothrow_default_constructible<__pointer_allocator>::value,
485484
"rebinding an allocator should not change excpetion 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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,7 @@ public:
692692
typedef void __remove_return_type;
693693
#endif
694694

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");
695+
static_assert(__check_valid_allocator<allocator_type>::value);
698696

699697
_LIBCPP_HIDE_FROM_ABI list() _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value) {}
700698
_LIBCPP_HIDE_FROM_ABI explicit list(const allocator_type& __a) : base(__a) {}

libcxx/include/map

Lines changed: 2 additions & 6 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

@@ -1684,9 +1682,7 @@ private:
16841682
typedef typename __base::__node_traits __node_traits;
16851683
typedef allocator_traits<allocator_type> __alloc_traits;
16861684

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");
1685+
static_assert(__check_valid_allocator<allocator_type>::value, "");
16901686

16911687
__base __tree_;
16921688

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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,7 @@ public:
782782
static_assert((is_same<typename allocator_type::value_type, value_type>::value),
783783
"Allocator::value_type must be same type as value_type");
784784

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");
785+
static_assert(__check_valid_allocator<allocator_type>::value, "");
788786

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

libcxx/include/unordered_map

Lines changed: 2 additions & 6 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), "");
@@ -1866,9 +1864,7 @@ private:
18661864
static_assert((is_same<typename __node_traits::size_type, typename __alloc_traits::size_type>::value),
18671865
"Allocator uses different size_type for different types");
18681866

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");
1867+
static_assert(__check_valid_allocator<allocator_type>::value, "");
18721868

18731869
public:
18741870
typedef typename __alloc_traits::pointer pointer;

libcxx/include/unordered_set

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,7 @@ public:
591591
static_assert((is_same<value_type, typename allocator_type::value_type>::value),
592592
"Allocator::value_type must be same type as value_type");
593593

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");
594+
static_assert(__check_valid_allocator<allocator_type>::value, "");
597595

598596
private:
599597
typedef __hash_table<value_type, hasher, key_equal, allocator_type> __table;

libcxx/include/vector

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ public:
409409
static_assert((is_same<typename allocator_type::value_type, value_type>::value),
410410
"Allocator::value_type must be same type as value_type");
411411

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

416414
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI vector()
417415
_NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value) {}

0 commit comments

Comments
 (0)