C + + std::min and Std::max such as:
float_t f (float_t x) const {return Std::max (float_t) 0.0, x);}
However, the following code is used:
#include "windows.h"
#include <algorithm>
void foo () {
int i = 5;
int j = 7;
int x = Std::max (i,j);
}
Compiling in VS can cause the following error:
1>test.cpp (7): Error C2589: ' (': illegal token on right side of ':: '
1>test.cpp (7): Error C2143:syntax Error : Missing '; ' Before ':: '
This is because the max and Min macros are also defined in Windows.h, and when you include Windows.h, the program cannot be compiled.
Solution:
1. Windows.h uses a different name, only for Windows systems.
int x = _cpp_max (i,j);
int y = _cpp_min (i,j);
This method changes on the system library and is not recommended.
2. Do not use the Std::max () and Std::min () functions in the system library and replace them with other alternatives such as:
int x = i > J? I:j; Max (i,j)
int y = i < J i:j;//min (i,j)
Simply re-implement these functions, and if you have large amounts of code, you can write a different name function yourself.
3. Add the namespace using namespace Std, or as follows:
Using Std::min;
Using Std::max;
int x = max (i,j);
int y = min (i,j);
This method experiment is not good, I do not know whether I understand wrong, welcome to discuss.
4. Use std::min<int> and std::max<int> as
int x = std::max<int> (i,j);
int y = std::min<int> (i,j);
This method requires prior knowledge of the input data type, but there are problems with different types I and J, such as:
int i = 5;
unsigned int j = 7;
int x = (Std::max) (i,j);
int y = (std::min) (I,J);
The following error occurs:
1>test.cpp (7): Error C2780: ' Const _ty &std::max (const _ty &,const _ty) ':
&,_PR 3 expects- 2 provided
1> c:program filesmicrosoft Visual Studio 8vcincludexutility (3190): "*
declaration of ' std: : Max '
1>test.cpp (7): Error C2782: ' Const _ty &std::max (const _ty &,const _ty &) ':
template Parame Ter ' _ty ' is ambiguous
1> c:program filesmicrosoft Visual Studio 8vcincludexutility (3182):
Declaration of ' Std::max '
1> could be ' unsigned int '
1> or ' int '
Summary: Recommended method 2, implement a function of the same function.