macros - Defining functions using backslash in c++ -
i saw people using backslash when define functions in macros as:
#define classnamenodebug(typenamestring) \ static const char* typename_() { return typenamestring; } \ static const ::foam::word typename
i did easy test. got bunch of errors. testing code follows: in testmacro.h file:
#define declearlarger(first,second) \ double whichislarger(double first,double second){ return (first>second) ? fisrt : second;}
in main() function:
int second =2; int first =1; cout << declearlarger(first,second) << endl;
the errors are:
/home/jerry/desktop/backslash/backslash_test/testmacro.h:7: error: expected primary-expression before 'double' double whichislarger(double first,double second){ return (first>second) ? fisrt : second;} ^ /home/jerry/desktop/backslash/backslash_test/testmacro.h:7: error: expected ';' before 'double' double whichislarger(double first,double second){ return (first>second) ? fisrt : second;} ^ /home/jerry/desktop/backslash/backslash_test/main.cpp:24: error: expected primary-expression before '<<' token cout << declearlarger(first,second) << endl; ^
that concludes testing errors. can give suggestions why these errors pop up?
you're trying use function definition (generated macro) inside expression. c++ not allow such thing. instead define macro be
#define declearlarger(first,second) \ (((first)>(second)) ? (first) : (second))
and work. note none of errors come backslash, generated because of function definition/expression clash.
Comments
Post a Comment