如何从元组中提取类型列表的struct/class

发布于 2025-01-27 07:37:54 字数 749 浏览 2 评论 0原文

我想在类中使用静态方法,该方法在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';
    }
};
using types = std::tuple<int, char, float, double>;
x<types>::test();

参见 godbolt上的示例

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2025-02-03 07:37:54

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

类似的东西

// 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';            
    }
};

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';            
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文