我想编写一个模板类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 < > ;
};
但是由于代码中显示的错误,这不能编译。请帮我理解错误。
答案 0 :(得分:5)
您需要模板关键字:
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>
}
答案 1 :(得分:1)
你需要这个:
using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ;
您错过了中间的template
关键字。