std::numeric_limits::max 的语法错误 [英] Syntax error with std::numeric_limits::max

查看:26
本文介绍了std::numeric_limits::max 的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的类结构定义如下:

#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];
};

问题是我不能使用 'std::numeric_limits::max()' 并且编译器说:

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

错误 8 错误 C2059:语法错误:'::'

Error 7 error C2589: '(' : '::'右侧的非法标记

我使用的编译器是 Visual C++ 11 (2012)

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

推荐答案

您的问题是由 头文件引起的,该头文件包含名为 maxmin:

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

看到这个定义,预处理器替换了表达式中的max标识符:

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

编译器报错:'(' : '::' 右侧的非法标记.

作为一种解决方法,您可以将 NOMINMAX 定义添加到编译器标志(或在包含标头之前添加到翻译单元):

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

#define NOMINMAX   

或用括号将max的调用包裹起来,以防止宏扩展:

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

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

#undef max 在调用 numeric_limits::max() 之前:

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

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

这篇关于std::numeric_limits::max 的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