Why not all C++ Header-only libraries?
Even Boost provides part of the functionality in the form of header-only libraries, and the part in the form of compiled.
And there are 2 questions:
- Are there any restrictions on header-only libraries?
- What type and when it is better to use?
Answer 1, Authority 100%
why not all C++ libraries are Header-only, you can understand, considering their advantages and cons
Pluses of using Header-Only Libraries:
allows you to simplify the delivery of libraries for the client, since the client itself can compile the source file with the desired settings for the desired platform.
Allows the compiler to make more optimizations, in particular inline, which is not possible when dividing libraries to implementing files and interface, but you are at risk of inflating the executable code due to the use of this header file in many units of broadcast, but most of the smart compilers are smart and can avoid it.
When adding Header-only to the project requires only Include-paths.
Disadvantages of using Header-Only Libraries:
You can not hide the implementation details, how can this be achieved when dividing the library to the interface in the
.hfile and the implementation file supplied for example in the form of
Most changes in the library will require recompilement of all compilation units using this library.
longer compilation time, as the compiler should see the implementation of all components in the included files, and not just their interfaces.
is complicated by reading the code, since the client is easier to see only the interface of the functional used and not to be distracted by the implementation (controversial statement).
You can not jumble dynamically = cannot be replaced / updated the library in the already compiled program.
When from using Header-Only Libraries Not anyway:
- When using templates (
Template), the title definitions are the only way to compile (if there is no explicit instance of templates), since the compiler must know the full definition of templates to instance the template.
Based on the foregoing, everyone can make a conclusion for themselves, use Header-only or not, I believe that using the Header-Only Library is not the best solution in most cases, if you just do not need templates or inline functions.