I met and wrote the library of one .hpp file myself. Conveniently, do not need to repeat the code. How can this be bad?
Here is an example of a fairly popular and successful library.

  • What do you mean by code? Header files must have declarations, not executable code. - Vlad from Moscow
  • @Vlad, That's it - kyb

3 answers 3

The bad thing about this is that you lose the advantages of a separate compilation.

First is the time. Your function will be compiled each time the cpp-file includes this header. Worse, now every time you change the implementation details, you will have to recompile not one cpp-file with implementation, but all that include your header.

Secondly, this is an increasing number of dependencies. If you do not need other headers for the declaration of functions, but are needed for implementation, then now you are forced to include everything in the header, which means that all who use your header will also “see” these other headers.

Problem number 3 adjoins this: ring dependencies. Suppose you have two functions in different files that call each other. If only function declarations are in headers, then the missing header can be connected to cpp-files, and functions can easily refer to each other. In the case where the implementation is in the header, this will not work because of the mutual recursion of the inclusions, and you have to go for ugly tricks like “know exactly what functions the other header needs and pre-declare them before switching on”.

And finally, you lose encapsulation: anyone can see your implementation, and this provokes coding using the knowledge of how everything is really arranged there (which is bad for obvious reasons).


Note that if your function is a template, then you will most likely have to declare it completely in the header, and face all the problems listed above.

  • As I understand it, you can write the library itself in .hpp, and compile it in .so, and then connect it? - handicraftsman
  • @Linuxoid: If the entire library is in .hpp, then in theory there is no need to compile it in .so. - VladD
  • What do you say about the size of the compiled file? Will the library be duplicated when compiling each file? - kyb pm
  • @kyb: If you mark functions as static , it will be. If not as static , but as inline , then there will not be duplicates will throw out the linker. - VladD
  • @VladD and if not static and not inline , but class A{ int f(){/*code*/} } - kyb

Implementation in header files is not only possible, but required in several cases. In particular, when implementing generic types, they are templates (templates), as well as when implementing inline functions.

On the other hand, in some cases such an implementation can lead to errors during linking, since the same function will appear in different .obj files. To avoid this, the function must be declared static ( static ), then it will not be visible from other object modules . In fact, this means that each module object will have its own copy of this function. I want to note that in this case we are not talking about static class methods, which are also commonly denoted by the keyword static .

Summary: this is not bad and sometimes necessary. The main thing is to understand what you are doing and why.

    Fundamentally nothing wrong with that. Vaughn boost by more than half in headers implemented.

    The downsides are an increase in compile time and binary size (optional).

    • Footprint just does not increase? - kyb
    • @kyb what is footprint? - Cerbo
    • the size of executable - kyb
    • @kyb yes, updated answer - Cerbo 1:51 pm
    • What does not necessarily mean? - kyb pm