locked
numeric_limits problem with windows.h RRS feed

  • 질문

  • The following code works fine:

    #include <limits>
    int max = std::numeric_limits<int>::max();

    However, if you include <windows.h>, you will get compile errors. After doing some investigation, this bug should be introduced by the marco max( ) defined in <windef.h>. ( It replaces max( ) with another statement but still preceeded by numberic_limits<int>:: ). To use numeric_limits correctly, I must write code like this:

    int max = std::numeric_limits<int>::max)()

    It's quit annoying. Coul MS replace this macro ( and min( ), and maybe some more macros ) with inline functions?

    2006년 12월 26일 화요일 오전 6:22

답변

  • I agree, the macro is unfortunate. At this point, it's a question of being backwards compatible.

    A workaround you can try, however:

    #include <limits>
    #include <windows.h>
    #undef max

    int main()
    {
      int max = std::numeric_limits<int>::max();
    }

    2006년 12월 26일 화요일 오후 4:02
  • Note that you can avoid the definition of min and max macros by defining NOMINMAX symbol:

    #define NOMINMAX

    #include <windows.h>

    In this case the min and max functions supplied by STL should work. But you will receive "identifier not found" error if other parts rely on min or max macros. In order to use the STL’s functions instead, I would recommend the following approach:

    #define NOMINMAX

    #include <windows.h>

    #include <limits>

    #include <algorithm>

    template< typename T >

    inline T max(const T & a, const T & b) { return std::max(a, b); }

    template< typename T >

    inline T min(const T & a, const T & b) { return std::min(a, b); }

    The using namespace std or using std::max declarations can help too. I hope this makes sense.

    2006년 12월 27일 수요일 오전 8:03

모든 응답

  • I agree, the macro is unfortunate. At this point, it's a question of being backwards compatible.

    A workaround you can try, however:

    #include <limits>
    #include <windows.h>
    #undef max

    int main()
    {
      int max = std::numeric_limits<int>::max();
    }

    2006년 12월 26일 화요일 오후 4:02
  • I use different trick to avoid the side effect of "#undef max" :

    int max = (std::numeric_limits<int>::max)();

    Use "()" to avoid the macro "max".

    And I don't see any backwards compatible issue if MS implement max like this:

    template<typename T>
    inline T max(const T& a, const T&b) { return a > b ? a : b; }

    or

    template<typename T>
    inline T max(const T& a, const T&b) { return std::max(a, b); }

     

    2006년 12월 27일 수요일 오전 4:08
  • Note that you can avoid the definition of min and max macros by defining NOMINMAX symbol:

    #define NOMINMAX

    #include <windows.h>

    In this case the min and max functions supplied by STL should work. But you will receive "identifier not found" error if other parts rely on min or max macros. In order to use the STL’s functions instead, I would recommend the following approach:

    #define NOMINMAX

    #include <windows.h>

    #include <limits>

    #include <algorithm>

    template< typename T >

    inline T max(const T & a, const T & b) { return std::max(a, b); }

    template< typename T >

    inline T min(const T & a, const T & b) { return std::min(a, b); }

    The using namespace std or using std::max declarations can help too. I hope this makes sense.

    2006년 12월 27일 수요일 오전 8:03
  • I use different trick to avoid the side effect of "#undef max" :

    int max = (std::numeric_limits<int>::max)();

    Use "()" to avoid the macro "max".

    And I don't see any backwards compatible issue if MS implement max like this:

    template<typename T>
    inline T max(const T& a, const T&b) { return a > b ? a : b; }

    or

    template<typename T>
    inline T max(const T& a, const T&b) { return std::max(a, b); }

     

    There is, instead!

    macros work on "C" language, templates do not!


    2014년 5월 7일 수요일 오전 8:59
  • I use different trick to avoid the side effect of "#undef max" :

    int max = (std::numeric_limits<int>::max)();

    Use "()" to avoid the macro "max".

    And I don't see any backwards compatible issue if MS implement max like this:

    template<typename T>
    inline T max(const T& a, const T&b) { return a > b ? a : b; }

    or

    template<typename T>
    inline T max(const T& a, const T&b) { return std::max(a, b); }

     

    There is, instead!

    macros work on "C" language, templates do not!



    Additionally -- if evaluating A and B has visible side effects, the min/max macros will evaluate the selected term twice.  The template function will only evaluate it once.  Normally you would only want to evaluate the term once, but it would likely break some program somewhere that depended on the double evaluation.

    • 편집됨 SimonRev 2014년 5월 7일 수요일 오전 10:56
    2014년 5월 7일 수요일 오전 10:55