Reputation: 368
I want to define template
template<template<typename...> class TT, size_t n, class T, class... U>
struct insert;
which inserts type T
on the n
-th place in between arguments U...
inside template TT
. So it should work in such a way that e.g. insert<std::tuple, 2, char, int,int,int,int>
expands into std::tuple<int,int, char, int,int>
, i.e. char
is inserted right in the middle of the tuple arguments.
Implementation is following
template<template<typename...> class TT, size_t n, class T, class U1, class... U>
struct insert<TT, n, T, U1, U...>
{
template<class...V>
using templ =
/* template<typename...> */
typename
insert
<
typename insert<TT, n - 1, T, U...>::templ,
0,
U1
>::templ < V... > ; // <-- error C2988: unrecognizable template
// declaration/definition
using type = typename templ < > ;
};
template<template<typename...> class TT, class T, class... U>
struct insert < TT, 0, T, U... >
{
template<class...V>
using templ = TT<T, U..., V...>;
using type = typename templ < > ;
};
But compilation fails. Thanks for help.
Upvotes: 0
Views: 99
Reputation: 119877
template<template<typename...> class TT, class T, size_t n, class... U>
struct insert;
template<template<typename...> class TT, class T, class U1, class... U>
struct insert <TT, T, 0, U1, U...>
{
using type = TT<T, U1, U...>;
};
template<template<typename...> class TT, class T>
struct insert <TT, T, 0>
{
using type = TT<T>;
};
template<template<typename...> class TT, class T, size_t n, class U1, class... U>
struct insert<TT, T, n, U1, U...>
{
template <typename... X> using templ = TT<U1, X...>;
using type = typename insert<templ, T, n-1, U...>::type;
};
test:
template <typename t1, typename t2, typename t3, typename t4>
struct test {};
using result = insert<test, int, 2, double, char, void*>::type;
int main()
{
std::string s = typeid(result).name();
int status;
s = abi::__cxa_demangle(s.c_str(), NULL, NULL, &status);
std::cout << s << std::endl;
}
Demo.
Upvotes: 1