是否可以单线获取第一类参数包? [英] Is it possible to get the first type of a parameter pack in a one-liner?
问题描述
我在可变参数模板类中给出了一个参数包,并且想要提取第一种类型。
I have a parameter pack given in a variadic template class and want to extract the first type.
目前,我这样做是可行的,但是有点麻烦。是否可以使同一件事更简单? FirstEntityType
应该定义为具有 EntityTs
中第一类型的类型。注意,我要保留类模板的签名。我知道可以写 template< typename FirstEntityType,typename ... OtherEntityTypes>
,但这是我不想做的事。
Currently I do this, which works fine but is somehow cumbersome. Is it possible to do the same thing simpler? FirstEntityType
should be defined to have the type of the first type in EntityTs
. Note, I want to keep the signature of the class template. I know that it would be possible to write template<typename FirstEntityType, typename... OtherEntityTypes>
, it is however something that I don't want to do.
template<typename... EntityTs>
class EntityContext
{
template<typename T, typename ... Ts>
struct K {
using type = T;
};
using FirstEntityType = typename K<EntityTs...>::type;
// ...
}
推荐答案
您可以使用FirstEntityType = std :: tuple_element_t< 0,std :: tuple<来编写:
You could write:
using FirstEntityType = std::tuple_element_t<0, std::tuple<EntityTs...>>;
这篇关于是否可以单线获取第一类参数包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!