473,563 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
2 5719
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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #2
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(MA X<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6975
by: foo | last post by:
I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the delete operator with a macro. I can't replace the delete operator by using void* as the first parameter, because then my code will not be able to...
27
5898
by: TheDD | last post by:
Hello all, right now, i'm using the following macro to automatically add informations to exceptions: #define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args) but AFAIK, it's gcc specific. Is there a way to do it in a standard c++ way? TIA
7
1957
by: sachin_mzn | last post by:
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
11
22330
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the type specified. This is much similar like a macro expansion in C. can anyone please explain advantages of one over the other ? Thanks in...
19
7902
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
9
2027
by: jedi200581 | last post by:
Hello, I have a singleton class that I'm using through macros: //snippet #define DO_SOMETHING ( myArgument) \ { \ MyClass *myObject = MyClass::getInstance(); \ myObject->doSomething(myArgument); \ }; \
12
3219
by: swellfr | last post by:
Hi if have simple macro defined this way: #define M_OPplus( classname ) typedef classname operator+(const classname& rhs); and a template class : template<typename X, typename Y> class Test1 {
2
2316
by: Christof Warlich | last post by:
Hi macro experts, in a variadic macro, i.e. in a macro with a variable parameter list, is there any way to access single parameters of the list? __VA_ARGS__ only expands to the whole list. The reason why I need this: I want a fixed Index->Value mapping that should be part of a compile time API. The straight forward way to do this would be...
2
2809
by: Alex Vinokur | last post by:
Hi, Here is my program -------------- Program -------------- #define ADD(x,t) (x + t) template <typename T, int N> int foo() {
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7945
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.