"Grey Plastic" <>
> I'm looking for a way to declare variables inside for statements (or
> perhaps some other statement) and have the following statement execute
> exactly once. For example,
>
> for(Type var=blah; 1; )
>
> would be what I wanted, if it ran only once, instead of forever.
[snip]
> I want something like
>
> #define CUSTOM_FOR(x,y,var) \
> for(int x=0; x<256; x++) \
> for(int y=0; y<256; y++) \
> for(Type var = blah; runOnce; )
>
> so that I can have code like
>
> CUSTOM_FOR(i,j,whee)
> foo(whee,j);
What you really need is a functor - have a look at
http://www.codeproject.com/cpp/TTLFunction.asp ...
But if you really want to call a macro with some arbitrary code as an
argument, just put it in brackets (braces ?). All the preprocessor cares
about is that there is a closing bracket, not what is inside ... Someone
here wrote some code like this (please excuse C-style output in a C++ news
group) -
#define DEBUG_PRINTF(x) printf x
DEBUG_PRINTF(("%d %d\n", x, y));
which expands to printf("%d %d\n", x, y);
David Fisher
Sydney, Australia