0

I'm boost newbie.

I would like to know is it possible

When I add some class member variable(at header file) then automatically generate some code

// classA.h file
class classA
{
    public:
        int a; // in fact a,b,c is some structure.
        int b;
        // I will add "int c;"

        void save(); // I want to auto generate some code at save()
        void load();
}

When I add "int c;"

// classA.cpp
void classA::save()
{
    someStream << a << b; // I use boost::serialize

    // I want auto replace above code by next
    // someStream << a << b << c;
}

void classA::load()
{
    someStream >> a >> b;

    // replace above 
    // someStream >> a >> b >> c;
    // Exactly same order
}
enter code here

It is possible? using boost mpl? macro?

I have variable to add a lot.

AnaHumid
  • 19
  • 7
  • no. maybe... with some crazy macro. It is worth it? No. Just write by hand. If you have a lot of members, consider refactoring (e.g. hold a vector) – bolov Jun 25 '15 at 11:15
  • 2
    there are some proposal to add compile-time reflection to c++, but I wouldn't count on it for the near (?) future. – bolov Jun 25 '15 at 11:17

1 Answers1

0

The closest you can get is std::tuple<int,int> data which you can then grow to std::tuple<int,int,int>, See here how to print them.

Community
  • 1
  • 1
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 1
    Actually, Boost Fusion has stuff much closer http://www.boost.org/doc/libs/1_58_0/libs/fusion/doc/html/fusion/adapted/define_struct.html – sehe Jun 25 '15 at 12:54