Dynamically create a list of types in C ++

I am looking for a way to dynamically create a list of types at compile time. I create components that have different types, and under the hood I would like to build a list of types that contains all types registered as components. Imagine the following:

using Name = Component<std::string>; using Position = Component<Vec2f>; 

I would like for me to get a list of types that contains std::string and Vec2f , in which case I can access and expand, that is, something like the following:

 using AllComponents = SomeMagicTypeThatHoldsAllComponents; 

I am already creating part of the type list and it works something like this:

 using List = typename MakeTypeList<int, double, std::string>::List; 

You can add or add a type to a List as follows:

 using Append = typename AppendType<List, float>::List; using Prepend = typename PrependType<List, Vec2f>::List; 

And get types by index like this (which basically allows you to extend types with std::integer_sequence ):

 using FirstType = typename TypeAt<List, 0>::Type; 

The problem I can’t wrap my head around is how to dynamically build this list in the background without breaking the simple component API . Does anyone have any suggestions if and how this can be achieved?

EDIT: For Clarify, the only solution I could suggest is to pass a list of types as an optional argument to the template, i.e.

 using AllTypes = using MakeTypeList<>::List; using Name = Component<AllTypes, std::string>; using Position = Component<AllTypes, Vec2f>; 

I am interested in a solution in which I do not need to pass a list of types.

Thanks!

+5
source share

Source: https://habr.com/ru/post/1273583/


All Articles