C++ *.h 或 *.hpp 用于您的类定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/152555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:17:20  来源:igfitidea点击:

*.h or *.hpp for your class definitions

c++header

提问by Mark Ingram

I've always used a *.hfile for my class definitions, but after reading some boost library code, I realised they all use *.hpp. I've always had an aversion to that file extension, I think mainly because I'm not used to it.

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

What are the advantages and disadvantages of using *.hppover *.h?

使用*.hppover 有*.h什么优点和缺点?

采纳答案by David Holm

Here are a couple of reasons for having different naming of C vs C++ headers:

以下是 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 编写库,然后用 C++ 实现包装器的项目。由于标题通常具有相似的名称,即 Feature.h 与 Feature.hpp,它们很容易区分。
  • 包含,也许您的项目有更合适的用 C++ 编写的版本,但您使用的是 C 版本(见上点)。如果头文件以它们实现的语言命名,您可以轻松找到所有 C 头文件并检查 C++ 版本。

Remember, C is notC++ and it can be very dangerous to mix and match unless you know what you are doing. Naming your sources appropriately helps you tell the languages apart.

请记住,C不是C++,混合和匹配可能非常危险,除非您知道自己在做什么。适当地命名您的来源有助于您区分语言。

回答by paercebal

I use .hpp because I want the user to differentiate what headers are C++ headers, and what headers are C headers.

我使用 .hpp 是因为我希望用户区分哪些头是 C++ 头,哪些头是 C 头。

This can be important when your project is using both C and C++ modules: Like someone else explained before me, you should do it very carefully, and its starts by the "contract" you offer through the extension

当您的项目同时使用 C 和 C++ 模块时,这可能很重要:就像我之前的其他人解释的那样,您应该非常小心地进行操作,并且从您通过扩展提供的“合同”开始

.hpp : C++ Headers

.hpp:C++ 头文件

(Or .hxx, or .hh, or whatever)

(或 .hxx,或 .hh,或其他)

This header is for C++ only.

此头文件仅适用于 C++。

If you're in a C module, don't even try to include it. You won't like it, because no effort is done to make it C-friendly (too much would be lost, like function overloading, namespaces, etc. etc.).

如果您在 C 模块中,甚至不要尝试包含它。你不会喜欢它,因为没有努力使它对 C 友好(太多会丢失,如函数重载、命名空间等)。

.h : C/C++ compatible or pure C Headers

.h : C/C++ 兼容或纯 C 头文件

This header can be included by both a C source, and a C++ source, directly or indirectly.

C 源代码和 C++ 源代码都可以直接或间接地包含此头文件。

It can included directly, being protected by the __cplusplusmacro:

