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>
staticvoid 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...
template<typename T, typename...tblDefs >
struct FKDependents
{
inlinestatic std::array<int, sizeof...(tblDefs)> m_positions;
using tableKey = T;
static std::tuple<tblDefs...> tblDefs_list;
staticconstexprint findInTblDefs_list()
{
staticconstexprint 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>
staticconstexprint 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<>
staticconstexprint 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>
staticvoid 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...
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.