Re: Alternatives to #define?
From: Robbie Hatley (lonewolfintj)
Date: 07/04/04
- Next message: Gwar: "Re: how to extract data in a textfile"
- Previous message: qazmlp: "comparator not called in std::map operations??"
- In reply to: Mark A. Gibbs: "Re: Alternatives to #define?"
- Next in thread: Rolf Magnus: "Re: Alternatives to #define?"
- Reply: Rolf Magnus: "Re: Alternatives to #define?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 3 Jul 2004 20:05:49 -0700
"Mark A. Gibbs" <x_gibbsmark@rogesr.com_x> wrote:
> just use this.
>
> >>>namespace MyUtils
> >>>{
> >>> inline void doStuff()
> >>> {
> >>> doThis();
> >>> doThat();
> >>> }
> >>>}
> >>
> >>That works, and most C++ purists would recommend it,
> >>but I think it sucks. Too complicated. KISS.
> >>Use the macro instead.
>
> this is probably the most bizarre thing i've read in a while.
Your reading must have been tame lately. :-) Try Anne Rice.
> it works, huh?:
>
> for (int i = 0; i < 10; ++i)
> STUFF
>
> vs.
>
> for (int i = 0; i < 10; ++i)
> doStuff();
>
> "kiss" that.
Well, that's technically correct, but you're losing
style points for no braces.
If you really needed to use such a macro in a
braceless loop like that, it COULD be done, though;
just make a slight correction to the macro definition:
#define DO_STUFF {doThis(); doThat();}
Then this will work:
for (int i = 0; i < 10; ++i)
DO_STUFF
> ... inline functions were largely created for the
> explicit purpose of eliminating macros ...
Well, for giving programmers an option that will be
better than macros in many cases, at least.
But I wouldn't say "eliminate". Note that the C and
C++ standards committes have not removed macros from
the languages, nor do I think that'll happen.
I agree inline functions are better than macros in
most cases; but if I want to simply paste-in a block
of code in several (or many) places in a program,
I'll sometimes use a macro:
#define PASTE_HUGE_CODE_BLOCK_HERE \
first line of code\
second line of code\
third line of code\
(and so on, for many more lines)\
last line of code
#define PASTE_TINY_CODE_BLOCK_HERE b=7;
int func1(double d)
{
...
PASTE_HUGE_CODE_BLOCK_HERE
...
}
float func2(int a, char b)
{
...
#ifdef FLAG_17
PASTE_HUGE_CODE_BLOCK_HERE
#else
PASTE_TINY_CODE_BLOCK_HERE
#endif
...
}
It's really just a matter of using the preprocessor
as an automated compile-time text editor. As long as
one remembers that macros are not functions, but just
cut-n-pastes, the concept works.
-- Cheers, Robbie Hatley Tustin, CA, USA email: lonewolfintj at pacbell dot net web: home dot pacbell dot net slant earnur slant
- Next message: Gwar: "Re: how to extract data in a textfile"
- Previous message: qazmlp: "comparator not called in std::map operations??"
- In reply to: Mark A. Gibbs: "Re: Alternatives to #define?"
- Next in thread: Rolf Magnus: "Re: Alternatives to #define?"
- Reply: Rolf Magnus: "Re: Alternatives to #define?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|