how can I create a list of types by appending them in a struct

I have a very simplified code that shows my intentions:

1
2
3
4
5
6
7
8
9
10
11
struct IncreasingCollection
{
	using Collection = typename Loki::Typelist < Loki::NullType, Loki::NullType >;

	template<typename T>
	static void append()
	{
		using list = typename Loki::TL::Append<Collection, T>::Result;
		// how to export this new collection from this struct??
	}
};


I want to be able to add several types and then have access to the complete collection...

Any ideas?

Regards,
Juan
Last edited on
using Alexandrescu's Loki meta library I can come up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct FKDependents
{
	static std::tuple<A, tblDefs...> tblDefs_list;

	using connections = Loki::Typelist<Loki::NullType, Loki::NullType>;

	template<int N>
	static constexpr int find()
	{
		// pick one TableDef
		using tableDef = K::type;

		if ( !std::is_same_v<TableDefInTuple<tableDef, decltype(tableDef::reference_list)>::type, void>)		// match found
		{
			using Loki::TL::Append<connections, typename tableDef::Target>::Result;
		}
	}


how can I modify connections? I can't! how do I offer the illusion of modifying connections? Only by adding another struct...

How can I modify connections? I can't! how do I offer the illusion of modifying connections? Only by adding another struct...

You can't, it's fundamentally impossible. You'd have to introduce another type.

Maybe a workaround exists, but it depends on what you are trying to do.
What is the purpose of the program you are writing?
Last edited on
Hi,

I have an array with positions indicating the indexes into a tuple of types or -1 if the type isn't found,

position[0] = 1 the position 1 of tblDefs[0] is a type
position[1] = 1 the position 1 of tblDefs[1] is a type

I need a tuple containing the 2 types indicated by the position array...

Say tblDefs[0] = tuple<I,R> and tblDefs[1] = tuple<T,Q>, so I need a tuple like so:
tuple<R,Q>

If on the other hand the position array was like so:

position[0] = -1
position[1] = 0

then I would need a tuple containing only one type:
tuple<T>

The main problem is how to create these type tuples...


Regards,
Juan

The notation tblDefs[x] is short for std::tuple_element_t<x, tblDefs>



The code is like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
template<typename T, typename...tblDefs >
struct FKDependents
{
	inline static std::array<int, sizeof...(tblDefs)> m_positions;
	using tableKey = T;
	static std::tuple<tblDefs...> tblDefs_list;

	static constexpr int findInTblDefs_list()
	{
		static constexpr int size = std::tuple_size_v<decltype(tblDefs_list)>;
		tableKey* pT;
		return find<size>();
	}

	using Collection = Loki::Typelist<Loki::NullType, Loki::NullType>;

private:
	template<int N>
	static constexpr int find()
	{
		// pick one TableDef
		using tableDef = std::tuple_element_t<N-1, decltype(tblDefs_list)>;
		using tableKeys = decltype(tableDef::reference_list);

                // find if tableKey is in tableKeys tuple:
		int index = TableKeyInTuple < tableKey, tableKeys>::index();
		if( index != -1)
		{
			// append tableKey to type collection
                        // THIS IS THE PROBLEM!! SOLVING THIS I WOULDN'T NEED THE ARRAY
		}

		m_positions[N-1] = index; // save the indexes in the array
		return find<N - 1>();
	}
	template<>
	static constexpr int find<0>()    // return how many types in the final collection...
	{
		int count = 0;
		for(int i : m_positions)
		{
			if (i != -1)
				++count;
		}
		return count;
	}
};


I have a very simplified code that shows my intentions:

1
2
3
4
5
6
7
8
9
10
11
struct IncreasingCollection
{
	using Collection = typename Loki::Typelist < Loki::NullType, Loki::NullType >;

	template<typename T>
	static void append()
	{
		using list = typename Loki::TL::Append<Collection, T>::Result;
		// how to export this new collection from this struct??
	}
};


I want to be able to add several types and then have access to the complete collection...

Any ideas?

Regards,
Juan
I want to be able to add several types and then have access to the complete collection

Template meta-programming is a purely-functional exercise. There are no side effects and all types are immutable. It is not possible to assign the local type list to IncreasingCollection::Collection.

You can make the new type (list) accessible, but it can never be incorporated into a type that is already defined.

From your prior post it sounds like you are given some coordinates (i, positions[i]) that describe an element in a data structure (tblDefs) and want to retrieve the values of the data structure at those coordinates. This is certainly possible, and it has nothing to do with appending stuff to a type list.

In general, if you want to do imperative programming at compile time, use consteval or constexpr. Or forget templates etc, and just write a code generator.
Last edited on
Topic archived. No new replies allowed.