How to make a tuple from a tuple?

孤人 提交于 2019-12-12 05:16:37

问题


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

From the convert, It convert

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

To

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

Any way to do it generally, like

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

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?


回答1:


Try this:

template <typename... Args>
struct convert;

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

Here is an example program:

#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, ""
    );
}

Demo



来源:https://stackoverflow.com/questions/17618938/how-to-make-a-tuple-from-a-tuple

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!