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

difference between a C++ macro MAX and the C++ template

P: n/a
Can someone explain me the difference between the following C++ macro
and the function MAX:

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

template <class T>
const T& MAX(const T& a, const T& b)
{
return (a > b ? a : b);
}

Some of the differences I beleive are that the macro is expanded
inline by the preprocessor, unlike the fucntion MAX? Any other
differences? Can the two basically be used interchangably, or would
one be used over the other for certain applications?

Thanks in advance.

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


P: n/a
Aakash Jain <a.****@iu-bremen.de> spoke thus:
Some of the differences I beleive are that the macro is expanded
inline by the preprocessor, unlike the fucntion MAX? Any other
differences? Can the two basically be used interchangably, or would
one be used over the other for certain applications?


Thought exercise:

int i=0, j=2;
MAX(i++,j++);

What's the difference?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #2

P: n/a
On 13 Apr 2004 10:50:58 -0700 in comp.lang.c++, a.****@iu-bremen.de
(Aakash Jain) wrote,
Can someone explain me the difference between the following C++ macro
and the function MAX:

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

template <class T>
const T& MAX(const T& a, const T& b)
{
return (a > b ? a : b);
}


1. Pass MAX as a function argument to another function, e.g.
// make all numbers non-negative
std::transform(v.begin(), v.end(), v.begin(),
std::bind2nd(MAX<double>, 0.0));

2. Define a class with a private member function that happens
to be named MAX.

3. Try to solve the problems with (1) and (2) by making MAX
a member of a namespace.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.