C++ 倒元组参数
我想编写一个模板类C++ 倒元组参数,c++,variadic-templates,C++,Variadic Templates,我想编写一个模板类InvTuple,它将type定义为类参数的一个倒序元组。所以它应该像这样工作 InvTuple<T1, T2, T3, ...>::type ---> tuple<..., T3, T2, T1> InvTuple::type-->tuple 我是这样定义的 template<class...T> struct InvTuple; template<class T1, class...T> struct In
InvTuple
,它将type
定义为类参数的一个倒序元组。所以它应该像这样工作
InvTuple<T1, T2, T3, ...>::type ---> tuple<..., T3, T2, T1>
InvTuple::type-->tuple
我是这样定义的
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 < > ;
};
模板
结构InvTuple;
模板
结构InvTuple
{
模板
使用doInvert=typename InvTuple::doInvert;
// ;
};
但是,由于代码中显示的错误,这不会编译。请帮助我了解问题所在。您需要:
using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ;
使用doInvert=typename InvTuple::template doInvert;
你在中间缺少了
你需要模板关键词:
using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ;
哇!首先,我甚至不知道,如果将
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>
}