什么时候可以忽略#include指令中的文件扩展名? [英] When can you omit the file extension in an #include directive?

查看:223
本文介绍了什么时候可以忽略#include指令中的文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩gmock,并注意到它包含这一行:

I'm playing around with gmock and noticed it contains this line:

#include <tuple>

我希望 tuple.h

什么时候可以排除扩展名,它给指令带来了不同的含义?

When is it okay to exclude the extension, and does it give the directive a different meaning?

推荐答案

C ++标准标头没有.h后缀。我相信的原因是有很多,不同的预标准实现,标准将打破。因此,不是要求供应商将他们退出的iostream.h(例如)头更改为符合标准(这将破坏其现有用户的代码),标准委员会决定删除后缀(我相信没有那么现有的实施已经完成)。

The C++ standard headers do not have a ".h" suffix. I believe the reason is that there were many, different pre-standard implementations that the standard would break. So instead of requiring that vendors change their exiting "iostream.h" (for example) header to be standards compliant (which would break their existing user's code), the standards committee decided that they'd drop the suffix (which, I believe no then existing implementation had already done).

这样,现有的非标准程序将继续使用供应商的非标准库。当用户想要使他们的程序符合标准时,他们将采取的步骤之一是更改 #include 指令删除.h后缀。

That way, existing, non-standard programs would continue to work using the vendor's non-standard libraries. When the user wanted to make their programs standards compliant, one of the steps they would take is to change the "#include" directive to drop the ".h" suffix.

所以

#include <iostream>     // include the standard library version
#include <iostream.h>   // include a vendor specific version (which by 
                        //      now might well be the same)

正如其他答案所提到的,非标准库的编写者可以选择命名约定,但我认为他们会想继续使用.h或.hpp(Boost已经做了)因为几个原因:

As other answers have mentioned, writers of non-standard libraries may choose either naming convention, but I'd think they would want to continue using ".h" or ".hpp" (as Boost has done) for a couple reasons:


  1. 如果&当库被标准化时,标准版本不会自动覆盖以前的非标准版本(导致破坏的用户代码很可能)

  2. 它似乎是一个约定



hash_map 存在的实现,而是他们正在调用标准的实现 unordered_map 。命名空间应该帮助防止这种类型的跳跃通过箍,但它似乎没有工作得很好(或使用得很好),允许他们使用更自然的名称,而不会打破大量的代码。

Note that a similar problem happened when the committee went to add hash maps to the STL - they found that there are already many (different) hash_map implementations that exist, so instead of coming up with a standard one that breaks a lot of stuff out there today, they are calling the standard implementation "unordered_map". Namespaces were supposed to help prevent this type of jumping through hoops, but it didn't seem to work well enough (or be used well enough) to allow them to use the more natural name without breaking a lot of code.

请注意,对于C头,C ++允许您包括< cxxxxxx> < xxxxxx.h> 变体。以c开头,没有.h后缀的文件将它们的声明放在 std 命名空间(可能是全局命名空间)中,具有。 h后缀将命名为全局命名空间(一些编译器也将命名空间放在 std 命名空间中 - 如果这是标准兼容的,我不清楚,危害)。

Note that for the 'C' headers, C++ allows you to include either a <cxxxxxx> or <xxxxxx.h> variant. The one that starts with 'c' and has no ".h" suffix put their declarations in the std namespace (and possibly the global namespace), the ones with the ".h" suffix puts the names the global namespace (some compilers also put the names in the std namespace - it's unclear to me if that's standards compliant, though I don't see the harm).

这篇关于什么时候可以忽略#include指令中的文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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