warning C4003 and errors C2589 and C2059 on: x = std::numeric_limits<int>::max();
Asked Answered
M

6

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

Marty answered 15/12, 2009 at 1:8 Comment(1)
In my case, without the -DNOMINMAX I was getting internal compiler error. It's amusing to watch how Microsoft constantly struggles against itself.Approachable
P
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.

Penrod answered 15/12, 2009 at 1:14 Comment(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);Marty
@Harvey: I've editted my answer to address your usage of max() and macro max() in one file.Penrod
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.Marty
@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++.Splutter
Z
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.

Zelig answered 26/11, 2012 at 14:2 Comment(4)
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.Egoism
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!Bindman
How does the wrapping helping here? really confused, can you please explain a bit?Sparkie
@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.Zelig
V
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();
Velma answered 15/12, 2009 at 1:11 Comment(1)
Don't fix it this way, you can stop it from being define in the first place with NOMINMAX.Hello
R
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
Repand answered 23/1, 2015 at 3:16 Comment(0)
F
2

(std::numeric_limits::max)()

Easy as pie.

Faubert answered 10/4, 2019 at 21:12 Comment(0)
P
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.

Paiz answered 15/5, 2015 at 23:1 Comment(2)
Be careful, you (may) need to #include <float.h> for access to FLT_MAXAlliterate
This only works if you know the type.Minorite

© 2022 - 2024 — McMap. All rights reserved.