我应该包括stddef。对于size_t, h或cstddef。

时间:2021-05-04 15:05:35

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?

当我想在c++中使用size_t时,应该包括 或< cstddef > ?我听过几个人说过, 是个坏主意,应该被弃用。这是为什么呢? 。h>

5 个解决方案

#1


13  

I prefer #include <stddef.h>.

我更喜欢# 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++?

C头中的一些名称可以是宏,但是集合与C规则不同。在C语言中,EXIT_FAILURE, isdigit(), getc() a.o.是宏。你知道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?

其次,只有几个标准的C标头需要有 头,Posix header不是。您是否知道哪些头是标准的,哪些是由编译器提供的?

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

第三,当使用来自第三方C库的标头时,您最终会得到#include ,我不喜欢混合 和< cstddef >。 。h> 。h>

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.

第四,新c++标准的当前草案说, 被允许将符号转储到全局名称空间(因为显然现在已经有许多编译器已经这么做了),所以使用#include 并不能保证全局名称空间在将来不会受到影响。因此,我建议在编写可移植代码时,您应该假定全局名称空间会受到影响(尽管现在不允许这样做)。由于只有少数专家似乎知道这一点(参见此处的讨论),最好使用 。即使是开始c++程序员也会理解它会污染全局名称空间。

#2


38  

stddef.h is the C header. The name size_t is in global namespace in it. <cstddef>, on the other hand, is a C++ header which wraps the C names into std namespace, which is naturally the C++ approach, so if you include <cstddef> and the compiler is compliant you'll have to use std::size_t . Clearly, in C++, C++ approach is more appropriate. HTH

stddef。h是C标头。名称size_t在它的全局命名空间中。 是一个c++头,它将C名称封装到std名称空间中,这自然是c++的方法,所以如果您包括 和编译器是兼容的,那么您将不得不使用std::size_t。显然,在c++中,c++方法更合适。HTH

Edit: Technically, the C header too may contain the names in the std namespace. But the C-headers (those that end with .h) introduce the names also to the global namespace (thus polluting it) via using-declarations.

编辑:技术上,C头也可能包含std名称空间中的名称。但是c -header(以.h结尾的)通过使用声明将名称引入到全局名称空间(从而造成污染)。

#3


11  

<stddef.h> is officially a deprecated part of C++ (along with the rest of Annex D of the C++ standard). All of these are (non-deprecated) parts of Standard C, so even though they're deprecated in C++, they're virtually certain to remain available almost indefinitely.

< stddef。h>是C++的正式弃用部分(与c++标准附件D的其他部分一起)。所有这些都是标准C的(非弃用的)部分,所以即使它们在c++中被弃用,它们几乎肯定会被无限期地使用。

A lot of features that aren't deprecated will almost certain disappear first -- export is already gone from the current draft of C++0x, and if I had to guess, I'd say exception specifications were a lot more likely to go than Annex D. When/if these headers do become truly obsolete, it'll probably be from a mature version of David Vandervoorde's modules proposal, which could easily render all headers obsolete.

很多功能不弃用几乎肯定会消失,出口已经从当前草案c++ 0 x,如果要我猜,我想说异常规范更有可能去比附件d .当/如果这些头变得真正过时了,它可能会从一个成熟版的大卫Vandervoorde模块提议,所有标题很容易过时。

At the same time, a fair number of compilers (especially older ones) don't implement the <c*> headers exactly the way the standard prescribes. If you want/need to write code that works with them, you gain quite a bit by using the <*.h> headers instead of the <c*> headers.

与此同时,相当数量的编译器(尤其是较老的编译器)没有按照标准规定的方式实现 报头。如果您希望/需要编写与它们一起工作的代码,您可以通过使用<*来获得相当多的代码。h>头,而不是 标头。 *> *>

Ultimately, I think the <c*> headers were a solution in search of a problem. The C standard requires that these headers only define the names that are required -- no others at all except names that are reserved, such as with a leading underscore followed by another underscore or a capital letter. The reserved names (and a few more) are reserved in C++ as well, so they can't collide with anything in portable code in any case. As such, all the <c*> headers buy you is the ability to define a name in the global namespace that collides with an existing name in the C standard library. That is such a spectacularly awful idea that it's not even worth considering doing, so from a practical viewpoint you've gained nothing.

