2011년 9월 10일 토요일

Variadic Macros

A macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of a function. Here is an example:


#define eprintf(...) fprintf (stderr, __VA_ARGS__)



eprintf ("%s:%d: ", input_file, lineno)
          ==>  fprintf (stderr, "%s:%d: ", input_file, lineno)


좋은 꼼수
#define FORMAT(format, ...) [NSString stringWithFormat:(format), ##__VA_ARGS__]

...

[self logInfo:FORMAT(@"Echo server started on port %hu", [listenSocket localPort])];

댓글 없음:

댓글 쓰기