c++ - Writing a function using a macro with variable number of arguments -
how write macro variable number of arguments define function? suppose define class class1
2 parameters , class class2
3 parameters.
class class1 { public: int arg1; int arg2; class1(int x1, int x2): arg1(x1), arg2(x2) {} }; class class2 { public: int arg1; int arg2; int arg3; class1(int x1, int x2, int x3): arg1(x1), arg2(x2), arg3(x3) {} };
for each class define or classes have been defined before want write following:
template<> inline void writeinfo<class1>(const class1& obj, file* fp) { writeamount(2, fp); writename("arg1", fp); writeinfo(obj.arg1, fp); writename("arg2", fp); writeinfo(obj.arg2, fp); } template<> inline void writeinfo<class2>(const class2& obj, file* fp) { writeamount(3, fp); writename("arg1", fp); writeinfo(obj.arg1, fp); writename("arg2", fp); writeinfo(obj.arg2, fp); writename("arg3", fp); writeinfo(obj.arg3, fp); }
we not need care definitions of writeamount
, writename
or writeinfo
. write like:
macrowriteinfo(class1, 2, arg1, arg2); macrowriteinfo(class2, 3, arg1, arg2, arg3);
is possible create such macro can expand above template definitions? i've read in lot of places macros evil, in case believe helpful since they'll reduce amount of code type , amount of typos i'll make during creation of template functions.
first of should improve formatting/code. code lacks "class" keywords , semicolons after classes definitions - when post snippet make sure it's proper code, because people (i.e. me) try compile it.
second of all, dont use function template specialization. if macros evil, must satan incarnation. stick old overloads. see here details.
and @ least - answer. mess around variadic macros if all args of same type - example, create array inside writeinfo function , iterate on elements. since it's cleary not case here can define many variants of macrowriteinfo macro different number of parameteres, using common blocks reduce code repetition. example:
#define macrowriteinfo_begin(type, amount) \ void writeinfo(const type& obj, file* fp) \ { \ writeamount(amount, fp); #define macrowriteinfo_nameinfo(name) \ writename(#name, fp); \ writeinfo(obj.##name, fp); #define macrowriteinfo_end() \ }
using can define variants based on number of arguments.
#define macrowriteinfo1(type, arg1) \ macrowriteinfo_begin(type, 1) \ macrowriteinfo_nameinfo(arg1) \ macrowriteinfo_end() #define macrowriteinfo2(type, arg1, arg2) \ macrowriteinfo_begin(type, 2) \ macrowriteinfo_nameinfo(arg1) \ macrowriteinfo_nameinfo(arg2) \ macrowriteinfo_end()
and on...
edit: guess is possible use variadic macros here. take @ @ so question. it's pure madness, should able achieve want.
edit: idea expand variadic arguments array iterate on them; if of same type, let's int, write:
#define vaargssample(...) \ int args[] = { __va_args__ }; \ (int = 0; < sizeof(args)/sizeof(int); ++i) \ { \ printf("%d\n", args[i]); \ } vaargssample(1, 5, 666);
so if variables of same type put them in array. not, won't do. if really, want stick variadic arguments go first edit.
Comments
Post a Comment