2

I develop small reflection library for studying purpose. I want to traverse all fields of some C++ class. There is guarantee, that this class implement reflect function. The full example of such class is:

struct some_class
{
    char field1;
    bool field2;

    some_class()
        : field1(0)
        , field2(0)
    {
    }
};

template<class proc>
void reflect(proc& p, some_class& sc)
{
    reflect(p, sc.field1, "field1");
    reflect(p, sc.field2, "field2");
}

I want to traverse  all the fields of this class and perform some action for each of field. For example, I may print field or serialize it.

I think I need to implement Visitor pattern. I also think that the Generic lambdas (С++ 14) help me too.

auto lambda = [](auto x, auto y) {return x + y;};

I do not know how to solve my task. I look forward to any advice.

Note, that the signature of reflect function is immutable. I cannot change it.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
David
  • 674
  • 6
  • 19
  • @nestedloop, no, It does not. The signature of `reflect function` is const. I can not change it. – David Apr 19 '15 at 20:32
  • @Barry, why you decide, that my question is duplicate? – David Apr 19 '15 at 20:57
  • Because you want to know how "to traverse all fields of some C++ class" -- which is exactly what that question asked and has answers for? – Barry Apr 19 '15 at 20:59
  • @Barry, but my case is other. I cannot use `REFLECTABLE macro` from that example. – David Apr 19 '15 at 21:01

0 Answers0