如何从元组生成元组? [英] How to make a tuple from a tuple?

查看:46
本文介绍了如何从元组生成元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个转换模板(任何现有的库都可以做到这一点?)

For example, I have a convert template (any existing library to do it?)

template<class T> struct Convert;
template<> struct Convert<T0> {typedef C0 Type;};
template<> struct Convert<T1> {typedef C1 Type;};
template<> struct Convert<T2> {typedef C2 Type;};

从转换,它转换

std::tuple<T0, T1, T2>; // version A

std::tuple<C0, C1, C2>; // version B

任何一般的方法,比如

template<class tupleA, template<class> class Convert>
{
    typedef .... tupleB;
}

其他一些问题:(1) 我可以从特定的元组中获取它的可变参数吗?(2) 如果是这样,我可以对可变参数使用 convert 吗?

Some other questions: (1) Can I get its variadic parameters from a specific tuple? (2) If so, can I use convert on the variadic parameters?

推荐答案

试试这个:

template <typename... Args>
struct convert;

template <typename... Args>
struct convert<std::tuple<Args...>>
{
    typedef std::tuple<typename Convert<Args>::Type...> type;
};

这是一个示例程序:

#include <type_traits>
#include <tuple>

template<class T> struct Convert;
template<> struct Convert<int>  {typedef bool Type;};
template<> struct Convert<char> {typedef int Type;};
template<> struct Convert<bool> {typedef char Type;};

template <typename... Args>
struct convert;

template <typename... Args>
struct convert<std::tuple<Args...>>
{
    typedef std::tuple<typename Convert<Args>::Type...> type;
};

int main()
{
     static_assert(
        std::is_same<
            convert<std::tuple<int, char, bool>>::type,
            std::tuple<bool, int, char>
        >::value, ""
    );
}

演示

这篇关于如何从元组生成元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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