它可以直接包含,受__cplusplus宏保护:

  • Which mean that, from a C++ viewpoint, the C-compatible code will be defined as extern "C".
  • From a C viewpoint, all the C code will be plainly visible, but the C++ code will be hidden (because it won't compile in a C compiler).
  • 这意味着,从 C++ 的角度来看,C 兼容代码将被定义为extern "C".
  • 从 C 的角度来看,所有 C 代码都将是显而易见的,但 C++ 代码将被隐藏(因为它不会在 C 编译器中编译)。

For example:

例如:

#ifndef MY_HEADER_H
#define MY_HEADER_H

   #ifdef __cplusplus
      extern "C"
      {
   #endif

   void myCFunction() ;

   #ifdef __cplusplus
      } // extern "C"
   #endif

#endif // MY_HEADER_H

Or it could be included indirectly by the corresponding .hpp header enclosing it with the extern "C"declaration.

或者它可以通过相应的 .hpp 标头间接包含在extern "C"声明中。

For example:

例如:

#ifndef MY_HEADER_HPP
#define MY_HEADER_HPP

extern "C"
{
#include "my_header.h"
}

#endif // MY_HEADER_HPP

and:

和:

#ifndef MY_HEADER_H
#define MY_HEADER_H

void myCFunction() ;

#endif // MY_HEADER_H

回答by Perculator

I always considered the .hppheader to be a sort of portmanteau of .hand .cppfiles...a header which contains implementation details as well.

我一直认为.hpp标题是一种文件.h.cpp文件的组合......一个包含实现细节的标题。

Typically when I've seen (and use) .hppas an extension, there is no corresponding .cppfile. As others have said, this isn't a hard and fast rule, just how I tend to use .hppfiles.

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

回答by Burkhard

It does not matter which extension you use. Either one is OK.

您使用哪个扩展并不重要。任何一个都可以。

I use *.hfor C and *.hppfor C++.

*.h用于 C 和*.hppC++。

回答by ProgramCpp

EDIT[Added suggestion from Dan Nissenbaum]:

编辑[添加来自丹尼森鲍姆的建议]:

By one convention, .hpp files are used when the prototypes are defined in the header itself. Such definitions in headers are useful in case of templates, since the compiler generates the code for each type only on template instantiation. Hence, if they are not defined in header files, their definitions will not be resolved at link time from other compilation units. If your project is a C++ only project that makes heavy use of templates, this convention will be useful.

按照一种约定,当在标头本身中定义原型时,将使用 .hpp 文件。头文件中的此类定义在模板的情况下很有用,因为编译器仅在模板实例化时为每种类型生成代码。因此,如果它们没有在头文件中定义,它们的定义将不会在链接时从其他编译单元解析。如果您的项目是大量使用模板的仅 C++ 项目,则此约定将很有用。

Certain template libraries that adhere to this convention provide headers with .hpp extensions to indicate that they do not have corresponding .cpp files.

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

another convention is to use .h for C headers and .hpp for C++; a good example would be the boost library.

另一个约定是对 C 头文件使用 .h,对 C++ 使用 .hpp;一个很好的例子是 boost 库。

Quote from Boost FAQ,

File extensions communicate the "type" of the file, both to humans and to computer programs. The '.h' extension is used for C header files, and therefore communicates the wrong thing about C++ header files. Using no extension communicates nothing and forces inspection of file contents to determine type. Using '.hpp' unambiguously identifies it as C++ header file, and works well in actual practice. (Rainer Deyke)

来自 Boost 常见问题的报价,

文件扩展名将文件的“类型”传达给人类和计算机程序。'.h' 扩展名用于 C 头文件,因此传达了有关 C++ 头文件的错误信息。不使用扩展名不会传达任何信息并强制检查文件内容以确定类型。使用 '.hpp' 可以明确地将其标识为 C++ 头文件,并且在实际实践中效果很好。(雷纳戴克)

回答by Serge

I've recently started using *.hppfor c++ headers.

我最近开始使用*.hppc++ 头文件。

The reason is that I use emacs as my primary editor and it enters automatically into c-mode when you load a *.hfile and into c++-mode when you load a *.hppfile.

原因是我使用 emacs 作为我的主要编辑器,它在加载*.h文件时自动进入 c 模式,在加载文件时自动进入 c++ 模式*.hpp

Apart that fact I see no good reasons for choosing *.hover *.hppor vice-versa.

除了这个事实,我看不出有什么好的理由来选择*.h*.hpp反之亦然。

回答by dhein

I'm answering this as an reminder, to give point to my comment(s) on "user1949346" answer to this same OP.

我回答这个是为了提醒,指出我对“user1949346”的评论对同一个 OP 的回答。



So as many already answered: either way is fine. Followed by emphasizes of their own impressions.

正如许多人已经回答的那样:无论哪种方式都可以。其次是强调自己的印象。

Introductory, as also in the previous named comments stated, my opinion is C++header extensions are proposed to be .hif there is actually no reason against it.

介绍性的,正如前面提到的评论中所说的,我的意见是C++.h如果实际上没有理由反对的话,建议使用标题扩展。

Since the ISO/IEC documents use this notation of header files and no string matching to .hppeven occurs in their language documentations about C++.

由于 ISO/IEC 文档使用头文件的这种表示法,并且.hpp在它们的语言文档中甚至没有出现与C++.

But I'm now aiming for an approvable reason WHY either way is ok, and especially why it's not subject of the language it self.

但我现在的目标是一个可批准的理由,为什么任何一种方式都可以,尤其是为什么它不是它本身的语言的主题。

So here we go.

所以我们开始了。

The C++documentation (I'm actually taking reference from the version N3690) defines that a header has to conform to the following syntax:

C++文件(实际上,我正在从版本N3690参考)定义了一个头必须符合以下语法:

2.9 Header names

header-name:
    < h-char-sequence >
    " q-char-sequence "
h-char-sequence:
    h-char
    h-char-sequence h-char
h-char:
    any member of the source character set except new-line and >
q-char-sequence:
    q-char
    q-char-sequence q-char
q-char:
    any member of the source character set except new-line and "

2.9 标题名称

header-name:
    < h-char-sequence >
    " q-char-sequence "
h-char-sequence:
    h-char
    h-char-sequence h-char
h-char:
    any member of the source character set except new-line and >
q-char-sequence:
    q-char
    q-char-sequence q-char
q-char:
    any member of the source character set except new-line and "

So as we can extract from this part, the header file name may be anything that is valid in the source code, too. Except containing '\n'characters and depending on if it is to be included by <>it is not allowed to contain a >. Or the other way if it is included by ""-include it is not allowed to contain a ".

因此,我们可以从这部分中提取,头文件名也可以是源代码中有效的任何名称。除了包含'\n'字符外,根据是否要包含<>它,不允许包含>. 或者,如果它被包含在""-include 中,则不允许包含".

In other words: if you had a environment supporting filenames like prettyStupidIdea.>, an include like:

换句话说:如果你有一个支持文件名的环境,比如prettyStupidIdea.>,包括:

#include "prettyStupidIdea.>"

would be valid, but:

将是有效的,但是:

#include <prettyStupidIdea.>>

would be invalid. The other way around the same.

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

And even

乃至

#include <<.<>

would be a valid includable header file name.

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

Even this would conform to C++, it would be a pretty pretty stupid idea, tho.

即使这符合C++,这也将是一个非常愚蠢的想法,不过。

And that's why .hppis valid, too.

.hpp也是有效的原因。

But it's not an outcome of the committees designing decisions for the language!

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

So discussing about to use .hppis same as doing it about .cc, .mmor what ever else I read in other posts on this topic.

因此,讨论使用.hpp与使用相同.cc.mm或者我在有关此主题的其他帖子中读到的其他内容。

I have to admit I have no clue where .hppcame from1, but I would bet an inventor of some parsing tool, IDE or something else concerned with C++came to this idea to optimize some internal processes or just to invent some (probably even for them necessarily) new naming conventions.

我不得不承认我不知道1.hpp是从哪里来的,但我敢打赌,某个解析工具、IDE 或其他与此相关的东西的发明者会想到这个想法来优化一些内部流程或只是发明一些(甚至可能对他们来说是必要的) ) 新的命名约定。C++

But it is not part of the language.

但它不是语言的一部分。

And whenever one decides to use it this way. May it be because he likes it most or because some applications of the workflow require it, it never2is a requirement of the language. So whoever says "the pp is because it is used with C++", simply is wrong in regards of the languages definition.

每当人们决定以这种方式使用它时。可能是因为他最喜欢它,或者因为工作流的某些应用程序需要它,它永远不会2是语言的要求。因此,无论谁说“pp 是因为它与 C++ 一起使用”,在语言定义方面都是错误的。

C++ allows anything respecting the previous paragraph. And if there is anything the committee proposed to use, then it is using .hsince this is the extension sued in all examples of the ISO document.

C++ 允许任何尊重上一段的内容。如果委员会提议使用任何内容,那么它正在使用,.h因为这是 ISO 文件所有示例中起诉的扩展名。

Conclusion:

结论:

As long you don't see/feel any need of using .hover .hppor 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 REQUIRESyou to use .hor .hppis 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

只要您没有看到/感觉需要使用.hover.hpp或反之亦然,您就不应该打扰。因为两者都将形成与标准相同质量的有效标头名称。因此,任何需要您使用的内容.h.hpp标准的附加限制,甚至可能与其他不符合的附加限制相矛盾。但由于 OP 没有提到任何额外的语言限制,这是该问题唯一正确且可批准的答案

"*.h or *.hpp for your class definitions" is:

您的类定义的 *.h 或 *.hpp”是:

Both are equally correct and applicable as long as no external restrictions are present.

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



1From what I know, apparently, it is the boost framework that came up with that .hppextension.

1 据我所知,很显然,提出该.hpp扩展的是 boost 框架。

2Of course I can't say what some future versions will bring with it!

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

回答by JohnMcG

I prefer .hpp for C++ to make it clear to both editors and to other programmers that it is a C++ header rather than a C header file.

我更喜欢 C++ 的 .hpp 以让编辑器和其他程序员清楚它是 C++ 头文件而不是 C 头文件。

回答by slashmais

You can call your includes whatever you like.

你可以随意调用你的包含。

Just need to specify that full name in the #include.

只需要在#include.

I suggest that if you work with C to use .hand when with C++ to use .hpp.

我建议,如果您使用 C 来使用.h以及使用 C++ 时使用.hpp.

It is in the end just a convention.

它最终只是一个约定。

回答by Dynite

C++ ("C Plus Plus") makes sense as .cpp

C++(“C Plus Plus”)作为 .cpp 有意义

Having header files with a .hpp extension doesn't have the same logical flow.

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