哪些C ++编译器自动定义size_t而不需要包含头?

时间:2022-03-01 15:08:29

While using the C++ compiler of Visual Studio 2013, I noticed that my code relying on size_t correctly compiled even without including any headers that define it (i.e. #include <stddef.h> or #include <string.h>).

在使用Visual Studio 2013的C ++编译器时,我注意到我的代码依赖于size_t正确编译,甚至没有包含任何定义它的头文件(即#include 或#include )。

I tested this because I kind of like the idea of not including a whole header just for something trivial like that; I feel like it bloats my code. I concluded that the C++ compiler of Visual Studio 2013 automatically defines the size_t, even without any header inclusions in the code.

我测试了这个,因为我有点像不包括整个标题的想法只是为了那些微不足道的事情;我觉得它膨胀了我的代码。我得出结论,Visual Studio 2013的C ++编译器自动定义size_t,即使代码中没有任何头部包含。

While enjoying this, I started worrying about portability. Coding convenience, and feeling like my code is elegant, are things more important to me than absolute portability; but I would still like some portability. For example, I don't mind using #pragma once since most compilers support it and header guards are a hassle (especially so in Visual Studio), but I would never resort to exporting templates just because one comiler supports it.

在享受这一点的同时,我开始担心可移植性。编码方便,感觉像我的代码优雅,对我来说比绝对可移植性更重要;但我还是喜欢一些便携性。例如,我不介意使用#pragma一次,因为大多数编译器都支持它,并且标题保护很麻烦(特别是在Visual Studio中),但我绝不会因为一个编译器支持它而导致导出模板。

So, my question is, is the automatic definition of size_t a widespread feature offered by many compilers, or is it something specific to Microsoft's compiler?

所以,我的问题是,size_t的自动定义是许多编译器提供的广泛特性,还是微软编译器特有的东西?

2 个解决方案

#1


10  

This behaviour certainly isn't widespread; both GCC and Clang require the inclusion of <cstddef> for std::size_t, or the deprecated <stddef.h> for ::size_t, as the standard specifies.

这种行为当然不普遍; GCC和Clang都需要为std :: size_t包含 ,或者为标准指定包含 for :: size_t。

#2


4  

Neither gcc nor clang contain a similar violation of the C++ standard.

gcc和clang都没有包含类似的违反C ++标准的内容。

Between the 3, MSVC gcc and clang are the vast majority of compilers out there.

在3,MSVC gcc和clang之间是绝大多数的编译器。

The same is true of icc 13.0.1.

icc 13.0.1也是如此。

I did not try ECG, feel free yourself.

我没有尝试心电图,自己感觉*。

#1


10  

This behaviour certainly isn't widespread; both GCC and Clang require the inclusion of <cstddef> for std::size_t, or the deprecated <stddef.h> for ::size_t, as the standard specifies.

这种行为当然不普遍; GCC和Clang都需要为std :: size_t包含 ,或者为标准指定包含 for :: size_t。

#2


4  

Neither gcc nor clang contain a similar violation of the C++ standard.

gcc和clang都没有包含类似的违反C ++标准的内容。

Between the 3, MSVC gcc and clang are the vast majority of compilers out there.

在3,MSVC gcc和clang之间是绝大多数的编译器。

The same is true of icc 13.0.1.

icc 13.0.1也是如此。

I did not try ECG, feel free yourself.

我没有尝试心电图,自己感觉*。