Reputation: 25
As per the title, this is what I am looking to do. Basically I am looking to load in structures from files, but support every kind of structure, so I am attempting to do it in a template. This is my first time using templates really so excuse my ignorance!
I want to be able to do something like:
template<class T> T ConfigLoader::LoadStructFromFile(T a)
{
int noOfThingsInStruct;
noOfThingsInStruct = a[1];
return a;
}
Is this at all possible? My function does sorting of the string loaded in from files etc but thought I would leave that part out. I want to be able to get this value to use it to loop and give the struct the correct number of values it is looking for.
Upvotes: 0
Views: 116
Reputation: 4012
Simple answer: impossible.
Long answer: still no.
Detour:
numberOfElements<typename T>
and overload it for every struct
you need with a value you expect. Then, use it in your LoadStructFromFile
, since you know T
.Upvotes: 0
Reputation: 28178
So you want to dynamically figure out what members and methods are in a struct? Similar to, say, what you can do in Javascript in runtime, but in compile time? No, you can't. However, you can make a template policy and base this function on that.
Upvotes: 1