我的类定义一直使用*.h文件,但在阅读了一些boost库代码后,我意识到它们都使用*.hpp。我一直很讨厌这个文件扩展名,我想主要是因为我不习惯它。
使用*.hpp而不是*.h的优点和缺点是什么?
我的类定义一直使用*.h文件,但在阅读了一些boost库代码后,我意识到它们都使用*.hpp。我一直很讨厌这个文件扩展名,我想主要是因为我不习惯它。
使用*.hpp而不是*.h的优点和缺点是什么?
当前回答
下面是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++,除非你知道你在做什么,否则混合和匹配是非常危险的。适当地命名源代码可以帮助您区分语言。
其他回答
幸运的是,这很简单。
如果您使用c++,您应该使用.hpp扩展名,并且您应该使用.h表示C或混合使用C和c++。
使用哪个扩展名并不重要。两种都可以。
我用*.h表示C,用*.hpp表示c++。
你可以随意命名你的include。
只需要在#include中指定全名。
我建议你使用C语言时使用.h,使用c++时使用.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++头文件。后者对于人工(或具有显式嵌入元数据的工具,如果需要的话)通过头的内容进行区分是微不足道的。
Bjarne Stroustrup和Herb Sutter在他们的c++核心指南(https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#S-source)中对这个问题有一个声明,该指南也提到了标准扩展(c++ 11, c++ 14等)的最新变化。
SF.1: Use a .cpp suffix for code files and .h for interface files if your Y project doesn't already follow another convention Reason It's a longstanding convention. But consistency is more important, so if your project uses something else, follow that. Note This convention reflects a common use pattern: Headers are more often shared with C to compile as both C++ and C, which typically uses .h, and it's easier to name all headers .h instead of having different extensions for just those headers that are intended to be shared with C. On the other hand, implementation files are rarely shared with C and so should typically be distinguished from .c files, so it's normally best to name all C++ implementation files something else (such as .cpp). The specific names .h and .cpp are not required (just recommended as a default) and other names are in widespread use. Examples are .hh, .C, and .cxx. Use such names equivalently. In this document, we refer to .h and .cpp > as a shorthand for header and implementation files, even though the actual extension may be different. Your IDE (if you use one) may have strong opinions about suffices.
我不是这种惯例的大粉丝,因为如果你正在使用像boost这样的流行库,你的一致性已经被打破了,你最好使用.hpp。