如果有装boost的话,可以这样做,gcc相容boost.preprocessor
#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/iteration/local.hpp>
//这里放每个成员的名称,此例子为3个成员
#define t (aaa,bbb,ccc)
//#的展开规则,所以得分两行写
#define str(s) _str(s)
#define _str(s) #s
//BOOST_PP_LOCAL_MACRO是每一轮的展开,n递增
//这里为函数本体,BOOST_PP_TUPLE_ELEM从上面的t抓token出来,从第一个抓起
#define BOOST_PP_LOCAL_MACRO(n) int BOOST_PP_CAT(get_,BOOST_PP_TUPLE_ELEM(3,n,t))(struct obj_s *self){\
int err_code = checkByMode(self,\
BOOST_PP_CAT(&_common_update_attr_,BOOST_PP_TUPLE_ELEM(3,n,t)),\
str(BOOST_PP_CAT(common_get_,BOOST_PP_TUPLE_ELEM(3,n,t))));\
if (err_code < 0){\
return err_code;\
}\
return (int)self-> BOOST_PP_TUPLE_ELEM(3,n,t);\
}
//表示BOOST_PP_LOCAL_MACRO为0->1->2的展开,即3个成员
#define BOOST_PP_LOCAL_LIMITS (0,2)
//实际展开
#include BOOST_PP_LOCAL_ITERATE()