Invert tuple arguments

I want to write an InvTuple template class that defines type as a tuple of class arguments in reverse order. Therefore, it should work like

 InvTuple<T1, T2, T3, ...>::type ---> tuple<..., T3, T2, T1> 

I defined it like this:

 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 < > ; }; 

But this does not compile due to an error, as shown in the code. Please help me understand what is wrong.

+5
source share
2 answers

You will need the template keyword:

 using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ; 

and you also need to switch U... and T1 on the same line so that they work correctly:

 #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> } 

Example

+5
source

You need the following:

 using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ; 

The keyword template missing in the middle.

+1
source

Source: https://habr.com/ru/post/1204322/


All Articles