60

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); ?

Phil Miller
  • 36,389
  • 13
  • 67
  • 90
Harvey
  • 2,062
  • 2
  • 21
  • 38
  • In my case, without the `-DNOMINMAX` I was getting internal compiler error. It's amusing to watch how Microsoft constantly struggles against itself. – rr- Jan 16 '16 at 08:22

6 Answers6

78

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.

Steve Guidi
  • 19,700
  • 9
  • 74
  • 90
  • Okay, I just have to ask... Can I have both in the same file? x = std::numeric_limits::max(); // some tricky preprocessor command c = max(a,b); – Harvey Dec 15 '09 at 01:31
  • @Harvey: I've editted my answer to address your usage of max() and macro max() in one file. – Steve Guidi Dec 15 '09 at 01: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 Dec 15 '09 at 01: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++. –  Feb 22 '10 at 06:53
70

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.

denis-bu
  • 3,426
  • 1
  • 17
  • 11
  • 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 Oct 08 '13 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 Jan 27 '14 at 15:36
  • How does the wrapping helping here? really confused, can you please explain a bit? – Yousuf Azad Aug 23 '16 at 06:08
  • 1
    @sami1592, apparently preporcessor doesn't recognize ```...max)()``` as macros. Then ```(std::numeric_limits::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::max);``` and check fp's type in run-time. – denis-bu Aug 25 '16 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();
R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
3
#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
dmjalund
  • 285
  • 2
  • 9
1

(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.

Andrew
  • 5,839
  • 1
  • 51
  • 72