我如何剥离一个元组返回到可变参数模板类型列表? [英] How do I strip a tuple<> back into a variadic template list of types?

查看:93
本文介绍了我如何剥离一个元组返回到可变参数模板类型列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以剥离一个 std :: tuple< T ...> 以便将其恢复为 T ...

Is there a way to strip a std::tuple<T...> in order to get it back to T...?

示例

假设 vct< T ...> 预先存在的 变量类模板

using U = std::tuple<int,char,std::string>;
using X = vct<int,char,std::string>;
using Y = vct< strip<U> >;            // should be same as X

注释

我了解 std :: tuple_element ,但我需要所有元素,其格式应为 T ...

I know about std::tuple_element, but I need all the elements, in a form that is usable as T...

,我发现了这个问题 ,这很相似,但是我的需求稍微简单了(所以我希望有一个更简单的解决方案):我需要的只是 tuple 中的类型列表不在乎 tuple 实例的实际值。

For reference, I have found this question, which is similar, but my needs are somewhat simpler (so I hope there is a simpler solution): all I need is the list of types that are in the tuple - I don't care about the actual values of a tuple instance.

推荐答案

否,这是不可能的。参数包是类型推导的结果,它们不能在其他上下文中生成。

No, this is not possible. Argument packs are the result of type deduction, and they can't be produced in other contexts.

您可以通过这种方式执行类似的操作:

You could do something similar to what you're asking for this way:

template<template<typename...> class T, typename>
struct instantiate_with_arg_pack { };

template<template<typename...> class T, typename... Ts>
struct instantiate_with_arg_pack<T, std::tuple<Ts...>>
{
    using type = T<Ts...>;
};

template<typename... Ts>
struct vct { };

int main()
{
    using U = std::tuple<int,char,std::string>;
    using X = vct<int,char,std::string>;
    using Y = instantiate_with_arg_pack<vct, U>::type;
}

实际上,您不需要将参数包保存在元组中:任何可变参数类模板都可以:

Actually, you don't need to hold the argument pack in a tuple: any variadic class template is OK:

template<template<typename...> class T, typename>
struct instantiate_with_arg_pack { };

template<
    template<typename...> class T, 
    template<typename...> class U, // <===
    typename... Ts
    >
struct instantiate_with_arg_pack<T, U<Ts...>>
//                                   ^^^^^^^^
{
    using type = T<Ts...>;
};

template<typename... Ts>
struct vct { };

int main()
{
    using U = std::tuple<int,char,std::string>;
    using X = vct<int,char,std::string>;
    using Y = instantiate_with_arg_pack<vct, X>::type;
    //                                        ^

    // Won't fire
    static_assert(
        std::is_same<Y, vct<int,char,std::string>>::value, 
        "Error!");
}

这是实时示例

这篇关于我如何剥离一个元组返回到可变参数模板类型列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