Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Alternatives to #define?

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-04-2004
* Rolf Magnus:
> 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(); }
>


Uhm, see my reply to Rob Williscroft. I was wrong. And the extreme
irony is that I've always used do-while for such things precisely
because a simple block won't do wrt. semicolon syntax, and done my best
to teach that technique to others (since you can read Norwegian as well
as C++ code I can offer an example of such teaching, namely the XASSERT
macro in <url: http://home.no.net/dubjai/03/index.html#cpputil>,
presumably some Googling on newsgroup posting would give more examples).

--
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
 
 
 
 
Carl Ribbegaardh
Guest
Posts: n/a
 
      07-05-2004
"Mark A. Gibbs" <(E-Mail Removed)_x> wrote in message
news:rqGFc.96$(E-Mail Removed) gers.com...
>
> Carl Ribbegaardh wrote:
>
> > Very enlightening post. I think that you might have mixed up who thought
> > what about macros and inline functions, but it really doesn't matter.
> > This was exactly the info about alternatives to macros I was looking

for.
>
> thank you, and don't worry, i was aware i was replying two steps removed
> when i was harping about the dangers of macros. sorry if i gave the
> impression otherwise.
>
> mark
>


One more question on the subject:

Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented as
macros?
...and other stuff that's common like SAFE_DELETE and so on...

Is it just done by habit, or are there any other reason for not using inline
functions?


/Carl


 
Reply With Quote
 
 
 
 
Mike Smith
Guest
Posts: n/a
 
      07-05-2004
Robbie Hatley wrote:

> "Carl Ribbegaardh" <(E-Mail Removed)> wrote:
>
>
>>#define DO_STUFF doThis(); doThat();

>
>
> I actually like that. Simple, direct, ALWAYS inlined.


It's dangerous, though. What if you have code like:

if (condition)
DO_STUFF
else
do_something_else();

This won't expand properly. You should write the macro as:

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

but IMO using an inlined function would be better.

--
Mike Smith
 
Reply With Quote
 
Jay Nabonne
Guest
Posts: n/a
 
      07-07-2004
On Mon, 05 Jul 2004 12:13:55 +0200, Carl Ribbegaardh wrote:

> "Mark A. Gibbs" <(E-Mail Removed)_x> wrote in message
> news:rqGFc.96$(E-Mail Removed) gers.com...
>>

>
> One more question on the subject:
>
> Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented as
> macros?
> ..and other stuff that's common like SAFE_DELETE and so on...
>
> Is it just done by habit, or are there any other reason for not using inline
> functions?
>


The standard windows headers can be used in either C or C++ programs. And
inline functions are a C++-only nicety...

- Jay

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-08-2004
Jay Nabonne wrote:

> On Mon, 05 Jul 2004 12:13:55 +0200, Carl Ribbegaardh wrote:
>
>> "Mark A. Gibbs" <(E-Mail Removed)_x> wrote in message
>> news:rqGFc.96$(E-Mail Removed) gers.com...
>>>

>>
>> One more question on the subject:
>>
>> Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented
>> as macros?
>> ..and other stuff that's common like SAFE_DELETE and so on...
>>
>> Is it just done by habit, or are there any other reason for not using
>> inline functions?
>>

>
> The standard windows headers can be used in either C or C++ programs.
> And inline functions are a C++-only nicety...


That's not true. ISO C has been knowing the inline keyword for 5 years
now, and many C compilers have been supporting it for a much longer
time. The windows headers have never been ISO compliant anyway, so they
could have used inline, too.

 
Reply With Quote
 
Jay Nabonne
Guest
Posts: n/a
 
      07-08-2004
On Thu, 08 Jul 2004 11:12:18 +0200, Rolf Magnus wrote:

> Jay Nabonne wrote:
>
>> On Mon, 05 Jul 2004 12:13:55 +0200, Carl Ribbegaardh wrote:
>>
>>> "Mark A. Gibbs" <(E-Mail Removed)_x> wrote in message
>>> news:rqGFc.96$(E-Mail Removed) gers.com...
>>>>
>>>
>>> One more question on the subject:
>>>
>>> Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented
>>> as macros?
>>> ..and other stuff that's common like SAFE_DELETE and so on...
>>>
>>> Is it just done by habit, or are there any other reason for not using
>>> inline functions?
>>>

>>
>> The standard windows headers can be used in either C or C++ programs.
>> And inline functions are a C++-only nicety...

>
> That's not true. ISO C has been knowing the inline keyword for 5 years
> now, and many C compilers have been supporting it for a much longer
> time. The windows headers have never been ISO compliant anyway, so they
> could have used inline, too.


I had a feeling someone would object to that.

How about this replacement for my last line then: at the time that the
Windows header files were developed (> 5 years ago), C compilers (in
particular the Microsoft C compiler) didn't support inline functions.

Does that sit well enough?

- Jay

 
Reply With Quote
 
Carl Ribbegaardh
Guest
Posts: n/a
 
      07-10-2004
"Jay Nabonne" <(E-Mail Removed)> wrote in message
newsan.2004.07.08.16.08.32.32000@rightagainBYTEM E.com...
> On Thu, 08 Jul 2004 11:12:18 +0200, Rolf Magnus wrote:
>
> > Jay Nabonne wrote:
> >
> >> On Mon, 05 Jul 2004 12:13:55 +0200, Carl Ribbegaardh wrote:
> >>
> >>> "Mark A. Gibbs" <(E-Mail Removed)_x> wrote in message
> >>> news:rqGFc.96$(E-Mail Removed) gers.com...
> >>>>
> >>>
> >>> One more question on the subject:
> >>>
> >>> Why is (for example) SUCCEEDED and FAILED (in winerror.h) implemented
> >>> as macros?
> >>> ..and other stuff that's common like SAFE_DELETE and so on...
> >>>
> >>> Is it just done by habit, or are there any other reason for not using
> >>> inline functions?
> >>>
> >>
> >> The standard windows headers can be used in either C or C++ programs.
> >> And inline functions are a C++-only nicety...

> >
> > That's not true. ISO C has been knowing the inline keyword for 5 years
> > now, and many C compilers have been supporting it for a much longer
> > time. The windows headers have never been ISO compliant anyway, so they
> > could have used inline, too.

>
> I had a feeling someone would object to that.
>
> How about this replacement for my last line then: at the time that the
> Windows header files were developed (> 5 years ago), C compilers (in
> particular the Microsoft C compiler) didn't support inline functions.
>
> Does that sit well enough?
>
> - Jay


Now I understand better why the API's looks like they do. Thanks a lot!

/Carl


 
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