如何从元组中生成元组
How to make a tuple from a tuple?
本文关键字:元组 更新时间:2023-10-16
例如,我有一个转换模板(任何现有的库都可以这样做吗?)
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) 如果是,我可以对变参数使用转换吗?
试试这个:
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, ""
);
}
演示
相关文章:
- C++:TypeDef使用元组
- Pybind11:将元组列表从Python传递到C++
- 重载元组索引运算符-C++
- 在C++中,如何通过几种类型从元组中选择多个元素
- 将fold表达式与std::一起用于两个元组
- std::ranges::elements_view,用于自定义类似元组的数据
- 将元组的向量转换/构造为堆
- 专用于 std 元组的模板,而无需用户执行remove_cvref
- 将元组的向量构造成堆
- 元组由 Swig 生成的 Python 包装器返回,用于C++向量
- 将元组类型扩展为可变参数模板?
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 类内部和外部静态 constexpr 元组之间的差异
- 可变参数模板与使用元组在参数中添加不同的数据对
- 访问和打印元组中的数据,并使用 C++14 使用模板函数显示数据
- boost::包含提升单元的元组的哈希值
- 我正在寻找一种优雅的方式来从元组向量创建tuple_element向量
- 如何在可变参数模板函数中遍历可变参数元组?
- 如何使用 SML 随机生成八进制元组
- 元组的运行时索引