Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
How can I write a macro which takes a variable number of arguments ....
How can I write a macro which takes a variable number of arguments, or use the preprocessor to ``turn off'' a function call with a variable number of arguments?
✍: Guest
One popular trick is to define and invoke the macro with a single, parenthesized ``argument'' which in the macro expansion becomes the entire argument list, parentheses and all, for a function such as printf:
#define DEBUG(args) (printf("DEBUG: "), printf args)
if(n != 0) DEBUG(("n is %d\n", n));
The obvious disadvantage is that the caller must always remember to use the extra parentheses. Another problem is that the macro expansion cannot insert any additional arguments (that is, DEBUG() couldn't expand to something like fprintf(debugfd, ...)).
gcc has an extension which allows a function-like macro to accept a variable number of arguments, but it's not standard. Other possible solutions are:
* Use different macros (DEBUG1, DEBUG2, etc.) depending on the number of arguments.
* Play games with commas:
#define DEBUG(args) (printf("DEBUG: "), printf(args))
#define _ ,
DEBUG("i = %d" _ i);
* Play horrendous games with mismatched parentheses:
#define DEBUG fprintf(stderr,
DEBUG "%d", x);
(These all require care on the part of the user, and all of them are rather ugly.)
C99 introduces formal support for function-like macros with variable-length argument lists. The notation ... can appear at the end of the macro ``prototype'' (just as it does for varargs functions), and the pseudomacro __VA_ARGS__ in the macro definition is replaced by the variable arguments during invocation.
Finally, you can always use a bona-fide function, which can take a variable number of arguments in a well-defined way. (If you needed a macro replacement, try using a function plus a non-function-like macro, e.g. #define printf myprintf .)
When you want to turn the debugging printouts off, you can either use a different version of your debug macro:
#define DEBUG(args) /* empty */
or, if you're using real function calls, use still more preprocessor tricks to remove the function name but not the arguments, such as
#define DEBUG (void)
or
#define DEBUG if(1) {} else printf
or
#define DEBUG 1 ? 0 : (void)
(These tricks are predicated on the assumption that a good optimizer will remove any ``dead'' printf calls or degenerate cast-to-void parenthesized comma expressions.)
2016-01-19, 954👍, 0💬
Popular Posts:
How do you estimate maintenance project and change requests?
What is the value of this expression? +1-2*3/4 -0.5
How To Export Your Connection Information to a File? - Oracle DBA FAQ - Introduction to Oracle SQL D...
How To Define a Sub Function? - Oracle DBA FAQ - Creating Your Own PL/SQL Procedures and Functions A...
If we have two version of same assembly in GAC how do we make a choice ? OK first let’s try to under...