Is it possible to get the first type of a parameter pack in a one-liner?

浪子不回头ぞ 提交于 2019-12-10 09:53:38

问题


I have a parameter pack given in a variadic template class and want to extract the first type.

Currently I do this, which works fine but is somehow cumbersome. Is it possible to do the same thing simpler? FirstEntityTypeshould 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;

   // ...
}

回答1:


You could write:

using FirstEntityType = std::tuple_element_t<0, std::tuple<EntityTs...>>;



回答2:


You may use

std::tuple_element<0, std::tuple<EntityTs...>>::type


来源:https://stackoverflow.com/questions/45578484/is-it-possible-to-get-the-first-type-of-a-parameter-pack-in-a-one-liner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!