C ++代码编译时不包含 [英] C++ code compiles without include

查看:66
本文介绍了C ++代码编译时不包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不需要包括cstdlib以及如何禁用它?我在Windows 7上使用带有GCC编译器的Code :: Blocks。

Why I don't need to include cstdlib and how do I disable this? I'm using Code::Blocks with GCC compiler on Windows 7.

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    cout << "Hello" << endl;

    system("pause");
    return 0;
}


推荐答案

您不需要包括< cstdlib> ,因为<$ c包含了它(或其中包含 system()的部分) $ c>< iostream> 。未指定标准头是否包括其他(标准)头。您不能禁用此行为,但应注意此行为,以避免不同标准库实现之间的可移植性问题。

You don't need to include <cstdlib> because it (or the part of it containing system()) was included by <iostream>. It is unspecified whether or which other (standard) headers are included by standard headers. You cannot disable this behavior but should be aware of it to avoid portability problems between different standard library implementations.

您不应该依赖此行为,并且自己包括< cstdlib> 。您还应该使用 std :: system 而不是全局 system 。仅保证< c *> 头中的函数位于 std 命名空间中(全局函数位于另一方面,在< *。h> 标头中)。

You should not depend on this behavior and include <cstdlib> yourself. You should also use std::system instead of the global system. Functions from <c*> headers are only guaranteed to be in the std namespace (the global ones, on the other hand, in the <*.h> headers).

这篇关于C ++代码编译时不包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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