How to deduce the template parameters of a vector?

Hi,

I have a variadic class vector that contains types. I want to access its elements in a metaprogram. The only way I have succeeded is to add methods to the vector class template as shown below, but I would like to receive the vector in another metafunction deducing the type elements it contains. How can I "export" or make available the template arguments the vector contains?

As of now, this is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<typename...TypeElements>
struct vector
{
	template<size_t N>
	struct get_at
	{
            using type = get_at_t<N, TypeElements...>;
	};
	template<typename Elem>
	struct push_back
	{
		using type = vector<TypeElements..., Elem>;
	};
	template<typename Elem>
	struct push_front
	{
		using type = vector<Elem, TypeElements...>;
	};
};


I would like to do something like this:

1
2
3
4
5
6
7
8
9
template<typename Elem>
struct push_back
{
	template<template<typename...ExistingElements> class Class>
	struct apply
	{
		using type = vector<Elem, ExistingElements...>;
	};
};


where I would call it like so:

1
2
using MyVector = vector<int, unsigned, char>;
using res = push_back<long double>::apply<MyVector>::type;


so that the compiler could deduce the template arguments of MyVector class.
Of course this does not compile.
How can I access the elements of a vector from outside the vector?

Regards,
Juan Dent
I think I found the solution: Access to each element of the type can be achieved by a recursive search by position as in this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//////////////////
// get_by_pos

template<size_t N, typename>
struct get_by_pos
{
	using type = nil_t;
};

template<size_t N, template<typename...> typename Seq, typename Head, typename...Tail>
struct get_by_pos<N, Seq<Head, Tail...>>
{
	using type = typename get_by_pos<N - 1, Seq<Tail...>>::type;
};

template<template<typename...> typename Seq, typename Head, typename...Tail>
struct get_by_pos<0, Seq<Head, Tail...>>
{
	using type = Head;
};
using aVector = vector<int, char>;
using type_1 = get_by_pos<1, aVector>::type;



Yet this is O(N) where N is the number of template instantiations created, is there a faster algorithm?

Thanks,
Juan Dent
> is there a faster algorithm?

Several ideas are explored here: http://ldionne.com/2015/11/29/efficient-parameter-pack-indexing/
Topic archived. No new replies allowed.