Ale*_* B. 5 c++ variadic-templates
I want to write a template class InvTuple which defines type as a tuple of class arguments in inverse order. So it should work like
InvTuple<T1, T2, T3, ...>::type ---> tuple<..., T3, T2, T1>
Run Code Online (Sandbox Code Playgroud)
I defined it like so
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 < > ;
};
Run Code Online (Sandbox Code Playgroud)
But this does not compile due to the error as shown in the code. Please help me to understand what's wrong.
You need the template keyword:
using doInvert = typename InvTuple<T...>::template doInvert < U..., T1 > ;
Run Code Online (Sandbox Code Playgroud)
and you also need to switch U... and T1 in the same line to have that work properly:
#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>
}
Run Code Online (Sandbox Code Playgroud)