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

C++Visual C++MaxNumeric Limits

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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

Solution 4 - C++

#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

Solution 5 - C++

(std::numeric_limits::max)()

Easy as pie.

Solution 6 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHarveyView Question on Stackoverflow
Solution 1 - C++Steve GuidiView Answer on Stackoverflow
Solution 2 - C++denis-buView Answer on Stackoverflow
Solution 3 - C++R Samuel KlatchkoView Answer on Stackoverflow
Solution 4 - C++dmjalundView Answer on Stackoverflow
Solution 5 - C++Mattias BergmarkView Answer on Stackoverflow
Solution 6 - C++AndrewView Answer on Stackoverflow