numeric_limits problem with windows.h
-
2006년 12월 26일 화요일 오전 6:22
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일 화요일 오후 4:02중재자
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 maxint main()
{
int max = std::numeric_limits<int>::max();
} -
2006년 12월 27일 수요일 오전 8:03
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월 26일 화요일 오후 4:02중재자
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 maxint main()
{
int max = std::numeric_limits<int>::max();
} -
2006년 12월 27일 수요일 오전 4:08
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일 수요일 오전 8:03
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.

