dimanche 27 août 2017

C++11 - get all class variables in compile time and call method for them without Boost

I know, that in C++, there is no reflection. However, I need to iterate variables in compile time and generate code for them.

What I have is this:

class Foo {
   int x;
   double y;

   Foo() {
     Register(x, "x");
     Register(y, "y");
   }

   template <typename T>
   void Register(T val, const std::string & key){
      //do something with val and key
   }
}

However, I would like to call Register automatically for each variable. Sometimes, there could be a lot of them and its easy to forgot to register something or make typo in key (key should be ideally same as variable name).

I have thought of something like CREATE_AND_REGISTER(x, "x") macro, but I dont know if it is possible. I have seen some Boost based solutions, but I need just plain C+11/14.

Aucun commentaire:

Enregistrer un commentaire