Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static_assert that container and allocator type parameters are object types #2436

Merged
merged 9 commits into from
Apr 14, 2023
6 changes: 6 additions & 0 deletions stl/inc/array
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ struct pointer_traits<_Array_iterator<_Ty, _Size>> {
_EXPORT_STD template <class _Ty, size_t _Size>
class array { // fixed size array of values
public:
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using value_type = _Ty;
using size_type = size_t;
using difference_type = ptrdiff_t;
Expand Down Expand Up @@ -592,6 +595,9 @@ struct _Empty_array_element {};
template <class _Ty>
class array<_Ty, 0> {
public:
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using value_type = _Ty;
using size_type = size_t;
using difference_type = ptrdiff_t;
Expand Down
2 changes: 2 additions & 0 deletions stl/inc/deque
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ private:
friend _Tidy_guard<deque>;
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Ty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("deque<T, Allocator>", "T"));
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using _Alty = _Rebind_alloc_t<_Alloc, _Ty>;
using _Alty_traits = allocator_traits<_Alty>;
Expand Down
2 changes: 2 additions & 0 deletions stl/inc/forward_list
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ private:

static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Ty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("forward_list<T, Allocator>", "T"));
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using _Scary_val = _Flist_val<conditional_t<_Is_simple_alloc_v<_Alnode>, _Flist_simple_types<_Ty>,
_Flist_iter_types<_Ty, typename _Alty_traits::size_type, typename _Alty_traits::difference_type,
Expand Down
2 changes: 2 additions & 0 deletions stl/inc/list
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,8 @@ private:
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Ty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("list<T, Allocator>", "T"));
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using value_type = _Ty;
using allocator_type = _Alloc;
Expand Down
8 changes: 8 additions & 0 deletions stl/inc/map
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class map : public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false>> {
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<pair<const _Kty, _Ty>, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("map<Key, Value, Compare, Allocator>", "pair<const Key, Value>"));
static_assert(is_object_v<_Kty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using _Mybase = _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false>>;
using _Nodeptr = typename _Mybase::_Nodeptr;
Expand Down Expand Up @@ -457,6 +461,10 @@ class multimap : public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, true>> {
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<pair<const _Kty, _Ty>, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("multimap<Key, Value, Compare, Allocator>", "pair<const Key, Value>"));
static_assert(is_object_v<_Kty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using _Mybase = _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, true>>;
using key_type = _Kty;
Expand Down
4 changes: 4 additions & 0 deletions stl/inc/queue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public:
using container_type = _Container;

static_assert(is_same_v<_Ty, value_type>, "container adaptors require consistent types");
static_assert(is_object_v<_Ty>, "The C++ Standard forbids container adaptors of non-object types "
"because of [container.requirements].");

queue() = default;

Expand Down Expand Up @@ -234,6 +236,8 @@ public:
using value_compare = _Pr;

static_assert(is_same_v<_Ty, value_type>, "container adaptors require consistent types");
static_assert(is_object_v<_Ty>, "The C++ Standard forbids container adaptors of non-object types "
"because of [container.requirements].");

priority_queue() = default;

Expand Down
4 changes: 4 additions & 0 deletions stl/inc/set
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class set : public _Tree<_Tset_traits<_Kty, _Pr, _Alloc, false>> {
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Kty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("set<T, Compare, Allocator>", "T"));
static_assert(is_object_v<_Kty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using _Mybase = _Tree<_Tset_traits<_Kty, _Pr, _Alloc, false>>;
using key_type = _Kty;
Expand Down Expand Up @@ -265,6 +267,8 @@ class multiset : public _Tree<_Tset_traits<_Kty, _Pr, _Alloc, true>> {
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Kty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("multiset<T, Compare, Allocator>", "T"));
static_assert(is_object_v<_Kty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using _Mybase = _Tree<_Tset_traits<_Kty, _Pr, _Alloc, true>>;
using key_type = _Kty;
Expand Down
2 changes: 2 additions & 0 deletions stl/inc/stack
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public:
using container_type = _Container;

static_assert(is_same_v<_Ty, value_type>, "container adaptors require consistent types");
static_assert(is_object_v<_Ty>, "The C++ Standard forbids container adaptors of non-object types "
"because of [container.requirements].");

stack() = default;

Expand Down
8 changes: 8 additions & 0 deletions stl/inc/unordered_map
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class unordered_map : public _Hash<_Umap_traits<_Kty, _Ty, _Uhash_compare<_Kty,
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<pair<const _Kty, _Ty>, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("unordered_map<Key, Value, Hasher, Eq, Allocator>", "pair<const Key, Value>"));
static_assert(is_object_v<_Kty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

private:
using _Mytraits = _Uhash_compare<_Kty, _Hasher, _Keyeq>;
Expand Down Expand Up @@ -557,6 +561,10 @@ public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<pair<const _Kty, _Ty>, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE(
"unordered_multimap<Key, Value, Hasher, Eq, Allocator>", "pair<const Key, Value>"));
static_assert(is_object_v<_Kty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

private:
using _Mytraits = _Uhash_compare<_Kty, _Hasher, _Keyeq>;
Expand Down
4 changes: 4 additions & 0 deletions stl/inc/unordered_set
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class unordered_set : public _Hash<_Uset_traits<_Kty, _Uhash_compare<_Kty, _Hash
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Kty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("unordered_set<T, Hasher, Eq, Allocator>", "T"));
static_assert(is_object_v<_Kty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

private:
using _Mytraits = _Uhash_compare<_Kty, _Hasher, _Keyeq>;
Expand Down Expand Up @@ -411,6 +413,8 @@ class unordered_multiset : public _Hash<_Uset_traits<_Kty, _Uhash_compare<_Kty,
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Kty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("unordered_multiset<T, Hasher, Eq, Allocator>", "T"));
static_assert(is_object_v<_Kty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

private:
using _Mytraits = _Uhash_compare<_Kty, _Hasher, _Keyeq>;
Expand Down
2 changes: 2 additions & 0 deletions stl/inc/vector
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ private:
public:
static_assert(!_ENFORCE_MATCHING_ALLOCATORS || is_same_v<_Ty, typename _Alloc::value_type>,
_MISMATCHED_ALLOCATOR_MESSAGE("vector<T, Allocator>", "T"));
static_assert(is_object_v<_Ty>, "The C++ Standard forbids containers of non-object types "
"because of [container.requirements].");

using value_type = _Ty;
using allocator_type = _Alloc;
Expand Down
4 changes: 4 additions & 0 deletions stl/inc/xmemory
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,10 @@ class allocator {
public:
static_assert(!is_const_v<_Ty>, "The C++ Standard forbids containers of const elements "
"because allocator<const T> is ill-formed.");
static_assert(!is_function_v<_Ty>, "The C++ Standard forbids allocators for function elements "
"because of [allocator.requirements].");
static_assert(!is_reference_v<_Ty>, "The C++ Standard forbids allocators for reference elements "
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
"because of [allocator.requirements].");

using _From_primary = allocator;

Expand Down