2

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

1 Answer 1

2

Your approach of using a defaulted template parameter for second(), third() was the right direction, but what you missed is that you needed to make get<>'s template parameter dependent on the default template parameter, so it does not get resolved until template instantiation time, and never gets resolved if it's never used. Unless it's dependent on the template parameter, it gets resolved at declaration time, and it fails for the reasons given.

A short example using just second(). third(), fourth(), et. al. would get declared the same way, using size_t n=2, size_t n=3, and so on:

#include <tuple>

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<size_t n=1> auto second() const { return get<n>(); }
};

tuple<int> foo;

tuple<int, float> bar;

float foobar()
{
    return bar.second();  // compiles, foo.first() would be a compilation error
}

In foo's case, since get<1> never actually exists unless second() gets explicitly called, there is no compilation error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.