Skip to content

Commit

Permalink
fix extern template instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
liss-h committed Mar 18, 2024
1 parent 6f63260 commit 5fe90a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
34 changes: 19 additions & 15 deletions include/dice/template-library/flex_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
namespace dice::template_library {
using std::dynamic_extent;

namespace detail_flex_array {
template<typename T, size_t extent, size_t max_extent>
struct flex_array_inner {
static constexpr size_t size_ = extent;
std::array<T, extent> data_;

constexpr auto operator<=>(flex_array_inner const &) const noexcept = default;
};

template<typename T, size_t max_extent>
struct flex_array_inner<T, dynamic_extent, max_extent> {
size_t size_ = 0;
std::array<T, max_extent> data_;

constexpr auto operator<=>(flex_array_inner const &) const noexcept = default;
};
} // namespace detail_flex_array

/**
* A combination of std::array and std::span.
* If extent_ is set to some integer value (!= dynamic_extent), flex_array behaves like std::array, i.e. has a fixed, statically known size, max_size/capacity.
Expand All @@ -32,21 +50,7 @@ namespace dice::template_library {
static constexpr bool is_dynamic_extent = extent == dynamic_extent;

private:
struct inner_type_fixed {
static constexpr size_t size_ = extent;
std::array<T, extent_> data_;

constexpr auto operator<=>(inner_type_fixed const &) const noexcept = default;
};

struct inner_type_dynamic {
size_t size_ = 0;
std::array<T, max_extent> data_;

constexpr auto operator<=>(inner_type_dynamic const &) const noexcept = default;
};

using inner_type = std::conditional_t<is_dynamic_extent, inner_type_dynamic, inner_type_fixed>;
using inner_type = detail_flex_array::flex_array_inner<T, extent_, max_extent_>;

public:
using value_type = T;
Expand Down
9 changes: 9 additions & 0 deletions tests/tests_flex_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
#include <algorithm>
#include <numeric>

namespace dice::template_library {
// extern template sometimes make problems if internal types are too eagerly instantiated
extern template struct flex_array<int, 5>;
extern template struct flex_array<int, dynamic_extent, 5>;

template struct flex_array<int, 5>;
template struct flex_array<int, dynamic_extent, 5>;
} // namespace dice::template_library

TEST_SUITE("flex_array") {
using namespace dice::template_library;

Expand Down

0 comments on commit 5fe90a6

Please sign in to comment.