You are currently offline, waiting for your internet to reconnect

PRB: Using STL in Windows Program Can Cause Min/Max Conflicts

This article was previously published under Q143208
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
SYMPTOMS
A windows-based program that uses the Standard Template Library (STL) willnot compile, and it generates several invalid parameter and syntax errors.
CAUSE
The problem is caused by conflicting definitions of min and max. Min andmax are defined as macros in Windef.h as follows:
   #ifndef NOMINMAX   #ifndef max   #define max(a,b)  (((a) > (b)) ? (a) : (b))   #endif   #ifndef min   #define min(a,b)  (((a) < (b)) ? (a) : (b))   #endif   #endif  /* NOMINMAX */ 				
NOTE: Windef.h is included by Windows.h, which is included in nearly everyWindows-based program.

Min and max are defined as templated functions in the Standard TemplateLibrary (STL). The following definitions of min and max can be found in thefile Algobase.h:
   template <class T>   inline const T& min(const T& a, const T& b) {       return b < a ? b : a;   }   template <class T, class Compare>   inline const T& min(const T& a, const T& b, Compare comp) {       return comp(b, a) ? b : a;   }   template <class T>   inline const T& max(const T& a, const T& b) {       return  a < b ? b : a;   }   template <class T, class Compare>   inline const T& max(const T& a, const T& b, Compare comp) {       return comp(a, b) ? b : a;   }				
Because the Windef.h definition is a macro, the function definitions causesyntax errors when both Windef.h (or Windows.h) and Algobase.h (from theSTL) are included. The precise errors will depend on the order in which thetwo are compiled.
RESOLUTION
Simply define the NOMINMAX preprocessor symbol. This can be done in theDeveloper Studio project under Build, Settings, on the C/C++ tab, in thePreprocessor category. This will suppress the min and max definitions inWindef.h.
STATUS
This behavior is by design.
Properties

Article ID: 143208 - Last Review: 08/03/2001 11:56:00 - Revision: 1.0

Microsoft Visual C++ 4.0 Standard Edition

  • kb3rdparty kbprb KB143208
Feedback
.name='ms.dqp0';m.content='true';document.getElementsByTagName('head')[0].appendChild(m);" onload="var m=document.createElement('meta');m.name='ms.dqp0';m.content='false';document.getElementsByTagName('head')[0].appendChild(m);" src="http://c1.microsoft.com/c.gif?"> /html>>dy>