c++ - How can I use a macro for collecting variable names? -


i simplify following

class {     int a;     int b;     int c;     std::vector<int*> addrs; public:     a() : addrs{ &a, &b, &c } {} }; 

so don't have write list of fields in 2 places, i.e. declarations , initializer addrs. there way use macro collect declarations , use them later. e.g.,

class {     var_decl(a);     var_decl(b);     var_decl(c);     std::vector<int*> addrs; public:     a() : addrs{ var_addresses } {} }; 

for context intended implementing kind of property introspection system.

you using boost preprocessor.

#define variables (a)(b)(c)  #define declare_member(mar, matype, maid) \   matype maid;  #define take_address(mar, maunused, maindex, maid) \   boost_pp_comma_if(maindex) & maid  class {   boost_pp_seq_for_each(declare_member, int, variables)   std::vector<int*> addrs; public:   a() : addrs { boost_pp_seq_for_each_i(take_address, %%, variables) } {} };  // can clean up: #undef declare_member #undef take_address // in case no longer need list, also: #undef variables 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -