Last active
July 24, 2018 13:35
-
-
Save gatchamix/b0b941b1bff29beb76811b69186284e6 to your computer and use it in GitHub Desktop.
simple tuple container using C++17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <std::size_t N, class T> | |
struct tuple_elem | |
{ | |
constexpr tuple_elem(T&& val) | |
: val_{ std::forward<T>(val) } | |
{} | |
template <auto I, std::enable_if_t<I == N>* = nullptr> | |
auto constexpr& operator[](constant<I>) | |
{ return val_; } | |
template <auto I, std::enable_if_t<I == N>* = nullptr> | |
auto constexpr& operator[](constant<I>) const | |
{ return val_; } | |
private: | |
std::decay_t<T> val_; | |
}; | |
template <class, class...> | |
struct tuple_impl; | |
template <std::size_t... Is, class... Ts> | |
struct tuple_impl<std::index_sequence<Is...>, Ts...> | |
: tuple_elem<Is, Ts>... | |
{ | |
using tuple_elem<Is, Ts>::operator[]...; | |
constexpr tuple_impl(Ts&&... ts) | |
: tuple_elem<Is, Ts>{ std::forward<Ts>(ts) }... | |
{} | |
}; | |
template <class... Ts> | |
struct tuple | |
: tuple_impl<std::make_index_sequence<sizeof...(Ts)>, Ts...> | |
{ | |
static auto constexpr size = sizeof...(Ts); | |
constexpr tuple(Ts&&... ts) | |
: tuple_impl<std::make_index_sequence<size>, Ts...> | |
{ std::forward<Ts>(ts)... } | |
{} | |
}; | |
template <class... Ts> | |
tuple(Ts&&...) -> tuple<Ts...>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment