反转元组参数
Invert tuple arguments
我想编写一个模板类InvTuple
,它将type
定义为按相反顺序排列的类参数元组。所以它应该像一样工作
InvTuple<T1, T2, T3, ...>::type ---> tuple<..., T3, T2, T1>
我这样定义
template<class...T>
struct InvTuple;
template<class T1, class...T>
struct InvTuple < T1, T... >
{
template<class... U>
using doInvert = typename InvTuple<T...>::doInvert < U..., T1 > ;
// <--- unrecognizable template declaration/definition,
// syntax error : '<'
using type = doInvert<>;
};
template<>
struct InvTuple <>
{
template<class... U>
using doInvert = tuple<U...>;
using type = doInvert < > ;
};
但是,由于代码中显示的错误,这不会编译。请帮我弄清楚出了什么问题。
您需要模板关键字:
using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ;
您还需要在同一行中切换U...
和T1
才能正常工作:
#include <iostream>
#include <tuple>
#include <typeinfo>
using namespace std; // Don't try this at home
template<class...T>
struct InvTuple;
template<class T1, class...T>
struct InvTuple < T1, T... >
{
template<class... U>
using doInvert = typename InvTuple<T...>::template doInvert < T1, U... >;
using type = doInvert<>;
};
template<>
struct InvTuple <>
{
template<class... U>
using doInvert = tuple<U...>;
using type = doInvert < > ;
};
int main()
{
InvTuple<int,char,bool> obj;
InvTuple<int,char,bool>::type obj2;
cout << typeid(obj).name() << endl; // InvTuple<int, char, bool>
cout << typeid(obj2).name() << endl; // std::tuple<bool, char, int>
}
示例
您需要这个:
using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ;
在中间缺少template
关键字。
相关文章:
- 将元组类型扩展为可变参数模板?
- 可变参数模板与使用元组在参数中添加不同的数据对
- 如何在可变参数模板函数中遍历可变参数元组?
- 创建派生自可变参数模板包的类型元组
- 如何在 c++ 中存储具有值的四个参数的元组
- 将函数应用于元组中的每个元素,将每个元素强制转换为类型包中的不同类型,然后作为参数包传递
- 试图在C++14中将元组应用为参数
- 从函数的可变参数构建特定元组
- 将可变参数模板包装到元组向量
- 从函数返回元组时,将复制元组的参数,而不是移动元组参数
- 使用每种类型的可变参数模板上的类模板初始化元组
- C++ 从模板参数创建元组向量
- 如何从参数包构造引用的 std::元组?
- 有没有办法将一对元组剥离为可变参数模板类型或实例化具有可变参数类型的东西?
- 从可调用可变参数元组中的函数结果创建元组
- 返回元组的可变参数模板
- 当元组给出参数时,如何检查方法是否存在?
- 如何部分专业化功能以用元组元素作为参数调用功能
- 在 c++17 中解压缩可变参数元组
- 如何使用已知的非默认构造函数实例化未知大小的可变参数元组