Re: Cannot use global scope "max" function even ::max is used.
Originally Posted by lucky6969b
Alrite Victor, changed it, but it didn't work, but I've worked around it by #defin'ing it in my header file.
Thanks
Jack
The problem is that the Windows header files defines min() and max() as macros. Attempting to mix them with the C++ standard std::min() and std::max() becomes problematic.
There are two solutions:
1) If your usage of min() and max() are in a CPP file (not a header file), and that source file extensively uses std::min/max, making changes risky, then you should do this:
Code:
#include (all of your Windows related headers)
#undef min
#undef max
#include <algorithm>
//...
// Your C++ source code
This undefined the macros, includes the C++ header that defines std::min/max. This effectively removes Visual C++'s version from the source module.
2) If you're using std::min/max in a header file, then you can't really use solution 1), since it may mess up other compilation units that may require the macro version of min() and max(). The workaround is to create your own macro/function called mymin()/mymax() (or similar named function), and use that in the header file.
Re: Cannot use global scope "max" function even ::max is used.
Originally Posted by lucky6969b
Alrite Victor, changed it, but it didn't work, but I've worked around it by #defin'ing it in my header file.
Thanks
Jack
Do not write any macro named min() and max() in a header file. Not only is there no need to, but it is very risky to do so. You're risking having modules not compile correctly.
Instead, work with what exists. Either you #undef the existing macros in a CPP source file and use std::min()/max(), or create brand new functions named differently than min() and max().
Bookmarks