Reputation: 2271
Can someone please help me to understand, why for non-template classes, it is recommended to separate code in header and source file? Is that only code styling or does this approach avoid possible errors (e.g., linker errors)?
I'm wondering since in case of template classes, we are not even allowed to do the separation.
Many thanks in advance!
Upvotes: 0
Views: 36
Reputation: 8018
Is that only code styling or does this approach avoid possible errors (e.g., linker errors)?
It's mainly used to reduce interdependencies of implementation details in the compilation phase, and such lower overall compilation times.
If you change an inlined implementation, all of the translation units seeing it need to be recompiled.
With separate declaration in header files and only referring to the function interface (signature) for non templated functions or classes, recompiling isn't needed, if the internal implementation changes.
I'm wondering since in case of template classes, we are not even allowed to do the separation.
Templates are a bit different, since type parameters are injected into the definition. These cannot be instantiated in separate translation units, unless keeping track and implementing all of the foreseeable type specializations there.
Upvotes: 1