You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
43 lines
1.2 KiB
|
2 days ago
|
#pragma once
|
||
|
|
|
||
|
|
#include <array>
|
||
|
|
#include <variant>
|
||
|
|
|
||
|
|
template <typename... Ts>
|
||
|
|
struct heter_array {
|
||
|
|
heter_array() : data(this->default_ctor_impl(std::make_index_sequence<N>{})) {}
|
||
|
|
|
||
|
|
~heter_array() = default;
|
||
|
|
|
||
|
|
heter_array(const heter_array&) = default;
|
||
|
|
heter_array(heter_array&&) = default;
|
||
|
|
heter_array& operator=(const heter_array&) = default;
|
||
|
|
heter_array& operator=(heter_array&&) = default;
|
||
|
|
|
||
|
|
auto& operator[](size_t i) { return data[i]; }
|
||
|
|
|
||
|
|
const auto& operator[](size_t i) const { return data[i]; }
|
||
|
|
|
||
|
|
template <typename F>
|
||
|
|
decltype(auto) visit(size_t i, F&& f)
|
||
|
|
{
|
||
|
|
return std::visit(std::forward<F>(f), data[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
template <typename F>
|
||
|
|
decltype(auto) visit(size_t i, F&& f) const
|
||
|
|
{
|
||
|
|
return std::visit(std::forward<F>(f), data[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected:
|
||
|
|
template <size_t... Is>
|
||
|
|
static auto default_ctor_impl(std::index_sequence<Is...>)
|
||
|
|
{
|
||
|
|
return std::array<heter_type, N>{heter_type{std::in_place_index<Is>}...};
|
||
|
|
}
|
||
|
|
|
||
|
|
using heter_type = std::variant<Ts...>;
|
||
|
|
static constexpr size_t N = sizeof...(Ts);
|
||
|
|
std::array<heter_type, N> data{};
|
||
|
|
};
|