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 7 1957
<sa********@yah oo.com> schrieb im Newsbeitrag
news:11******** **************@ z14g2000cwz.goo glegroups.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%c gl%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
"Gernot Frisch" <Me@Privacy.net > wrote in message news:33******** *****@individua l.net... <sa********@yah oo.com> schrieb im Newsbeitrag news:11******** **************@ z14g2000cwz.goo glegroups.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%c gl%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
sa********@yaho o.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.
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.
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
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>
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Aakash Jain |
last post by:
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);
}
|
by: ethan |
last post by:
Hi All,
How to write a macro with variable length of argument?
For example, if i need a macro to check return value of printf.
#define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end;
However, it doesn't work. >_<
Thank you very much.
|
by: Lindsay |
last post by:
I was just reading another post when someone commented that the code was C
and not C++. What are the differences? (Examples?) The answer does not
affect my programming but would help to know if I need to post a question.
|
by: junky_fellow |
last post by:
hi guys,
Can you please suggest that in what cases should a macro be
preferred over inline function and viceversa ? Is there any case where
using a macro will be more efficient as compared to inline function ?
thanks for any help in advance ...
|
by: bleyddyn.apRhys |
last post by:
I have a bit of code that doesn't compile under MSVC++ but does under
GCC. Even under GCC, though, it seems odd that I don't need a std
namespace qualifier for the exp function.
Is this just one of those difference I have to #ifdef around, or is
there a bit of code that will compile under both?
Basically, I have a std::vector<double>. I...
| |
by: aaragon |
last post by:
Hi everyone,
I would like to create a very simple function:
// header file
inline void point_map() {
PointMap pointMap = get(vertex_point_t(), g);
}
// main.cpp
|
by: MAx |
last post by:
Hi,
Could any one list possible number of diferences between a function
and a macro?
which one will be faster and why??
ex : function and a macro ti find max of two nums
#define MAX(a,b) (a>b) ? a:b
and
int max(int a,int b)
|
by: jason |
last post by:
Hi,
I learned my lesson about passing pointers, but now I have a question
about macros.
Why does the function work and the MACRO which is doing the same thing
on the surface, does not work in the following small example ?
#include <stdio.h>
|
by: sunnyalways4u2000 |
last post by:
hello sir,
Sir will please tell me the exact difference between
C and advanced C...what are the extra features or funcions...etc added
in this advanced one.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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. ...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |