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

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


当前回答

我一直认为.hpp头文件是.h和.cpp文件的混合…还包含实现细节的标头。

通常,当我看到(并使用).hpp作为扩展名时,没有相应的.cpp文件。正如其他人所说,这不是一个严格的规则,只是我倾向于使用.hpp文件。

其他回答

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

在常规使用中(通过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,用*.hpp表示c++。

编辑[增加了Dan Nissenbaum的建议]:

根据约定,当原型在头文件中定义时,使用.hpp文件。对于模板,头文件中的这种定义很有用,因为编译器只在模板实例化时为每种类型生成代码。因此,如果它们没有在头文件中定义,它们的定义将不会在链接时从其他编译单元解析。如果你的项目只使用c++,并且大量使用模板,这个约定将会很有用。

某些遵循此约定的模板库提供带有.hpp扩展名的头文件,以表明它们没有相应的.cpp文件。

另一个习惯是用.h表示C头文件,用.hpp表示c++头文件;boost库就是一个很好的例子。

引用自Boost FAQ, 文件扩展名向人类传递文件的“类型” 还有计算机程序。'.h'扩展名用于C头文件 文件,因此传达了关于c++头文件的错误信息 文件。不使用扩展不能传达任何信息,还会迫使检查 确定文件内容的类型。明确使用“。hpp” 将其标识为c++头文件,并且在实际应用中工作良好。 (Rainer Deyke)

下面是C和c++头文件命名不同的几个原因:

Automatic code formatting, you might have different guidelines for formatting C and C++ code. If the headers are separated by extension you can set your editor to apply the appropriate formatting automatically Naming, I've been on projects where there were libraries written in C and then wrappers had been implemented in C++. Since the headers usually had similar names, i.e. Feature.h vs Feature.hpp, they were easy to tell apart. Inclusion, maybe your project has more appropriate versions available written in C++ but you are using the C version (see above point). If headers are named after the language they are implemented in you can easily spot all the C-headers and check for C++ versions.

记住,C不是c++,除非你知道你在做什么,否则混合和匹配是非常危险的。适当地命名源代码可以帮助您区分语言。

你可以随意命名你的include。

只需要在#include中指定全名。

我建议你使用C语言时使用.h,使用c++时使用.hpp。

它最终只是一种惯例。