Home > front end >  How to extract type list from tuple for struct/class
How to extract type list from tuple for struct/class

Time:05-09

I want to use a static method in a class that gets a type list in the form std::tuple<T1, T2, T3,...>. Instead of working with std::tuple<...> I want to have <...>. How to implement example struct x resulting in Ts == <T1, T2, T3,...>

template<template <typename...> typename TL, typename... Ts>
struct x {
    static void test() {
        // expecting Ts == <int, char, float, double> (without std::tuple<>)
        std::cout << __PRETTY_FUNCTION__ << '\n';
    }
};
using types = std::tuple<int, char, float, double>;
x<types>::test();

See example on godbolt

CodePudding user response:

It seems to me that you're looking for template specialization.

Something as

// declaration (not definition) for a template struct x
// receiving a single template parameter 
template <typename>
struct x;

// definition for a x specialization when the template
// parameter is in the form TL<Ts...>
template<template<typename...> typename TL, typename... Ts>
struct x<TL<Ts...>> {
    static constexpr void test() {
        // you can use Ts... (and also TL, if you want) here
        std::cout << __PRETTY_FUNCTION__ << ' ' << sizeof...(Ts) << '\n';            
    }
};
  • Related