[fpc-pascal] Hack for C-like macro functions — will it work in future?

Florian Klämpfl florian at freepascal.org
Wed Sep 24 22:26:07 CEST 2014


Am 24.09.2014 um 02:59 schrieb Роман:
> As we all know, C preprocessor allows function-like macros, including ones with empty expansion.
> They can be very convenient for things that you'll want to disable depending on conditional defines,
> such as logging or assertions.
> 
> #ifdef DEBUG
>     #define dprf(...) print_debug_message(__VA_ARGS__)
> #else
>     #define dprf(...)
> #endif
> 
> I have a temptation to use this in FPC too. Here's a trick:
> 
> {$macro on}
> {$ifdef DEBUG}
>     {$define Log := ActualLog}
> {$else}
>     {$define Log := ;//}
> {$endif}
> 
> if A then
>   Log('will work until followed by ELSE at all or any statements on the same line (because of ; and
> //, respectively).');
> if B then
>   Log('And should not be carried over to the next line, of course!');
> 
> Because constant {$ifdef DEBUG} ActualLog(...); {$endif} are rather cumbersome.
> 
> This is by design? Can I rely on this behavior in the future? ._.

As far as the example above is concerned, yes. Macros just do a textual replacement.




More information about the fpc-pascal mailing list