I have a map loading function that takes input using ifstream from a file and creates objects from them.
Here is what the file might look like:
tree=50,100,"assets/tree.png"
box=10,10,"assets/box.png"
Which should create the objects tree and box and pass the values to their constructor. I already have the value part figured out, but I don't know how to take the string "tree" and create a tree object.
Is it possible to take a string(or c string) and use it as a type name?
Things I've tried:
Passing the string as a template typename
#include <string> struct A {}; template<typename T> T* createType() { T* type = new T() return T; } int main() { std::string tname = "A"; auto* type = createType<tname>; }
Using the using keyword
#include <string> template<std::string T> struct someType {} struct A {}; struct B {}; using someType<"A"> = A; using someType<"B"> = B; int main() { std::string tname1 = "A"; std::string tname2 = "B"; someType<tname1> typeA; someType<tname2> typeB; }
Problems:
I can't seem to find a clear answer about this but is seems like there are some problems with using a string as a template parameter.
I don't know if it is ok to pass a variable as a template parameter
I don't think that you can cast template types (from string to typename)
Is there any way that either of these, or some other way might work to accomplish this? Or is this just not possible?