[openbeos] Re: Coding Suggestion -- Templates, Iterators

>#define foreach(elem,set) for (elem = set.begin (); elem != set.end 
(); 
>++elem)
>
>Oh, I know, I can hear the groaning out there... "we don't use #define 
>macros -- that's old-style, icky C crap that we sophisticated C++ 
>programmers don't use". Maybe, but I still think macros are extremely 
>useful when used sparingly and judiciously. They're awfully good at 
>hiding uglies.

I hope im not repeating something someones already said in this thread, 
but if you really want to use code like this (to hide uglies, etc, as 
you say), then abandon the #define and use an inline'd function. The C+
+ compiler is smart enough to do exactly that (inline it) exactly as 
the #define would, but you get all the extra bonuses (like type 
checking, etc).
Essentially, using inline functions is the C++ replacement for #defines 
(just like "const int MY_CONSTANT = 1000;" *should* be used in place of 
"#define MY_CONSTANT 1000") although if you dont care about type 
checking, both methods have the same end result.

-Jim

Other related posts: