Why writing code in source file and not in header

The main reason that it is not common is that it is a tradition inherited from C to put the declarations in the headers, and implementations in the .cpp files. Most if not all C libraries and programs work this way. And it is generally better for build times in terms of memory consumption, parallelism, and incremental build time, if you do this.

  • The main time that it is NOT faster is if your code contains a lot of templates, or most if not all of your classes are templates. *

    Template instantiation can be a bottleneck in C++ code, and templates instantiations cannot be cached across compilation units. Look into C++ modules if you can use really recent versions of major compilers.

/r/cpp Thread