Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Alternatives to #define?

Reply
Thread Tools

Alternatives to #define?

 
 
Robbie Hatley
Guest
Posts: n/a
 
      07-04-2004
"Mark A. Gibbs" <(E-Mail Removed)_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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-04-2004
* Rolf Magnus:
>
> #define DO_STUFF do { doThis(); doThat(); } while(false)


Unfortunately that may cause a warning with too "helpful" compilers
such as Visual C++.

Try instead

#define DO_STUFF for(;{ doThis; doThat(); break; }

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-04-2004
"Robbie Hatley" <lonewolfintj at pacbell dot net> wrote:

>> 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



If at all, I'd write the macro as:

#define DO_STUFF do { doThis(); doThat(); } while(false)

so that it can be used like a normal function in most cases. But I see
no benefit over an inline function, which would be simpler.

>> ... 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.


Of course not. It would make huge amounts of legacy code useless.
However, that doesn't mean that you should still use macros where an
inline function could be used.

> 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


I fail to see why you consider this to be more simple than:

inline void 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.


That's the point. You have to remember, and take are. You don't have to
do that with inline functions.

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-04-2004

> #define DO_STUFF do { doThis(); doThat(); } while(false)



How would that be in any way different to:


#define DO_STUFF { doThis(); doThat(); }


?


-JKop
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-04-2004
JKop wrote:

>
>> #define DO_STUFF do { doThis(); doThat(); } while(false)


I meant:

#define DO_STUFF() do { doThis(); doThat(); } while(false)

>
>
> How would that be in any way different to:
>
>
> #define DO_STUFF { doThis(); doThat(); }
>
>
> ?


if (x) DO_STUFF(); else whatever();

This won't compile with the latter one.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-04-2004
Alf P. Steinbach wrote:

> * Rolf Magnus:
>>
>> #define DO_STUFF do { doThis(); doThat(); } while(false)

>
> Unfortunately that may cause a warning with too "helpful" compilers
> such as Visual C++.
>
> Try instead
>
> #define DO_STUFF for(;{ doThis; doThat(); break; }


Hmm, what would you gain with that for loop?

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-04-2004
* Rolf Magnus:
> Alf P. Steinbach wrote:
>
> > * Rolf Magnus:
> >>
> >> #define DO_STUFF do { doThis(); doThat(); } while(false)

> >
> > Unfortunately that may cause a warning with too "helpful" compilers
> > such as Visual C++.
> >
> > Try instead
> >
> > #define DO_STUFF for(;{ doThis; doThat(); break; }

>
> Hmm, what would you gain with that for loop?


See above.

And right, as you mention elsewhere, there should be empty arg list for
the macro.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      07-04-2004
Alf P. Steinbach wrote in news:(E-Mail Removed) in
comp.lang.c++:

> * Rolf Magnus:
>>
>> #define DO_STUFF do { doThis(); doThat(); } while(false)

>
> Unfortunately that may cause a warning with too "helpful" compilers
> such as Visual C++.


Yep VC++ has some distinctly un-useful warnings on by default.
I turn them off.

>
> Try instead
>
> #define DO_STUFF for(;{ doThis; doThat(); break; }
>


if DO_STUFF;
else doThat();

The advantage of the "do .. while(false)" is it allows the
trailing semicolon, your version might as well be:

#define DO_STUFF { doThis; doThat(); }

Though in this case, I'd prefer:

#define DO_STUFF() ( doThis(), doThat() )

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-04-2004
* Rob Williscroft:
> Alf P. Steinbach wrote in news:(E-Mail Removed) in
> comp.lang.c++:
>
> > * Rolf Magnus:
> >>
> >> #define DO_STUFF do { doThis(); doThat(); } while(false)

> >
> > Unfortunately that may cause a warning with too "helpful" compilers
> > such as Visual C++.

>
> Yep VC++ has some distinctly un-useful warnings on by default.
> I turn them off.
>
> >
> > Try instead
> >
> > #define DO_STUFF for(;{ doThis; doThat(); break; }
> >

>
> if DO_STUFF;
> else doThat();
>
> The advantage of the "do .. while(false)" is it allows the
> trailing semicolon, your version might as well be:
>
> #define DO_STUFF { doThis; doThat(); }
>
> Though in this case, I'd prefer:
>
> #define DO_STUFF() ( doThis(), doThat() )


Right, sorry.

I've always used the do-while version, for exactly that reason,
and even preaching it to others.

Smartass, Alf. Smartass.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-04-2004
Alf P. Steinbach wrote:

> * Rolf Magnus:
>> Alf P. Steinbach wrote:
>>
>> > * Rolf Magnus:
>> >>
>> >> #define DO_STUFF do { doThis(); doThat(); } while(false)
>> >
>> > Unfortunately that may cause a warning with too "helpful" compilers
>> > such as Visual C++.
>> >
>> > Try instead
>> >
>> > #define DO_STUFF for(;{ doThis; doThat(); break; }

>>
>> Hmm, what would you gain with that for loop?

>
> See above.


No, I mean what you gain compared to just:

#define DO_STUFF() { doThis(); doThat(); }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BGP alternatives? SP Cisco 3 09-08-2009 02:32 AM
LEAP & ACS Alternatives N. Hall Cisco 2 05-28-2005 08:31 AM
MS Press 2003 books and alternatives Bill Bixby MCSE 7 04-29-2004 05:52 PM
alternatives to accessing PIX via Telnet Anne Robynn Cisco 3 01-03-2004 08:48 AM
Alternatives when user's browser doesn't accept cookies Robert V. Hanson ASP .Net 2 07-03-2003 03:24 AM



Advertisments