to create a class that takes any types and puts them together by storing the next pointer and so on. I then realized i have to convert them to void * to be able to do so. Now, if any object is requestes by the user, he has to provide the index AND its type to convert back from void*.
how can I get it to only have to pass the index and have boudList figure out the type itself and return the correct type?
Ive seen that std::tuble is capable of doing so, so there must be a possibility?!
Ok, thank you keskiverto.
I never asked for types not to be kown in runime.
Instead i asked myself how std::tuple sets the exact type of instatiation at comp-time and wanted to realize that as well
tuple, like vector and others, are simply using c++ templates at compile time.
templates, under the hood, actually create the type at compile time.
that is, if you say vector<my_weird_thing> to get a vector of some class you made, at compile time the compiler effectively crafts something like this:
class vector
{
my_weird_thing * ptr; //a place holder here is swapped for the real thing
//more stuff
};
and compiles the new class as if it were created for your type all along.
This is a massive oversimplification, but if you want to know more, you will need to look at how an actual c++ compiler handles templates or a paper about that topic. I honestly don't know much more than this simple description of the process, but I am sure it is recursive (you can have templates of templates of templates...) and likely a bit complicated down in the details.
Thank you very much jonnin.
I will have a closer look at that.
Is there no option to store a type information in sth like a variable that i can later cast void * to get the type back?
However, what you are doing is really sort of fighting the design of C++. C++ is a very strongly typed language, and you are trying to weaken/break that. Note that void*s are powerful but crude, low level C tools that as you have seen do not play nice with some advanced c++. They are awesome for things like thread functions, where the parameters are sent to the generic tool as a void pointer and passed along back to your actual thread function, where... YOU KNOW THE TYPE .. and you simply cast it back to what it was. Nothing in between tried to USE it, its just passed along and ignored until it gets back somewhere that you know what it was. It was never meant to be used to lose the type and then recover it back without any help.