Void-pointer, variant-objects and any-objects are amazing because they can store many different types in the same variable. But I have a problem with them, I need to specify their type (creating and/or de-referencing them) in the execution time, is it possible?
To be more clear, for example, as far I know, to create and de-reference them I have to do this:
void* ptr = new int(8);
variant<int, float> var = 8;
any a = 8;
...
cout << *(int*)ptr;
cout << get<int>(var);
cout << any_cast<int>(a);
As you can see, in all cases the type must be "written in the code" (programming-time?). It would be excellent if that type could be determinate in the execution time. Let me dream... if, for example, we could "store a type" as an object, we could do this:
TYPE mi_tipo;
...
mi_tipo = int; // or float, or char or any other...
...
void* ptr = new mi_tipo();
cout << *(mi_tipo*)ptr;
Something like this is possible? If not, how would you do this? I'm trying to do simulate a compiler perform, so I need an structure which can store any type of variables, and so I need an efficient way of de-reference them in execution time.