min max
Hello,
I have :-
#define NOMINMAX
string::size_type maxlen = 10;
maxlen = max(maxlen, 14);
I've read that VC++ does not support min max and has _cpp_max instead. However, I'm not sure how to use it. Also, does it take the same parameters as the max above (not int) ?
Thanks,
Imanuel.
[354 byte] By [
ami] at [2007-11-11 10:06:30]

# 4 Re: min max
min_element and max_element search for max and min elements in range defined by two two iterators and optionaly predicate function. VC++ 6.0, however defines max and min as macros. As a quick fix, you could add your own definitions of min and max as function templates.
Here is possible implementation:
template<class CMPTYPE> inline
const CMPTYPE& max(const CMPTYPE& _Left, const CMPTYPE& _Right)
{ // return larger of _Left and _Right
return (_Left < _Right ? _Right : _Left);
}
// TEMPLATE FUNCTION max WITH PRED
template<class CMPTYPE, class PREDFUN> inline
const CMPTYPE& max(const CMPTYPE& _Left, const CMPTYPE& _Right, PREDFUN _Pred)
{ // return larger of _Left and _Right using _Pred
return (_Pred(_Left,_Right) ? _Right : _Left);
}
// TEMPLATE FUNCTION min
template<class CMPTYPE> inline
const CMPTYPE& min(const CMPTYPE& _Left, const CMPTYPE& _Right)
{ // return smaller of _Left and _Right
return (_Right < _Left ? _Right : _Left);
}
// TEMPLATE FUNCTION min WITH PRED
template<class CMPTYPE, class PREDFUN> inline
const CMPTYPE& min(const CMPTYPE& _Left, const CMPTYPE& _Right, PREDFUN _Pred)
{ // return smaller of _Left and _Right using _Pred
return (_Pred(_Right, Left) ? _Right : _Left);
}
Add this snippet in <algorithm> header, but make sure that you add it between _STD_BEGIN and _STD_END defines. When calling this code, always use 'std::max' or 'std::min' instead of plain 'max' and 'min' to avoid any possible ambiguity.
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string::size_type maxlen = 10;
maxlen = std::max<int>(maxlen, 14);
cout << maxlen << endl;
return 0;
}
In your example there could be one little problem, but if you are acustomed to templates, the solution will appear obvious. The thing is you use two different types: 'maxlen' and '14' are typed as 'unsigned int' and 'int' respectively (at least with my version of vc++6.0). So what happens when you try to compile your original code?
string::size_type maxlen = 10;
maxlen = max(maxlen, 14);
Template instatation mechanism will find that your max template has one template parameter, which is CMPTYPE. But you are providing two different types (int and unsigned int). When dealing with templates, compiler omits standard conversions, as it usually does when compiling "normal" functions. So you need to provide explicitly type that all arguments will be converted to. You do it like this:
max<int>( maxlen, 14 );
Now you should only add std:: qualification to your calls, and that's about it. For example:
cout << std::max<int>( maxlen, 14 ) << endl;
If you are calling 'std::min' and 'std::max' with arguments that are exactly the same type, then you simply omit argument type, like this:
cout << std::max( -3, 14 ) << endl;
Hope this helps.