Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inline functions

Reply
Thread Tools

inline functions

 
 
bjeremy
Guest
Posts: n/a
 
      12-21-2006

I apologize, you were right... the compiler I ran the test on was using
and old C version not a C++ and I got different results for some
reason..

 
Reply With Quote
 
 
 
 
bjeremy
Guest
Posts: n/a
 
      12-21-2006

Victor Bazarov wrote:
> bjeremy wrote:
> > Victor Bazarov wrote:
> >> bjeremy wrote:
> >>> [..]
> >>> 3. Expressions passed into macros are not always evaluated before
> >>> entering the macro body. i.e.
> >>>
> >>> #define MACRO (x) cout << "The value of x = " << x << endl;
> >>>
> >>> int main ()
> >>> {
> >>> int x = 10;
> >>> MACRO (x++)
> >>
> >> 'x++' subexpression is evaluated at some point *before* the full
> >> expression ('MACRO(x++)') is completely evaluated. What does "macro
> >> body" have to do with all this?
> >>
> >>>
> >>> return 42; //
> >>> }
> >>>
> >>> this would print out "The value of x = 10"
> >>
> >> Uh.. So? If the 'MACRO' would be a function, how different would
> >> the behaviour be?
> >>
> >> void MACRO(int x) { cout << "The value of x = " << x << endl; }
> >>
> >> V
> >> --
> >> Please remove capital 'A's when replying by e-mail
> >> I do not respond to top-posted replies, please don't ask

> >
> > The increment will happen after the std::cout statment. This example
> > shows that macros sometimes do not behave intuitively in the presence
> > of side effects. If 100 developers were reading the code I bet you
> > half of them would expect that that "The value of x = 11" be printed
> > out. This would not happen in an inline function since the expression
> > would be evaluated before entering the function.

>
> Exactly *what* would not happen? Half of them would not expect? 10
> would be printed? 11 would be printed? What's the difference in the
> side effects of the macro versus the inline function? What is the
> difference in the output? I have hard time seeing the point you're
> trying to make.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


I apologize, you're right... For some reason on one C compiler I got
different results from other compilers I tried... I've should have used
a better example like if the MACRO(x) uses x in a couple of dfferent
places but passes in MACRO(x++)

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-02-2007
bjeremy wrote:

>
> Victor Bazarov wrote:
>> bjeremy wrote:
>> > Victor Bazarov wrote:
>> >> bjeremy wrote:
>> >>> [..]
>> >>> 3. Expressions passed into macros are not always evaluated before
>> >>> entering the macro body. i.e.
>> >>>
>> >>> #define MACRO (x) cout << "The value of x = " << x << endl;
>> >>>
>> >>> int main ()
>> >>> {
>> >>> int x = 10;
>> >>> MACRO (x++)
>> >>
>> >> 'x++' subexpression is evaluated at some point *before* the full
>> >> expression ('MACRO(x++)') is completely evaluated. What does "macro
>> >> body" have to do with all this?
>> >>
>> >>>
>> >>> return 42; //
>> >>> }
>> >>>
>> >>> this would print out "The value of x = 10"
>> >>
>> >> Uh.. So? If the 'MACRO' would be a function, how different would
>> >> the behaviour be?
>> >>
>> >> void MACRO(int x) { cout << "The value of x = " << x << endl; }
>> >>
>> >> V
>> >> --
>> >> Please remove capital 'A's when replying by e-mail
>> >> I do not respond to top-posted replies, please don't ask
>> >
>> > The increment will happen after the std::cout statment. This example
>> > shows that macros sometimes do not behave intuitively in the presence
>> > of side effects. If 100 developers were reading the code I bet you
>> > half of them would expect that that "The value of x = 11" be printed
>> > out. This would not happen in an inline function since the expression
>> > would be evaluated before entering the function.

>>
>> Exactly *what* would not happen? Half of them would not expect? 10
>> would be printed? 11 would be printed? What's the difference in the
>> side effects of the macro versus the inline function? What is the
>> difference in the output? I have hard time seeing the point you're
>> trying to make.
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> I apologize, you're right... For some reason on one C compiler I got
> different results from other compilers I tried... I've should have used
> a better example like if the MACRO(x) uses x in a couple of dfferent
> places but passes in MACRO(x++)


As for unexpected results, this might be a better example:

#include <iostream>
#define MAX(a, b) ((a) < (b) ? (b) : (a))
int main()
{
int i = 6;
std::cout << MAX(--i, 5) << '\n';
}

 
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
Multiple functions (one version being inline and other beingnon-inline) Rahul C++ 3 02-28-2008 03:28 PM
converting inline functions to C functions jamihuq C Programming 7 05-17-2006 08:46 AM
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments