-4

I am studying the templates in c++ but in the book they use .hpp files and in the videos I have seen the templates are placed in .h files in some videos I have watched however in the book it uses .hpp it does not explain why and the benefits, I would like to understand why ?

What is the difference and what affects the performance of using .hpp or .h files in c++? Could you please help me. Thanks

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Ramon
  • 5
  • 2
  • 7
    Filename extensions make no difference, whatsoever. They're just different naming conventions. Additionally, I hope you realize that any clown can upload a video to Youtube. Even I can do that. 99% of all videos are garbage. Your textbook is where most of your attention should be directed to. – Sam Varshavchik Jul 20 '23 at 12:53
  • 1
    The names of the header files are irrelevant.There are many conventions about their suffix, some prefer one suffix and some prefer another, and companies might have style-guides that say a third thing. But in the end it's just part of the filename. – Some programmer dude Jul 20 '23 at 12:53
  • Files ending in .hpp are compiled faster because 'turbo power mode' is enabled. Seriously, though there is no difference and even if there were _it would be due to the compiler you use_ and not the language standard. – Dirk Eddelbuettel Jul 20 '23 at 12:54
  • 2
    Nothing affects the performance between the two. It's just convention. I like to use `.hpp` files to indicate that I write C++ code. But `.h` is perfectly fine. It's commonly used with C-files. But all in all pretty irrelevant. Just choose a style and stick to what you prefer. – Eldinur the Kolibri Jul 20 '23 at 12:54
  • 2
    Also, header files are a compile-time thing. There's just nothing about including a header file that will affect runtime performance. – Some programmer dude Jul 20 '23 at 12:55
  • 5
    It's very common for beginners to be focussed on *performance* as if that is the most important thing in programming. In reality you should concentrate on learning how to program in a well organised way, that is far, far more important than performance. In any case, filenames make no difference at all to performance. – john Jul 20 '23 at 13:15
  • Sie note: You can `#include` anything. You could `#include "lena.jpg"` if you had a good enough reason to do it. – user4581301 Jul 20 '23 at 17:17
  • I use separate extensions to separate C language header files from C++ header files. Common C file header extension is ".h". Popular file extensions for C++ are ".hpp", ".hxx" and ".hh". – Thomas Matthews Jul 20 '23 at 17:17
  • Let's get pedantic here and say yes, the files with ".hpp" extension will take longer to find than the ".h" extension files. Down deep in file system, there is a comparison going on to find your header file. The ".hpp" extension has 2 extra characters, so this will take longer to find. The extra time is probably measured in nanoseconds and is insignificant on most platforms. There are usually other activities that are a lot slower, blocking the execution. In summary, the longer the filename the longer the search will be; but the extra time will be insignificant. – Thomas Matthews Jul 20 '23 at 17:23
  • @ThomasMatthews Clearly this is why the C++ standards committee decided to drop the `.h` from standard header files. – john Jul 21 '23 at 06:39

1 Answers1

2

There's no difference, they're just different names. Includes are handled by the preprocessor, and it doesn't care what names your files have, you can end your headers in .blub or .ramons_special_header and it'd make no difference to the compiler (technically speaking the same is true of other source files as well, you could put C++ source code in a .c file, but there compilers that support multiple different language modes like gcc may change how they behave by default depending on what you name your files, but most compilers will have settings to override that behaviour anyhow).

The only reason people use different names is because some names are used for special purposes in certain projects (e.g. .hpp meaning "C++ header" and .h meaning "C header" in some projects with mixed C and C++ sources), or because some people have their editors configured to treat certain files differently based on name. Beyond that it's just a matter of preference. Some people use file endings like .hh or .hxx or .H, and if you search for it you'll find heated discussions about which name is best.

Cubic
  • 14,902
  • 5
  • 47
  • 92