Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::tuple_size and references

Tags:

c++

c++11

stl

My question is of the curious kind and quite short: Is there a good reason why there are no specializations of std::tuple_size for references?

(now the motivation)

I have a lot of code that looks like:

template<typename Tuple, typename Indices = 
    std::make_index_sequence<
        std::tuple_size<
            typename std::remove_reference<Tuple>::type>::value
        >
    >
auto func(Tuple&& t, ...) -> decltype(...) {
    return ...;
}

I hope using remove_reference is not the-wrong-way-to-do-it, but since it only works on type-level I see no problem here. Another possibilty would be to pass the tuples using std::move since then Tuple is not a reference, but since I only want to forward tuples I'd like to pass lvalue refs, too, so I used std::remove_reference.

It is not that bad, I was just curious why tuple_size doesn't work with reference types. Any idea?

like image 893
Richard Vock Avatar asked Mar 22 '26 15:03

Richard Vock


1 Answers

In general the type traits operate on the type actually passed rather than any of the potentially related types. It seems consistent to provide the fundamental operation for std::tuple_size> which can the be used to create more convenient versions. Going the other way, i.e., have the convenient version and build the more restrictive version from it, seems more problematic.

Just use a helper:

template <typename T>
struct my_tuple_size
   : std_integral_constant<std::size_t,
        std::tuple_size<
            typename std::remove_reference<T>::type>
             ::value> {
};

(I hope I typed this properly on my mobile)

like image 173
Dietmar Kühl Avatar answered Mar 25 '26 05:03

Dietmar Kühl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!