Skip to content

Commit 766ca64

Browse files
committed
test const 0 capacity variants
1 parent 3b6ffee commit 766ca64

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
@@ -57,10 +57,13 @@ using inplace_vector_internal_size_type =
5757

5858
// array based storage is used so that we can satisfy constexpr requirement
5959
//
60-
// Selecting this storage type implies: std::is_trivial_v<T>
60+
// Selecting this storage type implies: std::is_trivial_v<T> or Capacity = 0
6161
template <typename T, std::size_t Capacity>
6262
struct inplace_vector_array_based_storage {
63-
std::array<T, Capacity> elems;
63+
using array_type = If<!std::is_const_v<T>, std::array<T, Capacity>,
64+
const std::array<std::remove_const_t<T>, Capacity>>;
65+
66+
array_type elems{};
6467

6568
constexpr T *begin() { return elems.data(); }
6669
constexpr const T *begin() const { return elems.data(); }
@@ -69,7 +72,7 @@ struct inplace_vector_array_based_storage {
6972
// byte array based storage is used for non-constexpr environment, where default
7073
// initialization may not be available.
7174
//
72-
// Selecting this storage type implies: !std::is_trivial_v<T>
75+
// Selecting this storage type implies: !std::is_trivial_v<T> and Capacity != 0
7376
template <typename T, std::size_t Capacity>
7477
struct inplace_vector_bytes_based_storage {
7578
alignas(T) unsigned char elems[Capacity * sizeof(T)];
@@ -86,7 +89,7 @@ struct inplace_vector_destruct_base {
8689
using size_type = std::size_t;
8790
using internal_size_type = inplace_vector_internal_size_type<Capacity>;
8891
using internal_storage_type =
89-
std::conditional_t<std::is_trivial_v<T>,
92+
std::conditional_t<std::is_trivial_v<T> or Capacity == 0,
9093
inplace_vector_array_based_storage<T, Capacity>,
9194
inplace_vector_bytes_based_storage<T, Capacity>>;
9295

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)