I want to create a tuple that has specializations for up to 10 args, similar to how std::pair is a specialization for two args.
i.e tuple<int,float,bool>
will have the members first()
, second()
, and third()
Here is my attempt so far:
#pragma once
#include <tuple>
#include <type_traits>
template<typename... Types>
struct tuple : std::tuple<Types...> {
using std::tuple<Types...>::tuple;
static constexpr size_t size = sizeof...(Types);
template<size_t N>
using elem_n = std::tuple_element_t<N, std::tuple<Types...>>;
template<size_t N>
const elem_n<N>& get() const { return std::get<N>(*this); }
template<size_t N>
elem_n<N>& get() { return std::get<N>(*this); }
template<bool F = false> std::enable_if_t< (size >= 1) || F, const elem_n<0>&> first() const { return get<0>(); }
template<bool F = false> std::enable_if_t< (size >= 2) || F, const elem_n<1>&> second() const { return get<1>(); }
template<bool F = false> std::enable_if_t< (size >= 3) || F, const elem_n<2>&> third() const { return get<2>(); }
template<bool F = false> std::enable_if_t< (size >= 4) || F, const elem_n<3>&> fourth() const { return get<3>(); }
template<bool F = false> std::enable_if_t< (size >= 5) || F, const elem_n<4>&> fith() const { return get<4>(); }
template<bool F = false> std::enable_if_t< (size >= 6) || F, const elem_n<5>&> sixth() const { return get<5>(); }
template<bool F = false> std::enable_if_t< (size >= 7) || F, const elem_n<6>&> seventh() const { return get<6>(); }
template<bool F = false> std::enable_if_t< (size >= 8) || F, const elem_n<7>&> eighth() const { return get<7>(); }
template<bool F = false> std::enable_if_t< (size >= 9) || F, const elem_n<8>&> ninth() const { return get<8>(); }
template<bool F = false> std::enable_if_t< (size >= 10) || F, const elem_n<9>&> tenth() const { return get<9>(); }
template<bool F = false> std::enable_if_t< (size >= 1) || F, elem_n<0>&> first() { return get<0>(); }
template<bool F = false> std::enable_if_t< (size >= 2) || F, elem_n<1>&> second() { return get<1>(); }
template<bool F = false> std::enable_if_t< (size >= 3) || F, elem_n<2>&> third() { return get<2>(); }
template<bool F = false> std::enable_if_t< (size >= 4) || F, elem_n<3>&> fourth() { return get<3>(); }
template<bool F = false> std::enable_if_t< (size >= 5) || F, elem_n<4>&> fith() { return get<4>(); }
template<bool F = false> std::enable_if_t< (size >= 6) || F, elem_n<5>&> sixth() { return get<5>(); }
template<bool F = false> std::enable_if_t< (size >= 7) || F, elem_n<6>&> seventh() { return get<6>(); }
template<bool F = false> std::enable_if_t< (size >= 8) || F, elem_n<7>&> eighth() { return get<7>(); }
template<bool F = false> std::enable_if_t< (size >= 9) || F, elem_n<8>&> ninth() { return get<8>(); }
template<bool F = false> std::enable_if_t< (size >= 1) || F, elem_n<9>&> tenth() { return get<9>(); }
};
I have also tried it with:
template<size_t N>
using elem_n = std::conditional_t<(size >= N), std::tuple_element_t<N, std::tuple<Types...>>, void>;
But when testing with
using my_tripple = tuple<int, std::string, float>;
my_tripple a;
a.first() = 6;
a.second() = "hello";
a.third() = 0.1f;
I get the compile errors:
/usr/include/c++/9/tuple:1303: error: static assertion failed: tuple index is in range
1303 | static_assert(__i < tuple_size<tuple<>>::value,
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
And from fourth()
to tenth()
error: no type named ‘type’ in ‘struct std::tuple_element<3, std::tuple<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> >’
26 | template<bool F = false> std::enable_if_t< (size >= 4) || F, const elem_n<3>&> fourth() const { return get<3>(); }
| ^~~~~~
This is related to the question c++ Use std::enable_if to conditionally add getters to a variadic variant template, but the solution from that doesn't work here.
Thanks
Please login or Register to submit your answer