58

I have class struct definition as follows:

#include <limits>

struct heapStatsFilters
{
    heapStatsFilters(size_t minValue_ = 0, size_t maxValue_ = std::numeric_limits<size_t>::max())
    { 
         minMax[0] = minValue_; minMax[1] = maxValue_; 
    }

    size_t minMax[2];
};

The problem is that I cannot use 'std::numeric_limits::max()' and the compiler says:

Error 8 error C2059: syntax error : '::'

Error 7 error C2589: '(' : illegal token on right side of '::'

The compiler which I am using is Visual C++ 11 (2012)

5
  • Can't reproduce. Try including <cstddef>. Commented Dec 12, 2014 at 11:44
  • @ParkYoung-Bae I am not using std for size_t in my code. Should I do that? Commented Dec 12, 2014 at 11:48
  • @remyabel Which compiler are you using? I am using Visual C++ 11 (2012) Commented Dec 12, 2014 at 11:48
  • 2
    check if you have somewhere #define max(a,b) or similar, or add #undef max before struct ifxHeapStatsFilters to check if it's the issue here Commented Dec 12, 2014 at 11:53
  • @PiotrS. yes! you are right! it worked! :) Commented Dec 12, 2014 at 12:00

2 Answers 2

149

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Be warned: defining NOMINMAX might cause new trouble as the min/max macros are required/used/expected by lots of other Windows headers...
<< or wrap the call to max with parenthesis, which prevents the macro expansion: >> Worked perfectly for me, thank you!
10

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.