[Developers] macro as macro argument

Erik Schnetter schnetter at cct.lsu.edu
Tue Mar 6 11:35:32 CST 2007


David,

I would not express a multi-dimensional loop as a single while loop.   
I tried this, and it confuses the compiler optimiser.

The C++ Boost libraries (www.boost.org) have support for this kind of  
looping, built on templates.  I haven't used it myself, but I've  
talked to people, and it seems to be a very elegant solution.   
Hartmut Kaiser, one of the CCT scientists, is contributing to Boost;  
maybe he can point to a particular library that would be useful for you?

-erik

On Mar 6, 2007, at 09:17:27, 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
>


-- 
Erik Schnetter <schnetter at cct.lsu.edu>

My email is as private as my paper mail.  I therefore support encrypting
and signing email messages.  Get my PGP key from www.keyserver.net.



-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://www.cactuscode.org/pipermail/developers/attachments/20070306/cc584f10/attachment.bin 


More information about the Developers mailing list