Reputation: 1468
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?
Upvotes: 3
Views: 868
Reputation: 153830
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)
Upvotes: 1