Search code examples
c++variadic-templatesc++17

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 class template 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? 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>
struct EntityContext
{
    template<typename T, typename ... Ts>
    struct K {
        using type = T;
    };

    using FirstEntityType = typename K<EntityTs...>::type;
    
   // ...
};

Solution

  • You could write:

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

    Or you could use Boost.Mp11:

    using FirstEntityType = mp_front<EntityContext>;