warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits::max();

前端 未结 6 1096
梦毁少年i
梦毁少年i 2020-12-04 21:28

This line works correctly in a small test program, but in the program for which I want it, I get the following compiler complaints:

#include 

         


        
相关标签:
6条回答
  • 2020-12-04 21:55

    (std::numeric_limits::max)()

    Easy as pie.

    0 讨论(0)
  • 2020-12-04 21:56

    Other solution would be to wrap function name with parenthesis like this: (std::numeric_limits<int>::max)(). Same applies to std::max.

    Not sure it's good solution for this... NOMINMAX is better IMO, but this could be an option in some cases.

    0 讨论(0)
  • 2020-12-04 21:58

    Some other header file is polluting the global name space with a max macro. You can fix that by undefining the macro:

    #undef max
    x = std::numeric_limits<int>::max();
    
    0 讨论(0)
  • 2020-12-04 22:15

    Its definition in for me in Visual Studio 2013 (formatted for better spacing...) is as follows:

    static _Ty (max)() _THROW0()
    {   // return maximum value
        return (FLT_MAX);
    }
    

    So I'm just using FLT_MAX. :) This may not be a universal solution, but it works well in my case, so I thought I would share.

    0 讨论(0)
  • 2020-12-04 22:19

    This commonly occurs when including a Windows header that defines a min or max macro. If you're using Windows headers, put #define NOMINMAX in your code, or build with the equivalent compiler switch (i.e. use /DNOMINMAX for Visual Studio).

    Note that building with NOMINMAX disables use of the macro in your entire program. If you need to use the min or max operations, use std::min() or std::max() from the <algorithm> header.

    0 讨论(0)
  • 2020-12-04 22:20
    #ifdef max
    #pragma push_macro("max")
    #undef max
    #define _restore_max_
    #endif
    
    #include <limits>
    
    //... your stuff that uses limits
    
    #ifdef _restore_max_
    #pragma pop_macro("max")
    #undef _restore_max_
    #endif
    
    0 讨论(0)
提交回复
热议问题