C++ cstdlib 和 stdlib.h 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2900785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:26:21  来源:igfitidea点击:

What's the difference between cstdlib and stdlib.h?

c++std

提问by Free Wildebeest

When writing C++ code is there any difference between:

在编写 C++ 代码时,以下之间有什么区别:

#include <cstdlib>

and

#include <stdlib.h>

other than the former being mostly contained within the std::namespace?

除了前者主要包含在std::命名空间中?

Is there any reason other than coding standards and style to use one over the other?

除了编码标准和风格之外,还有什么理由可以使用其中之一吗?

采纳答案by Brendan Long

The first one is a C++ header and the second is a C header. Since the first uses a namespace, that would seem to be preferable.

第一个是 C++ 头文件,第二个是 C 头文件。由于第一个使用命名空间,这似乎更可取。

回答by Jerry Coffin

No, other than the namespace situation, they're essentially identical.

不,除了命名空间情况外,它们本质上是相同的。

回答by klutt

Is there any reason other than coding standards and style to use one over the other?

除了编码标准和风格之外,还有什么理由可以使用其中之一吗?

Yes. The fact that stdlib.his deprecatedis a very good reason to not use it. It was actually deprecated in the very first standard that came 1998. Sure, it still existed in C++14, and possibly or even probably in C++17 (I don't have access to the C++17 standard) but since it is deprecated it is strong signal that you should not use it. Maybe the risk of removal isn't very high, but why even risk it while writing new code when it is so easy to avoid?

是的。不推荐使用的事实stdlib.h不使用它的一个很好的理由。它实际上在 1998 年出现的第一个标准中就被弃用了。当然,它仍然存在于 C++14 中,并且可能甚至可能存在于 C++17 中(我无法访问 C++17 标准)但是由于它已被弃用,这是您不应使用它的强烈信号。也许删除的风险不是很高,但是为什么在编写新代码时要冒这个风险,那么容易避免呢?

From C++14 standard:

C++14 标准

These are deprecated features, where deprecated is defined as: Normative for the current edition of the Standard, but having been identified as a candidate for removal from future revisions.

...

这些是不推荐使用的功能,其中不推荐使用的定义为: 本标准当前版本的规范,但已被确定为从未来修订版中删除候选功能。

...

You should have a pretty strong argument to use stdlib.hinstead of cstdlib

你应该有一个非常有力的论据来使用stdlib.h而不是cstdlib