最后,我认为 header是一个解决问题的解决方案。C标准要求这些标头只定义需要的名称——除了保留的名称以外,没有其他的名称,例如在一个突出的下划线后面加上另一个下划线或大写字母。在c++中保留的名称(以及更多)也保留了,因此无论如何它们都不能与任何可移植代码发生冲突。因此,所有 报头购买您都是在c标准库中与现有名称冲突的全局名称空间中定义名称的能力。这是一个非常可怕的想法,甚至不值得考虑,所以从实际的观点来看,你一无所获。 *> *>

Edit: Even that useless capability worked with few enough real compilers that the current drafts of the up-combing C++0x give permission for the <c*> headers to pollute the global namespace anyway, so even the theoretical advantage is gone.

编辑:即使是那些无用的功能,也没有足够的真正编译器,而现在的c++ 0x的当前草稿给了< C *>头文件的权限,从而污染了全局名称空间,所以即使是理论上的优势也消失了。

#4


2  

Both are in the standard and, AFAIK, there to stay.

两者都是标准的,AFAIK,在那里停留。

The form cXXX always introduces the names in the std namespaces, the form XXX.h always introduces the names in the global namespace. Both may also put the names in the other namespace (at least in C++0X, it wasn't the case previously. As respecting that constraint make it impossible to build a C++ library from a C library you don't control, the constraint was removed. g++ suffers of that problem at least on the non glibc targets).

表单cXXX总是在std命名空间中引入名称,即表单XXX。h总是在全局名称空间中引入名称。这两种方法都可以将名称放在另一个名称空间中(至少在c++ 0X中,以前不是这样的)。由于遵守了这个限制,所以不可能从您不控制的C库中构建c++库,因此取消了约束。g++至少在非glibc目标上有这个问题。

For traditional Unix headers, in all implementation I've tested the form XXX.h includes the additional Unix identifier if you have the needed feature macros defined before. The behavior for the form cXXX was inconsistent between implementations. So in practice I use the XXX.h as I often need those declarations.

对于传统的Unix头文件,在所有实现中,我都测试了表单XXX。如果您有之前定义的必要的特性宏,则包含额外的Unix标识符。表单cXXX的行为在实现之间是不一致的。实际上我用的是XXX。我经常需要那些声明。

#5


-2  

<cstddef> is Standard, and <stddef.h> is not. That's pretty much the end of that. It's not going to be deprecated any time soon because there's a bunch of programs that depend on it.

是标准的, 不是。差不多就是这样了。它不会很快被弃用,因为有很多程序依赖于它。 。h>

#1


13  

I prefer #include <stddef.h>.

我更喜欢# 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++?

C头中的一些名称可以是宏,但是集合与C规则不同。在C语言中,EXIT_FAILURE, isdigit(), getc() a.o.是宏。你知道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?

其次,只有几个标准的C标头需要有 头,Posix header不是。您是否知道哪些头是标准的,哪些是由编译器提供的?

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

第三,当使用来自第三方C库的标头时,您最终会得到#include ,我不喜欢混合 和< cstddef >。 。h> 。h>

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.

第四,新c++标准的当前草案说, 被允许将符号转储到全局名称空间(因为显然现在已经有许多编译器已经这么做了),所以使用#include 并不能保证全局名称空间在将来不会受到影响。因此,我建议在编写可移植代码时,您应该假定全局名称空间会受到影响(尽管现在不允许这样做)。由于只有少数专家似乎知道这一点(参见此处的讨论),最好使用 。即使是开始c++程序员也会理解它会污染全局名称空间。

#2


38  

stddef.h is the C header. The name size_t is in global namespace in it. <cstddef>, on the other hand, is a C++ header which wraps the C names into std namespace, which is naturally the C++ approach, so if you include <cstddef> and the compiler is compliant you'll have to use std::size_t . Clearly, in C++, C++ approach is more appropriate. HTH

stddef。h是C标头。名称size_t在它的全局命名空间中。 是一个c++头,它将C名称封装到std名称空间中,这自然是c++的方法,所以如果您包括 和编译器是兼容的,那么您将不得不使用std::size_t。显然,在c++中,c++方法更合适。HTH

Edit: Technically, the C header too may contain the names in the std namespace. But the C-headers (those that end with .h) introduce the names also to the global namespace (thus polluting it) via using-declarations.

编辑:技术上,C头也可能包含std名称空间中的名称。但是c -header(以.h结尾的)通过使用声明将名称引入到全局名称空间(从而造成污染)。

#3


11  

<stddef.h> is officially a deprecated part of C++ (along with the rest of Annex D of the C++ standard). All of these are (non-deprecated) parts of Standard C, so even though they're deprecated in C++, they're virtually certain to remain available almost indefinitely.

