-
Minimum Macro
I am trying setup a simple macro that compares 2 numbers and selects the lowest number.
I found the following example from someelse on the C++ board.
#define MINIMUM1 (a, b) (((a) < (b)) ? (a) : (b))
I am confused how I execute the macro from main. I have tried
Code:
double lowestNumber;
lowestNumber = MINIMUM1(10, 5);
cout << "Lowest number = " << lowestNumber;
but that didn't even compile. I received the this error, "term does not evaluate to a function."
Any help would be appreciated.
Thanks,
-
Code:
#define MINIMUM1(a, b) (((a) < (b)) ? (a) : (b))
^^^^ no space here
-
>#define MINIMUM1 (a, b) (((a) < (b)) ? (a) : (b))
Note the space between the macro name and the argument list. This shouldn't be there:
Code:
#define MINIMUM1(a, b) (((a) < (b)) ? (a) : (b))
Of course, in C++ you would be better served with an inline template function:
Code:
template <typename T>
T minimum1(T a, T b) {
return (a < b) ? a : b;
}
-
Thanks - I took the extra space out but am still getting the same compiler error.
Any suggestions?
-
The macro must appear in the same file, either directly or in an #include, before any use. "does not evaluate to a function" most likely means that the compiler sees symbol(whatever) before symbol is introduced. All the cool kids use std::min anyway. What happens with MINIMUM1(*p1++,*p2++);?
-
>What happens with MINIMUM1(*p1++,*p2++);?
You get what you deserve. :D
-
Actually I was thinking about this, it does run twice as fast;) We also have a very good chance of "thinking outside the box"
edit: The above was written by marketing people, the actual speed improvement is only 50%