Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ jobs:
}
],
"clang": [
{ "versions": ["trunk"],
"tests": [
{ "cxxversions": ["c++26"],
"tests": [
{"stdlibs": ["libstdc++", "libc++"], "tests": ["Release.Default"]}
]
}
]
},
{ "versions": ["23"],
"tests": [
{"cxxversions": ["c++26"],
Expand Down
8 changes: 8 additions & 0 deletions include/beman/inplace_vector/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@
#define BEMAN_INPLACE_VECTOR_NO_EXCEPTIONS() 0
#endif

#ifndef BEMAN_INPLACE_VECTOR_HAS_TRIVIAL_UNION
#if defined(__cpp_trivial_union) && __cpp_trivial_union >= 202602L
#define BEMAN_INPLACE_VECTOR_HAS_TRIVIAL_UNION 1
#else
#define BEMAN_INPLACE_VECTOR_HAS_TRIVIAL_UNION 0
#endif
#endif

#endif
63 changes: 58 additions & 5 deletions include/beman/inplace_vector/inplace_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ concept satisfy_triviality = std::is_trivially_copyable_v<T> &&
std::is_trivially_destructible_v<T>;

template <typename T, std::size_t N>
concept satisfy_constexpr = N == 0 || satisfy_triviality<T>;
concept satisfy_constexpr =
#if BEMAN_INPLACE_VECTOR_HAS_TRIVIAL_UNION
true;
#else
N == 0 || satisfy_triviality<T>;
#endif

template <typename T>
concept lessthan_comparable = requires(const T &a, const T &b) {
Expand Down Expand Up @@ -140,6 +145,7 @@ template <class T, size_t N> struct trivial {
// which makes inplace_vector of non-trivial types non-constexpr-friendly.
//
// Note: This is not used if trivial union is supported.
#if !BEMAN_INPLACE_VECTOR_HAS_TRIVIAL_UNION
template <class T, size_t N> struct raw_byte_based_storage {
alignas(T) std::byte _d[sizeof(T) * N];
T *storage_data(size_t i) noexcept {
Expand Down Expand Up @@ -193,13 +199,60 @@ template <class T, size_t N> struct non_trivial {
std::destroy(storage_data(), storage_data() + storage_size());
}
};
#else
template <class T, size_t N> struct union_storage {
static_assert(!satisfy_triviality<T>,
"use storage::trivial for Trivial<T> elements");
static_assert(N != size_t{0}, "use storage::zero for N==0");

protected:
using size_type = smallest_size_t<N>;

private:
union {
T storage_[N];
};
size_type storage_size_ = 0;

protected:
constexpr const T *storage_data() const noexcept { return storage_; }
constexpr auto storage_data() noexcept { return storage_; }
constexpr size_type storage_size() const noexcept { return storage_size_; }
constexpr void unsafe_set_size(size_t new_size) noexcept {
IV_EXPECT(size_type(new_size) <= N && "new_size out-of-bounds [0, N)");
storage_size_ = size_type(new_size);
}

public:
constexpr union_storage() noexcept { std::start_lifetime(storage_); }
constexpr union_storage(const union_storage &) noexcept = default;
constexpr union_storage &operator=(const union_storage &) noexcept = default;
constexpr union_storage(union_storage &&) noexcept = default;
constexpr union_storage &operator=(union_storage &&) noexcept = default;

constexpr ~union_storage()
requires(std::is_trivially_destructible_v<T>)
= default;
constexpr ~union_storage()
requires(!std::is_trivially_destructible_v<T>)
{
std::destroy(storage_data(), storage_data() + storage_size());
}
};
#endif

// Selects the vector storage.
template <class T, size_t N>
using storage_for = std::conditional_t<
!satisfy_constexpr<T, N>, non_trivial<T, N>,
std::conditional_t<N == 0, zero_sized<T>, trivial<T, N>>>;

using storage_for =
#if BEMAN_INPLACE_VECTOR_HAS_TRIVIAL_UNION
std::conditional_t<N == 0, zero_sized<T>,
std::conditional_t<satisfy_triviality<T>, trivial<T, N>,
union_storage<T, N>>>;
#else
std::conditional_t<
!satisfy_constexpr<T, N>, non_trivial<T, N>,
std::conditional_t<N == 0, zero_sized<T>, trivial<T, N>>>;
#endif
} // namespace storage

template <class T, size_t N>
Expand Down
28 changes: 28 additions & 0 deletions tests/beman/inplace_vector/constexpr.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,34 @@ static_assert(!has_constexpr_support<inplace_vector<std::string, 50>>);
static_assert(has_constexpr_support<inplace_vector<std::unique_ptr<int>, 0>>);
static_assert(!has_constexpr_support<inplace_vector<std::unique_ptr<int>, 50>>);

constexpr bool test_non_trivial_constexpr() {
inplace_vector<std::string, 4> v;

// Construction and emplacement in constant evaluation.
v.emplace_back("hello");
v.emplace_back("world");

if (v.size() != 2)
return false;

if (v[0] != "hello" || v[1] != "world")
return false;

// Modification of an existing element.
v[0] = "goodbye";

if (v[0] != "goodbye")
return false;

// Destruction of the vector with live non-trivial elements is
// also exercised when leaving this function.
return true;
}

#if BEMAN_INPLACE_VECTOR_HAS_TRIVIAL_UNION
static_assert(test_non_trivial_constexpr());
#endif

#define TEST(NAME) \
static_assert(std::invoke([]() { \
NAME<beman::inplace_vector::inplace_vector<int, 20>>(); \
Expand Down
Loading