0

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.

  • Side note: wouldn't `things_count` be more clear than `noOfThingsInStruct`? I see a lot of people who think long names are more descriptive, so it's better. No, they just make you blind. – Shahbaz May 20 '14 at 16:27
  • Try Boost serialization. It is a good idea to have used something before designing it. E.g. you wouldn't want to be a passenger in a plane designed by someone who had never been in a plane. – Cheers and hth. - Alf May 20 '14 at 16:29
  • Yes probably, however it was just quickly to post up a question. – user3375989 May 20 '14 at 16:29
  • @Cheersandhth.-Alf I am programming this to specifically avoid using boost :). – user3375989 May 20 '14 at 16:30
  • OK, but (I still recommend that you) try it first. – Cheers and hth. - Alf May 20 '14 at 16:31
  • Yeah, I should have tried it in more detail, I simply wrote a[1] earlier and it didn't throw an error, So I assumed it was legal, however I did not check which value it held nor if I could make something equal it. My bad on that part! – user3375989 May 20 '14 at 16:35

2 Answers2

1

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.

David
  • 27,652
  • 18
  • 89
  • 138
  • Sorry, I do not quite understand what you mean by that? As in the second part. – user3375989 May 20 '14 at 16:32
  • I wouldn't be so sure. Theoretically for a concrete instantiation, you know sizes of all fields. I would not be surprised if some variadic templates or boost magic was able to do figure out what fields of what types are there. The problem might be with the fact, that compiler can reorganize order of members and memory layout. – luk32 May 20 '14 at 16:34
0

Simple answer: impossible.

Long answer: still no.

Detour:

  • you can use something based on type traits. Create a template class 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.
  • You can also use SFINAE to test for some function that'll return the number of elements in a struct. If a given class/struct implements it, just use it to get the number of members. If not - assume there's only 1 member (or whatever you wish).
Community
  • 1
  • 1
Paweł Stawarz
  • 3,952
  • 2
  • 17
  • 26
  • Looks like I am gonna have to redesign how I do it then, minus the template. I not only need access to the first element, but then each one after that in order to put the value from the text file into it. Thanks for the answer. – user3375989 May 20 '14 at 16:41