Syntax error with std::numeric_limits::max

前端 未结 2 1402
一生所求
一生所求 2020-12-13 00:06

I have class struct definition as follows:

#include 

struct heapStatsFilters
{
    heapStatsFilters(size_t minValue_ = 0, size_t maxValue_ = s         


        
2条回答
  •  渐次进展
    2020-12-13 00:30

    Your problem is caused by the 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::max()
    

    by the macro definition, eventually leading to invalid syntax:

    std::numeric_limits::(((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::max)()
    //                 ^                                ^
    

    or #undef max before calling numeric_limits::max():

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

提交回复
热议问题