Skip to content

Commit c9f2d6e

Browse files
committed
test const 0 capacity variants
1 parent 548a282 commit c9f2d6e

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

include/beman/inplace_vector/inplace_vector.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ using inplace_vector_internal_size_type =
4545

4646
// array based storage is used so that we can satisfy constexpr requirement
4747
//
48-
// Selecting this storage type implies: std::is_trivial_v<T>
48+
// Selecting this storage type implies: std::is_trivial_v<T> or Capacity = 0
4949
template <typename T, std::size_t Capacity>
5050
struct inplace_vector_array_based_storage {
51-
std::array<T, Capacity> elems;
51+
using array_type = If<!std::is_const_v<T>, std::array<T, Capacity>,
52+
const std::array<std::remove_const_t<T>, Capacity>>;
53+
54+
array_type elems{};
5255

5356
constexpr T *begin() { return elems.data(); }
5457
constexpr const T *begin() const { return elems.data(); }
@@ -57,7 +60,7 @@ struct inplace_vector_array_based_storage {
5760
// byte array based storage is used for non-constexpr environment, where default
5861
// initialization may not be available.
5962
//
60-
// Selecting this storage type implies: !std::is_trivial_v<T>
63+
// Selecting this storage type implies: !std::is_trivial_v<T> and Capacity != 0
6164
template <typename T, std::size_t Capacity>
6265
struct inplace_vector_bytes_based_storage {
6366
alignas(T) unsigned char elems[Capacity * sizeof(T)];
@@ -74,7 +77,7 @@ struct inplace_vector_destruct_base {
7477
using size_type = std::size_t;
7578
using internal_size_type = inplace_vector_internal_size_type<Capacity>;
7679
using internal_storage_type =
77-
std::conditional_t<std::is_trivial_v<T>,
80+
std::conditional_t<std::is_trivial_v<T> or Capacity == 0,
7881
inplace_vector_array_based_storage<T, Capacity>,
7982
inplace_vector_bytes_based_storage<T, Capacity>>;
8083

src/beman/inplace_vector/tests/constexpr.test.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,51 +16,55 @@ struct NonTrivial {
1616
};
1717
static_assert(!std::is_trivial_v<NonTrivial>);
1818

19-
static_assert(std::invoke([]() {
20-
inplace_vector<int, 0> vec;
19+
template <typename T> constexpr bool test_empty_vec(T &vec) {
2120

22-
// sizes
23-
S_ASSERT(vec.max_size() == 0);
24-
S_ASSERT(vec.capacity() == 0);
25-
S_ASSERT(vec.size() == 0);
26-
S_ASSERT(vec.empty());
21+
// sizes
22+
S_ASSERT(vec.max_size() == 0);
23+
S_ASSERT(vec.capacity() == 0);
24+
S_ASSERT(vec.size() == 0);
25+
S_ASSERT(vec.empty());
2726

28-
// itr
29-
S_ASSERT(vec.begin() == vec.end());
30-
S_ASSERT(vec.cbegin() == vec.cend());
31-
S_ASSERT(vec.rbegin() == vec.rend());
32-
S_ASSERT(vec.crbegin() == vec.crend());
27+
// itr
28+
S_ASSERT(vec.begin() == vec.end());
29+
S_ASSERT(vec.cbegin() == vec.cend());
30+
S_ASSERT(vec.rbegin() == vec.rend());
31+
S_ASSERT(vec.crbegin() == vec.crend());
3332

34-
// push_back
35-
S_ASSERT(vec.try_push_back(0) == nullptr);
36-
S_ASSERT(vec.try_emplace_back(0) == nullptr);
33+
// push_back
34+
S_ASSERT(vec.try_push_back({}) == nullptr);
35+
S_ASSERT(vec.try_emplace_back() == nullptr);
36+
37+
return true;
38+
}
3739

40+
static_assert(std::invoke([]() {
41+
inplace_vector<int, 0> vec;
42+
test_empty_vec(vec);
3843
return true;
3944
}),
4045
"0 capacity Trivial type");
4146

4247
static_assert(std::invoke([]() {
43-
inplace_vector<NonTrivial, 0> vec;
44-
45-
// sizes
46-
S_ASSERT(vec.max_size() == 0);
47-
S_ASSERT(vec.capacity() == 0);
48-
S_ASSERT(vec.size() == 0);
49-
S_ASSERT(vec.empty());
50-
51-
// itr
52-
S_ASSERT(vec.begin() == vec.end());
53-
S_ASSERT(vec.cbegin() == vec.cend());
54-
S_ASSERT(vec.rbegin() == vec.rend());
55-
S_ASSERT(vec.crbegin() == vec.crend());
56-
57-
// push_back
58-
S_ASSERT(vec.try_push_back({}) == nullptr);
48+
inplace_vector<const int, 0> vec;
49+
test_empty_vec(vec);
50+
return true;
51+
}),
52+
"0 capacity Trivial const type");
5953

54+
static_assert(std::invoke([]() {
55+
inplace_vector<NonTrivial, 0> vec;
56+
test_empty_vec(vec);
6057
return true;
6158
}),
6259
"0 capacity Non-trivial type");
6360

61+
static_assert(std::invoke([]() {
62+
inplace_vector<const NonTrivial, 0> vec;
63+
test_empty_vec(vec);
64+
return true;
65+
}),
66+
"0 capacity Non-trivial const type");
67+
6468
static_assert(std::invoke([]() {
6569
// sizes
6670
{

0 commit comments

Comments
 (0)