Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
371 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...