CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

SF.1: Use .hpp instead of .h for header files

Open jfbilodeau opened this issue 10 years ago • 10 comments

May I suggested that .hpp be the proposed standard extensions for C++ header file instead of .h? Let .h be for header that compile with the C programming langage and .hpp be headers that compile with C++.

I'm of the opinion that .hpp is a good choice since is follows the standard form of source file (.cpp) and is also widely used (ie: Boost)

jfbilodeau avatar Dec 10 '15 13:12 jfbilodeau

Many projects and development environments have mixture of "C" (e.g. system headers) and "C++". Many projects have legitimate reasons to distinguish the header files by extensions. Furthermore, there are many other well-established extensions, such as .hxx or .H. These choices should be left to individual projects.

gdr-at-ms avatar Dec 10 '15 18:12 gdr-at-ms

Many projects and development environments have mixture of "C" (e.g. system headers) and "C++".

I recognize that many projects mix C and C++ and use .h files to allow the C++ compiler to use C-style function/structs/etc. However, I cannot think of a situation were we would want the C compiler to attempt to compile C++ code.

Furthermore, there are many other well-established extensions, such as .hxx or .H. These choices should be left to individual projects.

The core guideline does suggest using .cpp as the extension for source file even though it is not the only one used (.c++, .cxx, .cc, etc...). Furthermore, the core guideline does suggest .h as opposed to a C++ specific extension.

Beyond legacy, are there any reason to have the same extension being used by two difference programming language?

jfbilodeau avatar Dec 10 '15 19:12 jfbilodeau

I agree with jfbilodeau. If the guidelines are going to recommend .cpp (and I’m not sure they should), then they should recommend .hpp for headers that are not meant to be included in C files. While .h should be recommended for files that are C and C++ compatible.

Or drop the .cpp recommendation and be silent on the matter.

fisherro avatar Dec 10 '15 20:12 fisherro

@fisherro Ha, that is a left over and will be cleaned. Thanks!

gdr-at-ms avatar Dec 10 '15 21:12 gdr-at-ms

If I may suggest, it might make more sense to move SF.1 to NL (Naming and Layout). I seems more appropriate.

jfbilodeau avatar Dec 11 '15 02:12 jfbilodeau

Note that SF.1 does not actually mandate using ".cpp" and ".h". It already contains this note (emphasis added):

The specific names .h and .cpp are not required (but recommended) and other names are in widespread use. Examples are .hh and .cxx. Use such names equivalently.

Having said that, perhaps the title of the rule should be improved to avoid giving this misimpression...

hsutter avatar Dec 11 '15 06:12 hsutter

Note that SF.1 does not actually mandate using ".cpp" and ".h".

The Guideline does not mandate, but suggests. Is there any good reason to suggest that C++ headers use the C extension (.h) as opposed to its own extension (.hpp)? Support for legacy code base doesn't sound like a good reason to me.

I recognized that from a technical perspective, the file extension has no bearing on the code itself but I feel that it would make it easier to differentiate between C and C++ code base. How many .h files are incorrectly tagged as C files on site such as GitHub and Tiobe?

...also, how many of us are sick of explaining that C and C++ are not the same language? ;)

jfbilodeau avatar Dec 11 '15 23:12 jfbilodeau

We will clarify this paragraph.

-------- Original message -------- From: Jean-François Bilodeau [email protected] Date: 12/11/2015 3:27 PM (GMT-08:00) To: isocpp/CppCoreGuidelines [email protected] Cc: Gabriel Dos Reis [email protected] Subject: Re: [CppCoreGuidelines] SF.1: Use .hpp instead of .h for header files (#442)

Note that SF.1 does not actually mandate using ".cpp" and ".h".

The Guideline does not mandate, but suggests. Is there any good reason to suggest that C++ headers use the C extension (.h) as opposed to its own extension (.hpp)? Support for legacy code base doesn't sound like a good reason to me.

I recognized that from a technical perspective, the file extension has no bearing on the code itself but I feel that it would make it easier to differentiate between C and C++ code base. How many .h files are incorrectly tagged as C files on site such as GitHub and Tiobe?

...also, how many of us as sick of explaining that C and C++ are not the same language ;)

Reply to this email directly or view it on GitHubhttps://github.com/isocpp/CppCoreGuidelines/issues/442#issuecomment-164076560.

gdr-at-ms avatar Dec 13 '15 01:12 gdr-at-ms

Sorry to resurrect the issue, but why is .h the recommended extension for interface file?

The CCG reason states 'convention', but the STL doesn't use it, neither does Boost and the extension already belongs to another programming language.

jfbilodeau avatar Dec 24 '15 15:12 jfbilodeau

Clarified, thanks!

hsutter avatar Jan 01 '16 01:01 hsutter