difference bet. macro and inline

S

sachin_mzn

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
 
G

Gernot Frisch

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
 
M

Mole Wang

Gernot Frisch said:
#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".
 
B

BB

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

Ron Natalie

Gernot said:
#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.
 
N

Niels Dekker - no reply address

Gernot said:
#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++ReportColumns/jan95.pdf


Kind regards,

Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
 
N

Niels Dekker - no reply address

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

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++ReportColumns/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top