By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

difference bet. macro and inline

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

<sa********@yahoo.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr 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
Jul 22 '05 #2

P: n/a

"Gernot Frisch" <Me@Privacy.net> wrote in message news:33*************@individual.net...

<sa********@yahoo.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr 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

Jul 22 '05 #3

P: n/a
BB
sa********@yahoo.com 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.
Jul 22 '05 #4

P: n/a
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.
Jul 22 '05 #5

P: n/a
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
Jul 22 '05 #6

P: n/a
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>

Jul 22 '05 #7

P: n/a
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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.