Syntax error with std::numeric_limits::max

Solution 1:

Your problem is caused by the <Windows.h> header file that includes macro definitions named max and min:

#define max(a,b) (((a) > (b)) ? (a) : (b))

Seeing this definition, the preprocessor replaces the max identifier in the expression:

std::numeric_limits<size_t>::max()

by the macro definition, eventually leading to invalid syntax:

std::numeric_limits<size_t>::(((a) > (b)) ? (a) : (b))

reported in the compiler error: '(' : illegal token on right side of '::'.

As a workaround, you can add the NOMINMAX define to compiler flags (or to the translation unit, before including the header):

#define NOMINMAX   

or wrap the call to max with parenthesis, which prevents the macro expansion:

size_t maxValue_ = (std::numeric_limits<size_t>::max)()
//                 ^                                ^

or #undef max before calling numeric_limits<size_t>::max():

#undef max
...
size_t maxValue_ = std::numeric_limits<size_t>::max()

Solution 2:

As other people say the problem is that in <WinDefs.h> (included by <windows.h>) is defined macroses min and max, but if you'll see it's declaration:

// <WinDefs.h>
#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 */

you'll see that if there is defined a macro NOMINMAX then WinDefs.h will not produce these macroses.

That's why it would be better to add a define NOMINMAX to project.