Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > difference bet. macro and inline

Reply
Thread Tools

difference bet. macro and inline

 
 
sachin_mzn@yahoo.com
Guest
Posts: n/a
 
      12-28-2004
Hi,

It may be a silly question but I want to know
the difference between #define macro and inline functions
Is there any performance issue related to it.

-Sachin

 
Reply With Quote
 
 
 
 
Gernot Frisch
Guest
Posts: n/a
 
      12-28-2004

<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> Hi,
>
> It may be a silly question but I want to know
> the difference between #define macro and inline functions
> Is there any performance issue related to it.
>
> -Sachin
>


#define max(a,b) ((a>b) ? (a) : (b))

inline template<class T> T max(const T& t1, const T& t2)
{
return t1>t2 ? t1 : t2;
}

will produce the same results on good compilers.


--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com


 
Reply With Quote
 
 
 
 
Mole Wang
Guest
Posts: n/a
 
      12-28-2004

"Gernot Frisch" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>
> <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed) oups.com...
> > Hi,
> >
> > It may be a silly question but I want to know
> > the difference between #define macro and inline functions
> > Is there any performance issue related to it.
> >
> > -Sachin
> >

>
> #define max(a,b) ((a>b) ? (a) : (b))
>
> inline template<class T> T max(const T& t1, const T& t2)
> {
> return t1>t2 ? t1 : t2;
> }
>
> will produce the same results on good compilers.


Really? Consider the following code:
int i = 3, j = 2;
int res = max(++i, j);
The inline function and macro result in different return value "res".

>
>
> --
> -Gernot
> int main(int argc, char** argv) {printf
> ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
>
> ________________________________________
> Looking for a good game? Do it yourself!
> GLBasic - you can do
> www.GLBasic.com
>
>



 
Reply With Quote
 
BB
Guest
Posts: n/a
 
      12-28-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> It may be a silly question but I want to know
> the difference between #define macro and inline functions
> Is there any performance issue related to it.
>
> -Sachin
>


This is addressed in the FAQ.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-28-2004
Gernot Frisch wrote:

> #define max(a,b) ((a>b) ? (a) : (b))
>
> inline template<class T> T max(const T& t1, const T& t2)
> {
> return t1>t2 ? t1 : t2;
> }
>
> will produce the same results on good compilers.
>
>


No it will NOT.

max(++a, ++b)

will yield different results.
 
Reply With Quote
 
Niels Dekker - no reply address
Guest
Posts: n/a
 
      12-29-2004
Gernot Frisch wrote:
> #define max(a,b) ((a>b) ? (a) : (b))


Two more parentheses, please:
#define max(a,b) ((a)>(b) ? (a) : (b))

(Try calling max(1, 1 & 2) to spot the difference!)

> inline template<class T> T max(const T& t1, const T& t2)
> {
> return t1>t2 ? t1 : t2;
> }
>
> will produce the same results on good compilers.


Ron Natalie replied:
>
> No it will NOT.
>
> max(++a, ++b)
>
> will yield different results.



max(a, b) = 0;

will yield different results as well. Apparently, the macro approach is
the better one, in this case!

Scott Meyers wrote about the implementation of max(a, b), in his article
"min, max, and more": <quote> I increasingly find myself telling people
that the macro approach may well be best, and I hate macros. </quote>

Source: http://www.aristeia.com/Papers/C++Re...umns/jan95.pdf


Kind regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-29-2004
Niels Dekker - no reply address wrote:

> max(a, b) = 0;


add:
inline template<class T> T& max(T& t1, T& t2)
{
return t1 > t2 ? t1 : t2;
}

>
> will yield different results as well. Apparently, the macro approach is
> the better one, in this case!
>
> Scott Meyers wrote about the implementation of max(a, b), in his article
> "min, max, and more": <quote> I increasingly find myself telling people
> that the macro approach may well be best, and I hate macros. </quote>
>

 
Reply With Quote
 
Niels Dekker - no reply address
Guest
Posts: n/a
 
      12-29-2004
Gernot Frisch wrote:
> inline template<class T> T max(const T& t1, const T& t2)
> {
> return t1>t2 ? t1 : t2;
> }


To support assignment [max(a, b) = 0], Ron Natalie wrote:
>
> add:
> inline template<class T> T& max(T& t1, T& t2)
> {
> return t1 > t2 ? t1 : t2;
> }


The Scott Meyers article "min, max, and more" (1995) said that this
would still lead to troubles when mixing const and non-const arguments:

void g(const BigNumber& n1)
{
BigNumber n2 = 22;
BigNumber n3 = max(n1, n2); // call which max?...
}

(From http://www.aristeia.com/Papers/C++Re...umns/jan95.pdf again)
But all of the compilers I just tried accept the code. So may I assume
that this issue has been solved by a revision of the C++ language?


Kind regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
 
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
about extern inline and static inline Sean C++ 4 04-30-2006 03:18 PM
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
To inline or not to inline? Alvin C++ 7 05-06-2005 03:04 PM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments