#define NOMINMAX using std::min/max

后端 未结 4 1039
春和景丽
春和景丽 2020-11-27 05:39

i recently added:

#define NOMINMAX
#include 
#include 

to my main.cpp in order to use

st         


        
相关标签:
4条回答
  • 2020-11-27 06:17

    It's likely that your problem is that you #define NOMINMAX after you #include "windows.h". It is important that the #define come first.

    The reason is that windows.h (actually I think windef.h, which is included by windows.h) has code similar to this:

    #ifndef NOMINMAX
    #define min(x,y) ((x) < (y) ? (x) : (y))
    #define max(x,y) ((x) > (y) ? (x) : (y))
    #endif
    

    So #define NOMINMAX is telling the compiler (or actually the preprocessor) to skip over the definitions of min and max, but it will only apply if you do it before you #include "windows.h".

    0 讨论(0)
  • 2020-11-27 06:22

    If you're really desperate, put parentheses around the function names:

    (std::min)(x, y);
    

    This syntax won't apply a function-like macro. (Formally, to apply a function-like macro the name of the macro must be followed by optional white space then a '('.)

    0 讨论(0)
  • 2020-11-27 06:27

    If you define NOMINMAX, because you prefer the STL version, then you may get problems while including gdiplus.h, which uses the min/max macro. As solution you need to include the STL headers and use "using namespace std" before you include the gdiplus.h.

    In example:

    #define NOMINMAX
    
    // Include C++ headers
    #include <algorithm>
    using namespace std;
    
    // Include Windows headers
    #include <windows.h>
    #include <gdiplus.h>
    
    0 讨论(0)
  • 2020-11-27 06:30

    Define NOMINMAX via a compiler flag:

    > cl.exe -DNOMINMAX ...
    

    this will then be defined for all of the source files. I don't use the IDEs but this page provides guidance on navigating the IDE to set this: Using STL in Windows Program Can Cause Min/Max Conflicts :

    Simply define the NOMINMAX preprocessor symbol. This can be done in the Developer Studio project under Build, Settings, on the C/C++ tab, in the Preprocessor category. This will suppress the min and max definitions in Windef.h.

    0 讨论(0)
提交回复
热议问题