Wednesday, 28 August 2013

How can I use a macro for collecting variable names?

How can I use a macro for collecting variable names?

I would like to simplify the following
class A {
int a;
int b;
int c;
std::vector<int*> addrs;
public:
A() : addrs{ &a, &b, &c } {}
};
so that I don't have the write the list of fields in two places, i.e. the
declarations and the initializer for addrs. Is there some way to use a
macro to collect the declarations and use them later. E.g.,
class A {
VAR_DECL(a);
VAR_DECL(b);
VAR_DECL(c);
std::vector<int*> addrs;
public:
A() : addrs{ VAR_ADDRESSES } {}
};
For context this is intended for implementing some kind of property
introspection system.

No comments:

Post a Comment