gpt4 book ai didi

c++ - 如何键入定义其他元组串联的元组?

转载 作者:搜寻专家 更新时间:2023-10-31 02:08:58 25 4
gpt4 key购买 nike

class A{ typedef tuple<int, double, int> list; };
class B{ typedef tuple<int, int> list; };
class C{ typedef tuple<int, double, char> list; };

template <typename... args>
class My_class{
//need a tuple type that is combination of all the tuple named list in the class A B C
};

My_class<A, B, C> obj;

如何在 My_class 中创建一个 typedef,使其等于 tuple<int, double, int, int, int, int, double, char>

最佳答案

假设您可以使 A::listB::listC::list 可公开访问,您需要的是:

template <typename... Args>
class My_class{
typedef decltype(tuple_cat(declval<typename Args::list>()...)) list;
};

它看起来很难看,但不幸的是,在 C++ 标准中没有连接元组类型的方法。你可以这样制作你自己的 tuple_flatten:

template <typename... Tuple>
struct tuple_flatten{
typedef decltype(tuple_cat(declval<Tuple>()...)) type;
};
template <typename... Tuple>
using tuple_flatten_t = typename tuple_flatten<Tuple...>::type;

使用:

template <typename... Args>
class My_class{
typedef typename tuple_flatten<typename Args::list...>::type list;
};

此外,根据大多数约定,类型参数 (Args) 应以大写字母开头。

关于c++ - 如何键入定义其他元组串联的元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46945478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com