[Developers] macro as macro argument
Tom Goodale
goodale at cct.lsu.edu
Tue Mar 6 10:26:17 CST 2007
Hi David,
if you want the argument expanded, just do
#define foo1(arg1, XX) foo(arg1, XX)
#define foo(arg1, XX) { \
int internal_var##XX; \
... code(arg1) ... }
and call foo1 with
foo1(bar, __LINE__)
as XX will get expanded in the call to the lower-level macro.
Cheers,
Tom
On Tue, 6 Mar 2007, David Rideout wrote:
> Hi Erik,
>
> Thanks for your reply. I realize now that I did not explain the function of
> the macro sufficiently. Indeed the code I posted does not generate compiler
> warnings.
>
> This macro is meant to 'set up' a loop, so in a sense it has a final 'implied
> argument' which is a block of code enclosed in {}. The warning comes when
> one uses this for a nested loop:
>
> /home/rideout/Cactus/configs/causets/build/Playground/Distance.c:94: warning:
> declaration of 'SETS_word_index' shadows a previous local
> /home/rideout/Cactus/configs/causets/build/Playground/Distance.c:94: warning:
> shadowed declaration is here
>
> I sorted out a fix by reading appendix A12 of Kernighan & Richie carefully.
> It explains that macro arguments to ## are not expanded.
>
> I realize that this loop macro could be replaced with something like the
> following in the calling code:
>
> while (inline_function_with_static_vars(arg)) { ... user code ...}
>
> Perhaps this is cleaner? (Or maybe replacing
> inline_function_with_static_vars(arg) with a macro which passes around the
> internal variables as necessary.)
>
> Thanks again,
> David
>
> On Monday 05 March 2007 16:34, Erik Schnetter wrote:
>> On Mar 5, 2007, at 10:06:49, David Rideout wrote:
>>> I have a C macro which employs internal variables, e.g.
>>>
>>> #define foo(arg1) { \
>>> int internal_var; \
>>> ... code(arg1) ... }
>>>
>>> Often the macro is invoked multiple times in a single source file,
>>> which leads
>>> to a compiler warning about identical variables in scope or
>>> something like
>>> this. I tried a hack:
>>>
>>> #define foo(arg1, XX) { \
>>> int internal_var##XX; \
>>> ... code(arg1) ... }
>>>
>>> which I wanted to call with
>>>
>>> foo(bar, __LINE__)
>>>
>>> to give each internal variable a unique name, but this instead
>>> makes each
>>> internal variable into 'internal_var__LINE__'.
>>>
>>> Can someone more familiar with the workings of the C preprocessor
>>> suggest a
>>> way to avoid this compiler warning?
>>
>> As shown, your code should not warn about identical variables in
>> scope, since each macro closes its scope. Do you have maybe other
>> variables as well which have the same name?
>>
>> Could you rewrite your code using inline functions, or maybe C++
>> templates? I often find this much cleaner than macros, and also much
>> easier to debug. You can use templates in C++ and have the remainder
>> of your programme still in C style, if you prefer that.
>>
>> If you want to continue with your existing approach, then the usual
>> remedy is to add a bit of indirection by defining some additional
>> macros with arguments. See <http://www.decompile.com/cpp/faq/
>> file_and_line_error_string.htm> for an example; this doesn't do
>> exactly what you want, but it handles __LINE__ and encounters a very
>> similar error.
>>
>> -erik
> _______________________________________________
> Developers mailing list
> Developers at cactuscode.org
> http://www.cactuscode.org/mailman/listinfo/developers
>
More information about the Developers
mailing list