< stddef。h>是C++的正式弃用部分(与c++标准附件D的其他部分一起)。所有这些都是标准C的(非弃用的)部分,所以即使它们在c++中被弃用,它们几乎肯定会被无限期地使用。

A lot of features that aren't deprecated will almost certain disappear first -- export is already gone from the current draft of C++0x, and if I had to guess, I'd say exception specifications were a lot more likely to go than Annex D. When/if these headers do become truly obsolete, it'll probably be from a mature version of David Vandervoorde's modules proposal, which could easily render all headers obsolete.

很多功能不弃用几乎肯定会消失,出口已经从当前草案c++ 0 x,如果要我猜,我想说异常规范更有可能去比附件d .当/如果这些头变得真正过时了,它可能会从一个成熟版的大卫Vandervoorde模块提议,所有标题很容易过时。

At the same time, a fair number of compilers (especially older ones) don't implement the <c*> headers exactly the way the standard prescribes. If you want/need to write code that works with them, you gain quite a bit by using the <*.h> headers instead of the <c*> headers.

与此同时,相当数量的编译器(尤其是较老的编译器)没有按照标准规定的方式实现 报头。如果您希望/需要编写与它们一起工作的代码,您可以通过使用<*来获得相当多的代码。h>头,而不是 标头。 *> *>

Ultimately, I think the <c*> headers were a solution in search of a problem. The C standard requires that these headers only define the names that are required -- no others at all except names that are reserved, such as with a leading underscore followed by another underscore or a capital letter. The reserved names (and a few more) are reserved in C++ as well, so they can't collide with anything in portable code in any case. As such, all the <c*> headers buy you is the ability to define a name in the global namespace that collides with an existing name in the C standard library. That is such a spectacularly awful idea that it's not even worth considering doing, so from a practical viewpoint you've gained nothing.

最后,我认为 header是一个解决问题的解决方案。C标准要求这些标头只定义需要的名称——除了保留的名称以外,没有其他的名称,例如在一个突出的下划线后面加上另一个下划线或大写字母。在c++中保留的名称(以及更多)也保留了,因此无论如何它们都不能与任何可移植代码发生冲突。因此,所有 报头购买您都是在c标准库中与现有名称冲突的全局名称空间中定义名称的能力。这是一个非常可怕的想法,甚至不值得考虑,所以从实际的观点来看,你一无所获。 *> *>

Edit: Even that useless capability worked with few enough real compilers that the current drafts of the up-combing C++0x give permission for the <c*> headers to pollute the global namespace anyway, so even the theoretical advantage is gone.

编辑:即使是那些无用的功能,也没有足够的真正编译器,而现在的c++ 0x的当前草稿给了< C *>头文件的权限,从而污染了全局名称空间,所以即使是理论上的优势也消失了。

#4


2  

Both are in the standard and, AFAIK, there to stay.

两者都是标准的,AFAIK,在那里停留。

The form cXXX always introduces the names in the std namespaces, the form XXX.h always introduces the names in the global namespace. Both may also put the names in the other namespace (at least in C++0X, it wasn't the case previously. As respecting that constraint make it impossible to build a C++ library from a C library you don't control, the constraint was removed. g++ suffers of that problem at least on the non glibc targets).

表单cXXX总是在std命名空间中引入名称,即表单XXX。h总是在全局名称空间中引入名称。这两种方法都可以将名称放在另一个名称空间中(至少在c++ 0X中,以前不是这样的)。由于遵守了这个限制,所以不可能从您不控制的C库中构建c++库,因此取消了约束。g++至少在非glibc目标上有这个问题。

For traditional Unix headers, in all implementation I've tested the form XXX.h includes the additional Unix identifier if you have the needed feature macros defined before. The behavior for the form cXXX was inconsistent between implementations. So in practice I use the XXX.h as I often need those declarations.

对于传统的Unix头文件,在所有实现中,我都测试了表单XXX。如果您有之前定义的必要的特性宏,则包含额外的Unix标识符。表单cXXX的行为在实现之间是不一致的。实际上我用的是XXX。我经常需要那些声明。

#5


-2  

<cstddef> is Standard, and <stddef.h> is not. That's pretty much the end of that. It's not going to be deprecated any time soon because there's a bunch of programs that depend on it.

是标准的, 不是。差不多就是这样了。它不会很快被弃用,因为有很多程序依赖于它。 。h>