如何从结构/类的元组中提取类型列表

Chr*_* G. 4 c++ templates variadic-templates template-argument-deduction c++20

我想在类中使用静态方法来获取表单中的类型列表std::tuple<T1, T2, T3,...>。我不想与 一起工作,而是std::tuple<...>想拥有<...>. 如何实现示例struct x结果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';
    }
};
Run Code Online (Sandbox Code Playgroud)
using types = std::tuple<int, char, float, double>;
x<types>::test();
Run Code Online (Sandbox Code Playgroud)

请参阅godbolt 上的示例

max*_*x66 5

在我看来,您正在寻找模板专业化。

诸如此类的东西

// 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';            
    }
};
Run Code Online (Sandbox Code Playgroud)