Skip to content

Commit 541ad68

Browse files
committed
remove inplace_vector_array_type and add const tests
1 parent 12fe05e commit 541ad68

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

include/beman/inplace_vector/inplace_vector.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,14 @@ using inplace_vector_internal_size_type =
5656
If<Capacity <= std::numeric_limits<uint32_t>::max(), uint32_t,
5757
uint64_t>>>;
5858

59-
template <typename T, std::size_t Capacity>
60-
using inplace_vector_array_type =
61-
If<!std::is_const_v<T>, std::array<T, Capacity>,
62-
const std::array<std::remove_const_t<T>, Capacity>>;
63-
6459
// array based storage is used so that we can satisfy constexpr requirement
6560
//
6661
// Selecting this storage type implies: std::is_trivial_v<T> or Capacity = 0
6762
template <typename T, std::size_t Capacity>
6863
struct inplace_vector_type_based_storage {
69-
inplace_vector_array_type<T, Capacity> elems{};
64+
using array_type = If<!std::is_const_v<T>, std::array<T, Capacity>,
65+
const std::array<std::remove_const_t<T>, Capacity>>;
66+
array_type elems{};
7067

7168
constexpr T *begin() { return elems.data(); }
7269
constexpr const T *begin() const { return elems.data(); }
@@ -78,11 +75,11 @@ struct inplace_vector_type_based_storage {
7875
// Selecting this storage type implies: !std::is_trivial_v<T> and Capacity != 0
7976
template <typename T, std::size_t Capacity>
8077
struct inplace_vector_bytes_based_storage {
81-
alignas(T) inplace_vector_array_type<std::byte, Capacity * sizeof(T)> elems;
78+
alignas(T) std::array<std::byte, Capacity * sizeof(T)> elems;
8279

83-
T *begin() { return std::launder(reinterpret_cast<const T *>(elems)); }
80+
T *begin() { return std::launder(reinterpret_cast<const T *>(elems.data())); }
8481
const T *begin() const {
85-
return std::launder(reinterpret_cast<const T *>(elems));
82+
return std::launder(reinterpret_cast<const T *>(elems.data()));
8683
}
8784
};
8885

tests/beman/inplace_vector/inplace_vector.test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22
#include <beman/inplace_vector/inplace_vector.hpp>
3+
#include <type_traits>
34

45
using namespace beman::inplace_vector;
56

7+
struct NonTrivial {
8+
int z = 5;
9+
10+
bool operator==(NonTrivial const &other) { return this->z == other.z; }
11+
};
12+
static_assert(!std::is_trivial_v<NonTrivial>);
13+
614
template <typename T> constexpr void test() {
715
using vec = inplace_vector<T, 42>;
816
vec range{T(1), T(1337), T(42), T(12), T(0), T(-1)};
@@ -82,8 +90,19 @@ void test_exceptions() {
8290
}
8391
}
8492
}
93+
94+
template <typename T> void test_const() {
95+
inplace_vector<const T, 20> vec;
96+
vec.push_back({50});
97+
const T &first = vec.front();
98+
assert(first == T{50});
99+
}
100+
85101
int main() {
86102
test<int>();
87103
test_exceptions();
104+
105+
test_const<int>();
106+
test_const<NonTrivial>();
88107
return 0;
89108
}

0 commit comments

Comments
 (0)