stdio.h
还是cstdio
?为什么?发布于 2017-03-25 13:06:29
,因为这篇文章有点老了,我想分享以下内容:
查看代码:
Using X.h // Compatible with C language standard
---------------
#include <X.h>
int main() {
// Invoke X's corresponding function
return 0;
}
Using X // Not compatible with C language standard
--------------
#include <X>
int main() {
// Invoke X's corresponding function
return 0;
}
他们都编译和执行ok!
哪一个在C++中更好?
C.5.1 ( C++17文档中的一节) 对标头的修改diff.mods.to.headers
<stdatomic.h>
、<stdnoreturn.h>
和<threads.h>
),C头本身也没有是C++的一部分。
<ccomplex>
(D.4.1)和<ctgmath>
(D.4.4)以及它们相应的C头( <complex.h>
和<tgmath.h>
)包含来自C标准库的任何内容,而只是包含来自C++标准库的其他标头。
D.5 C标准库头depr.c.headers
C++11和C++17标准规范文档都指出,<X.h>
的使用仍然与C标准兼容,尽管它们的使用被认为是不推荐的。
他们正在检查"undeprecating" -- C++20中C库头的使用情况。<X.h>
以绿色突出显示。到目前为止,C++11和C++17已被声明为“弱建议”,并显示了用于保持"C标准库标题(c.headers)“的”调整“:
“基本的C库头是一个基本的兼容性特性,而且不会在短期内出现。”(来自C++ 20审查文件)
D.5 C标准 库标头depr.c.headers 弱建议:除了上述之外,还从C++标准中删除相应的C头,就像我们没有相应的
<stdatomic.h>
、<stdnoreturn.h>
或<threads.h>
标头一样。如前所述,但通过以下调整:20.5.5.2.1C标准库头c.headers 为了与C标准库兼容,C++标准库提供了表141中所示的C标头。表141-C报头
<assert.h> <inttypes.h> <signal.h> <stdio.h> <wchar.h>
<complex.h> <iso646.h> <stdalign.h> <stdlib.h> <wctype.h>
<ctype.h> <limits.h> <stdarg.h> <string.h>
<errno.h> <locale.h> <stdbool.h> <tgmath.h>
<fenv.h> <math.h> <stddef.h> <time.h>
<float.h> <setjmp.h> <stdint.h> <uchar.h>
标头
<complex.h>
的行为就好像它只是包含标头<complex>
一样。标头<tgmath.h>
的行为就好像它只是包含标头、<complex>
和<cmath>
。Bjarne Stroustrup建议最大限度地提高C和C++ languages之间的互操作性,通过尽可能减少不兼容。另一些人则持相反的观点,因为这会使事情复杂化。
所以,<X.h>
似乎没有去任何地方。最终,你可以同时使用两者。就我个人而言,我将决定我将使用哪一个代码,归结为让您的代码向后兼容C代码。
https://stackoverflow.com/questions/13889467
复制相似问题