62

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 <limits>

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

c:\...\x.cpp(192) : warning C4003: not enough actual parameters for macro 'max'
c:\...\x.cpp(192) : error C2589: '(' : illegal token on right side of '::'
c:\...\x.cpp(192) : error C2059: syntax error : '::'

I get the same results with:

#include <limits>
using namespace std;

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

Why is it seeing max as the macro max(a,b); ?

1
  • In my case, without the -DNOMINMAX I was getting internal compiler error. It's amusing to watch how Microsoft constantly struggles against itself.
    – rr-
    Commented Jan 16, 2016 at 8:22

6 Answers 6

80

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.

4
  • Okay, I just have to ask... Can I have both in the same file? x = std::numeric_limits<int>::max(); // some tricky preprocessor command c = max(a,b);
    – Harvey
    Commented Dec 15, 2009 at 1:31
  • @Harvey: I've editted my answer to address your usage of max() and macro max() in one file. Commented Dec 15, 2009 at 1:43
  • I do use min() and max() in other files in this project and using precompiled headers, it is disabled for all files. #undef max works for my case and is only effective for the rest of the file it is in.
    – Harvey
    Commented Dec 15, 2009 at 1:56
  • 4
    @Harvey: #undef affects the rest of the entire translation unit (very different from "rest of the file it is in"), can lead to results highly dependent on include order, and may interfere with precompiled headers. This answer is the preferred solution. Macros like min and max cause complicated problems in what should be easy. Macros are evil in C++.
    – Roger Pate
    Commented Feb 22, 2010 at 6:53
71

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.

4
  • 1
    Much as I hate the use of the global min/max macros, sometimes it's tricky to remove them from a project completely. Never thought of this as a solution, so +1.
    – icabod
    Commented Oct 8, 2013 at 13:53
  • 7
    Unfortunately, min and max macros are widely used in Windows Platform SDK (for example in GDI+ GdiplusTypes.h). So, you answer is better then define NOMINMAX. +1!
    – 23W
    Commented Jan 27, 2014 at 15:36
  • How does the wrapping helping here? really confused, can you please explain a bit? Commented Aug 23, 2016 at 6:08
  • 1
    @sami1592, apparently preporcessor doesn't recognize ...max)() as macros. Then (std::numeric_limits<int>::max) is converted to a function pointer and () is a function call invoked on a function pointer. You can use debugger to check this. Just type auto fp = (std::numeric_limits<int>::max); and check fp's type in run-time.
    – denis-bu
    Commented Aug 25, 2016 at 14:37
27

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();
1
  • 6
    Don't fix it this way, you can stop it from being define in the first place with NOMINMAX.
    – GManNickG
    Commented Oct 16, 2013 at 17:15
4
#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
2

(std::numeric_limits::max)()

Easy as pie.

0

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.

2
  • 1
    Be careful, you (may) need to #include <float.h> for access to FLT_MAX
    – Ben Voigt
    Commented May 15, 2015 at 23:04
  • This only works if you know the type.
    – Caduchon
    Commented Apr 17, 2023 at 7:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.