I just Read #define macros use instead of tiny functions...How i rewrite the following tiny fn:
by a #define macro
Plz comment....Code:static long Multiply(long m,long n)
{
return ( ((long long) m) * ((long long) n) ) >> 32;
}
Printable View
I just Read #define macros use instead of tiny functions...How i rewrite the following tiny fn:
by a #define macro
Plz comment....Code:static long Multiply(long m,long n)
{
return ( ((long long) m) * ((long long) n) ) >> 32;
}
Read Stroustrup's answer to the FAQ: So, what's wrong with using macros?
Also see this FAQ: http://www.codeguru.com/forum/showthread.php?t=328273.
The worst part with macros is that being expanded at pre-compile time they are not helpful at all during debugging.
A demonstration of the perils of macro 'functions'
Try compiling this...
Then try this...Code:#define Multiply(a, b) ((a) * (b))
struct Test
{
int Multiply(int a)
{
return (a * 2);
}
};
int main()
{
Test test;
int result = test.Multiply(3);
return 0;
}
If you really want macro style flexibility...Code:int Multiply(int a, int b)
{
return (a * b);
}
struct Test
{
int Multiply(int a)
{
return (a * 2);
}
};
int main()
{
Test test;
int result = test.Multiply(3);
return 0;
}
Code:template <typename TReturn, typename T1, typename T2>
TReturn Multiply(T1 a, T2 b)
{
return static_cast<TReturn>(a * b);
}