我正在开发一个现有的C++代码库,它恰好在几个地方使用SIZE_MAX
。我做了一些重构,现在没有在其中一个模块中定义SIZE_MAX
。当特拉维斯-CI试图在Linux上构建该项目时,出现了这个问题。在重构内容之前,它工作得很好,但是跟踪包含了哪些确切的头文件是很困难的。
为了在本地复制这个问题,我安装了一个带有默认gcc的Ubuntu,并能够复制它。以下是相关的消息来源:
#include <stddef.h>
int main()
{
size_t a = SIZE_MAX;
}
命令行简单地是:
g++ a.cpp
错误是:
a.cpp: In function ‘int main()’:
a.cpp:5:16: error: ‘SIZE_MAX’ was not declared in this scope
系统信息:
$ uname -a
Linux quartz 3.11.0-15-generic #25~precise1-Ubuntu SMP Thu Jan 30 17:39:31 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
我尝试过包括cstdint
、stdint.h
、limits.h
、inttypes.h
、stdio.h
、stdlib.h
,以及其他一些,但我无法确定SIZE_MAX
需要哪个特定的头文件。
重要的是要注意,在我做一些修改之前,我正在编写的程序编译得很好,在不同的地方都使用了SIZE_MAX
。我所做的更改使它在使用它的一个.cpp
源文件中变得没有定义(其他的仍然很好)。因此,在我的系统中有一些头文件是正确定义的。
发布于 2015-05-27 03:54:50
在包含__STDC_LIMIT_MACROS
和__STDC_CONSTANT_MACROS
之前,可能会有一些标头定义了stdint.h
和stdint.h
。
使用g++ -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS a.cpp
在Linux上编译应该可以解决旧编译器上的这个问题。
发布于 2015-05-27 03:41:47
18.4.1标题概要
标头还定义了表单的许多宏: INT_FAST最少{8 16 32 64}_MIN UINT_FAST最少{8 16 32 64}_MAX INT{MAX PTR}_MIN UINT{MAX PTR}_MAX {PTRDIFF SIG_ATOMIC WCHAR WINT}{_MAX _MIN} SIZE_MAX
编辑
在目前的C++11/14标准中,SIZE_MAX
被引入,并且只在<cstdint>
中提到。它也是C99
的一部分,规范C++11通过<cxxx>
头完全包括其中。因此,它似乎不是在C++11之前定义的。
发布于 2015-09-02 02:57:44
哪个C++标准标头定义了SIZE_MAX?
它应该在<cstdint>
中定义,但它是可选的。
这是关于费多拉22和GCC 5.1的结果:
#include <cstdint>
// use SIZE_MAX
在以下方面的成果:
g++ -DNDEBUG -g -O2 -fPIC -march=native -pipe -c filters.cpp
In file included from /usr/include/c++/5.1.1/cstdint:35:0,
from filters.cpp:14:
/usr/include/c++/5.1.1/bits/c++0x_warning.h:32:2: error: #error This file requires
compiler and library support for the ISO C++ 2011 standard. This support is currently
experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
filters.cpp: In constructor ‘Filter::Filter(BufferedTransformation*)’:
filters.cpp:305:36: error: ‘SIZE_MAX’ was not declared in this scope
: Filter(attachment), m_firstSize(SIZE_MAX), m_blockSize(0), m_lastSize(SIZE_M
^
简单地说,这样做更容易,并且不再担心不可移植的可选性--2015年仍然会造成问题。
#include <limits>
#ifndef SIZE_MAX
# ifdef __SIZE_MAX__
# define SIZE_MAX __SIZE_MAX__
# else
# define SIZE_MAX std::numeric_limits<size_t>::max()
# endif
#endif
尝试__SIZE_MAX__
会让您回到您可能渴望的编译时间常量。您可以看到它是否在带有cpp -dM < /dev/null | grep __SIZE_MAX__
的预处理器中定义。
(以及为什么numeric_limits<size_t>::max()
是而不是编译时常数是另一个C++谜团,但这是一个不同的问题)。
https://stackoverflow.com/questions/30472731
复制相似问题