我应该包括stddef.h或cstddef为size_t [英] Should I include stddef.h or cstddef for size_t

查看:260
本文介绍了我应该包括stddef.h或cstddef为size_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想在C ++中使用 size_t 时,应包含< stddef.h> < cstddef> ?我听到几个人说,< cstddef> 是一个坏主意,它应该被弃用。为什么呢?

解决方案

我更喜欢 #include< stddef.h>



C头文件中的一些名称可以是宏,但是集合不同于C规则。在C中, EXIT_FAILURE isdigit() getc > ao是宏。你知道哪些是C ++中的宏?



其次,只有几个标准的C头文件需要< cfoo& code>头,Posix头不是。你知道哪些标头是标准的,哪些是由你的编译器提供的?



第三,当使用第三方C库的头文件时, #include< stddef.h> ,我不喜欢混合< stddef.h> 第四,新C ++标准的当前草案说,< cstddef>



。 ; cstdlib>
允许将符号转储到全局命名空间中(因为显然很多编译器现在已经这么做),所以使用 #include< cstdlib> 不保证全局命名空间将来不会被污染。所以我建议,当写可移植代码,你应该假设全局命名空间将受到影响(即使它不允许现在)。由于只有少数专家似乎知道这一点(参见本文评论中的讨论),最好使用< stddef.h> ,因为即使是开始C ++程序员理解它污染了全局命名空间。


When I want to use size_t in C++, should I include <stddef.h> or <cstddef>? I have heard several people saying that <cstddef> was a bad idea, and it should be deprecated. Why is that?

解决方案

I prefer #include <stddef.h>.

Some of the names in the C headers are allowed to be macros, but the set differs from the C rules. In C, EXIT_FAILURE, isdigit(), getc() a.o. are macros. Do you know which ones are macros in C++?

Secondly, only a couple standard C headers are required to have the <cfoo> header, Posix headers are not. Do you know which headers are standard and which ones are provided by your compiler only?

Thirdly, when using headers from a third-party C library, you will end up with #include <stddef.h>, and I prefer not to mix <stddef.h> and <cstddef>.

Fourthly, the current draft for the new C++ standard says that <cstdlib> is allowed to dump the symbols into the global namespace (because apparently many compilers already do so nowadays), so using #include <cstdlib> is not a guarantee that the global namespace will be unpolluted in the future. So I would advice that when writing portable code, you should assume the global namespace will be affected (even though it is not allowed now). As only a few experts seem to know this (see the discussion in the comments here), it is better to use <stddef.h> as even a beginning C++ programmer will understand that it pollutes the global namespace.

这篇关于我应该包括stddef.h或cstddef为size_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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