Syntax error with std::numeric_limits::max(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)
推荐答案
您的问题是由 头文件引起的,该头文件包含名为 max 和 min:
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 之前:
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 的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::numeric_limits::max 的语法错误
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
