views:

446

answers:

5

Is using .h as a header for a c++ file wrong?

I see it all over the place, especially with code written in the "C style". I noticed that Emacs always selects C highlighting style for a .h header, but c++ for hpp or hh.

Is it actually "wrong" to label your headers .h or is it just something which annoys me?

EDIT:

There is a good (ish) reason why this annoys me, if I have project files labelled, 'hpp & cpp' I can get away with 'grep something *pp' etc. otherwise I have to type '.h cpp'

+2  A: 

It's like with the other files - .c for C, .cpp for C++ - use .h for C, .hpp for C++. This will satisfy most compilers/editors. It's not really wrong to not do so, but it can be used to give a hint which of the both languages you are actually using.

If you want to stop that behaviour without renaming the files, you should have a look at the Emacs config and try to change the source code detection.

schnaader
+13  A: 

nothing wrong with that. This is the default with Microsoft Visual C++.

Just follow the standard you like, and stick with it.

decasteljau
And noting most of the standard library does not use an extension at all.
Richard
The standard library is a special case though. Don't start writing headers with no extensions.
jalf
The standard library headers aren't required to be "files" at all, so they aren't a good example here. The compiler is free to implement them as it sees fit.
Brian Neal
I might have know it was all Bill's fault, grrrr
Chris Huang-Leaver
+2  A: 

Since Emacs has no easy way of finding out whether a .h file is C or C++, it's not ridiculous to choose C for .h files and C++ for .hpp files.

Note that all boost header files are suffixed with .hpp.

Benoît
strange that vim doesn't have this problem, then?
anon
Score one for the vim homers!
Judge Maygarden
Heh, I recently decided to write some elisp bound to F9 to switch between c-mode and c++-mode exactly for .h header files:(defun switch-c-or-c++-mode () "Switch to c++-mode if currently c-mode, and vice-versa." (interactive) (cond ((eq major-mode 'c-mode) (c++-mode)) ((eq major-mode 'c++-mode) (c-mode))))
Paul Stephenson
I suppose the real question is why would you want to make things needlessly more unclear.
Chris Huang-Leaver
+4  A: 

It's not wrong to call your C++ headers .h. There are no rules of what extensions your code must use. For your non-headers, MSVC uses .cpp and on Linux, .cc as well. There is no one global standard.

But I'd say calling your headers .hpp (I've seen .hh a few times as well) is a lot more consistent and informative than just using .h.

jalf
+3  A: 

You're free to use .h, .H, .hpp, .hxx, .hh, or whathaveyou. Just like you're free to use .c for C sources, and .C, .cpp, .cc, or .cxx for C++ sources. The extension is largely a convention.

greyfade