问题描述
我的问题很奇怪,很简短:为什么没有充分的理由为什么std :: tuple_size没有专门的参考?
(现在是动机)
我有很多看起来像这样的代码:
template< typename元组,typename索引=std :: make_index_sequence<std :: tuple_size<类型名称std :: remove_reference< Tuple> :: type> ::: value>>自动功能(Tuple& t,...)->decltype(...){返回 ...;}
我希望使用remove_reference并非错误的做法,但是由于它仅适用于类型级别,因此我在这里看不到任何问题.另一个可能的方法是使用std :: move传递元组,因为那时候Tuple不是引用,但是由于我只想转发元组,所以我也想传递左值引用,所以我使用了std :: remove_reference.>
还不错,我很好奇为什么tuple_size不能用于引用类型.有什么主意吗?
通常,类型特征对实际传递的类型起作用,而不是对任何可能相关的类型起作用.为 std :: tuple_size>
提供基本操作似乎是一致的,可用于创建更方便的版本.换一种方式,即拥有方便的版本并从中构建限制性更强的版本,似乎更成问题.
只需使用一个助手:
模板< typename T>结构my_tuple_size:std_integral_constant< std :: size_t,std :: tuple_size<类型名称std :: remove_reference< T> :: type>:: value>{};
(我希望我可以在手机上正确键入此内容)
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?
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)
这篇关于std :: tuple_size和参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!