我的类定义一直使用*.h文件,但在阅读了一些boost库代码后,我意识到它们都使用*.hpp。我一直很讨厌这个文件扩展名,我想主要是因为我不习惯它。

使用*.hpp而不是*.h的优点和缺点是什么?


当前回答

幸运的是,这很简单。

如果您使用c++,您应该使用.hpp扩展名,并且您应该使用.h表示C或混合使用C和c++。

其他回答

Codegear c++ Builder使用.hpp作为从Delphi源文件自动生成的头文件,使用.h文件作为“自己的”头文件。

所以,当我写一个c++头文件时,我总是使用.h。

工具和人类很容易区分事物。就是这样。

在常规使用中(通过boost等),.hpp是特别的c++头文件。另一方面,.h用于非c++专用的头文件(主要是C)。由于存在许多非平凡的情况,因此精确检测内容的语言通常很难,因此这种差异通常使现成的工具易于编写。对于人类来说,一旦习惯了,也就容易记忆和使用了。

然而,我要指出,公约本身并不总是如预期的那样有效。

它不受语言规范的强迫,无论是C还是c++。有许多项目不遵循惯例。一旦你需要合并(混合)它们,就会很麻烦。 .hpp本身不是唯一的选择。为什么不是。hh或。hxx?(尽管无论如何,您通常至少需要一个关于文件名和路径的常规规则。)

我个人在我的c++项目中使用。h和。hpp。我没有遵循上面的惯例,因为:

The languages used by each part of the projects are explicitly documented. No chance to mix C and C++ in same module (directory). Every 3rdparty library is required to conforming to this rule. The conformed language specifications and allowed language dialects used by the projects are also documented. (In fact, I even document the source of the standard features and bug fix (on the language standard) being used.) This is somewhat more important than distinguishing the used languages since it is too error-prone and the cost of test (e.g. compiler compatibility) may be significant (complicated and time-consuming), especially in a project which is already in almost pure C++. Filenames are too weak to handle this. Even for the same C++ dialect, there may be more important properties suitable to the difference. For example, see the convention below. Filenames are essentially pieces of fragile metadata. The violation of convention is not so easy to detect. To be stable dealing the content, a tool should eventually not only depend on names. The difference between extensions is only a hint. Tools using it should also not be expected behave same all the time, e.g. language-detecting of .h files on github.com. (There may be something in comments like shebang for these source files to be better metadata, but it is even not conventional like filenames, so also not reliable in general.)

我通常在c++的头文件中使用.hpp,并且头文件应该只以头文件的方式使用(维护),例如作为模板库。对于.h中的其他头文件,要么有相应的.cpp文件作为实现,要么是非c++头文件。后者对于人工(或具有显式嵌入元数据的工具,如果需要的话)通过头的内容进行区分是微不足道的。

我使用。h,因为这是微软使用的,也是他们的代码生成器创建的。没必要违背常理。

c++ ("C Plus Plus")作为.cpp是有意义的

扩展名为.hpp的头文件没有相同的逻辑流。

我回答这个问题是作为一个提醒,以指出我对“user1949346”回答这个OP的评论。


正如许多人已经回答的那样:两种方式都可以。 接着强调他们自己的印象。

介绍一下,正如前面提到的,我的观点是,如果没有反对的理由,c++头扩展名应该是。h。

由于ISO/IEC文档使用这种头文件的符号,甚至在他们的c++语言文档中也没有与.hpp匹配的字符串。

但我现在的目标是一个可认可的理由,为什么两种方式都是可以的,特别是为什么它不是语言本身的主题。

我们开始吧。

c++文档(我实际上参考了N3690版本)定义了头文件必须符合以下语法:

2.9报头名称 头名称: < h-char-sequence > “q-char序列” h-char-sequence: h-char h-char-sequence h-char h-char: 源字符集的任何成员,除了new-line和> q-char-sequence: q-char q-char-sequence q-char q-char: 源字符集的任何成员,除了new-line和"

因此,正如我们可以从这一部分提取的那样,头文件名也可以是源代码中任何有效的名称。除了包含'\n'字符并且取决于它是否包含<>,它不允许包含>。 或者另一种方式,如果包含了""-include则不允许包含"。

换句话说:如果您有一个环境支持像prettstupididea这样的文件名。>,一个include like:

#include "prettyStupidIdea.>"

是有效的,但是:

#include <prettyStupidIdea.>>

是无效的。反过来也一样。

甚至

#include <<.<>

将是一个有效的可包含头文件名。

即使这是符合c++的,但这将是一个非常非常愚蠢的想法。

这就是为什么.hpp也是有效的。

但这不是委员会为语言设计决策的结果!

所以讨论使用。hpp和讨论。cc,。mm或其他我在其他文章中读到的关于这个主题的东西是一样的。

我不得不承认,我不知道.hpp从何而来1,但我敢打赌,某些解析工具、IDE或其他与c++有关的东西的发明者想到这个想法是为了优化一些内部进程,或者只是为了发明一些(甚至可能是必要的)新的命名约定。

但它不是语言的一部分。

无论何时有人决定这样使用它。也许是因为他最喜欢它,或者是因为工作流的一些应用程序需要它,它从来不是语言的要求。因此,无论谁说“pp是因为它与c++一起使用”,就语言定义而言都是错误的。

c++允许与前一段有关的任何内容。 如果有什么是委员会建议使用的,那么它将使用.h,因为这是ISO文档中所有示例中使用的扩展名。

结论:

As long you don't see/feel any need of using .h over .hpp or vise versa, you shouldn't bother. Because both would be form a valid header name of same quality in respect to the standard. And therefore anything that REQUIRES you to use .h or .hpp is an additional restriction of the standard which could even be contradicting with other additional restrictions not conform with each other. But as OP doesn't mention any additional language restriction, this is the only correct and approvable answer to the question

“*.h或*.hpp用于类定义”是:

只要不存在外部限制,两者都是正确的和适用的。


1据我所知,显然是boost框架提供了.hpp扩展名。

当然我不能说未来的版本会带来什么!