为什么我不需要在 gcc 4.6 中包含 STL 头文件?

发布于 2024-12-12 21:31:47 字数 214 浏览 1 评论 0原文

我在 c++0x 模式下使用 gcc 4.6。我注意到,如果我使用向量和其他 STL 容器但不包含其关联的头文件,则似乎不会出现任何编译器错误。这是设计使然吗?首选做法是什么?

我总是使用显式指定 std 命名空间...但我总是认为即使指定了命名空间也必须包含标头。

如果最好包含标头,这似乎是一个奇怪的默认值,因为如果没有编译器的帮助,我是否能 100% 正确地做事是值得怀疑的。

I'm using gcc 4.6 in c++0x mode. I've noticed I don't seem to get any compiler errors if I use vectors and other STL containers but don't include their associated header files. Was this by design? What's the preferred practice?

I do always use explicitly specify the std namespace... but I always thought that the header had to be included even if the namespace is specified.

This seems like a strange default if it's preferable to include the headers since it's doubtful that I'll get things right 100% of the time without help from the compiler.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2024-12-19 21:31:47

您应该始终包含代码所需的所有标准标头。

不幸的是,标准标头允许包含其他标准标头,在这种情况下,您可能会发现自己可以使用某些不包含正确标头的内容。您不能依赖于此,并且如果您更改为不同的库实现,您可能会发现必须修复您的包含内容。

没有简单的方法可以确保您始终获得正确的包含内容;最简单的选择是测试您的代码是否可以使用您感兴趣的所有库实现进行编译。

You should always include all the standard headers that your code needs.

Unfortunately, standard headers are allowed to include other standard headers, in which case you may find yourself able to use something without including the correct header. You can't rely on that, and you may find that you have to fix your includes if you change to a different library implementation.

There's no simple way to ensure you always get your includes correct; the easiest option is to test that your code compiles with all the library implementations you're interested in.

甜警司 2024-12-19 21:31:47

赋予GCC(即您的情况下的g++)的选项-H使GCC打印它所包含的每个标头。它可以帮助您找出文件是如何被包含的(即来自哪个其他标头)。我不认为 GCC 神奇地包含了一些 stdc++ 标头。

The option -H given to GCC (i.e. to g++ in your case) make GCC print every header that it is including. It can help you to find out how a file was included (i.e. from which other header). And I don't think GCC is including magically some stdc++ headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文