c++ - Macro overloading -


is possible define this:

#define foo(x, y) bar() #define foo(x, sth, y) bar(sth) 

so this:

foo("daf", sfdas); foo("fdsfs", something, 5); 

is translated this:

bar(); bar(something); 

?

edit: actually, bar's methods of class. sorry not saying before (didn't think relevant).

answering dyp's question:

class testy {     public:     void testfunction(std::string one, std::string two, std::string three)     {         std::cout << 1 << 2 << three;     }     void anotherone(std::string one)     {         std::cout << one;     }     void anotherone(void)     {         std::cout << "";     } };  #define pp_narg(...) pp_narg_(__va_args__,pp_rseq_n()) #define pp_narg_(...) pp_arg_n(__va_args__) #define pp_arg_n(_1, _2, _3, n, ...) n #define pp_rseq_n() 3, 2, 1, 0  // macro 2 arguments #define foo_2(_1, _2) anotherone() // macro 3 arguments #define foo_3(_1, _2, _3) anotherone(_2)  // macro selection number of arguments #define foo_(n) foo_##n #define foo_eval(n) foo_(n) #define testfunction(...) foo_eval(pp_narg(__va_args__))(__va_args__) 

and call:

testy testy; testy.testfunction("one", "two", "three");   // line 9 

compiler output:

warning 1 warning c4003: not enough actual parameters macro 'pp_arg_n' main.cpp 9

warning 2 warning c4003: not enough actual parameters macro 'foo_' main.cpp 9

error 3 error c2039: 'foo_' : not member of 'testy' main.cpp 9

edit ------------------------------------------------look here----------------------------------------------------------------->

(overloading macro on number of arguments)

// functions, or macros, .... void bar(){} void bar(int){}  #define expand(x) x    // msvc10 compatibility  // compute number of (variadic) macro arguments // http://groups.google.com/group/comp.std.c/browse_thread/thread/77ee8c8f92e4a3fb/346fc464319b1ee5?pli=1 #define pp_narg(...) expand( pp_narg_(__va_args__, pp_rseq_n()) ) #define pp_narg_(...) expand( pp_arg_n(__va_args__) ) #define pp_arg_n(_1, _2, _3, n, ...) n #define pp_rseq_n() 3, 2, 1, 0   // macro 2 arguments #define foo_2(_1, _2) bar() // macro 3 arguments #define foo_3(_1, _2, _3) bar(_2)  // macro selection number of arguments #define foo_(n) foo_##n #define foo_eval(n) foo_(n) #define foo(...) expand( foo_eval(expand( pp_narg(__va_args__) ))(__va_args__) )  int main() {     int = 42;     foo("daf", sfdas);     foo("fdsfs", something, 5); } 

preprocessor output:

void bar(){} void bar(int){}  int main() {     int = 42;     bar();     bar(something); } 

edit2: seems vs2010 has issues __va_args__ , macro replacement.

update: it's ... bug?? (also see this question):

#define macro2(param0, param1, ...) arg 0: >param0<    arg 1: >param1<    \   additional args: >__va_args__< #define macro1(...) macro2(__va_args__, otherarg_0, otherarg_1)  macro1(arg0, arg1); 

preprocessor output:

arg 0: >arg0, arg1<    arg 1: >otherarg_0<    additional args: >otherarg_1<; 

for workaround, see linked question. i've updated original answer (code) above , tested msvc10 -> works now.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -