I know this maybe quite subjective, but are there any general rules for situations when it is not necessary for code to be split into two files?
For example is the class is extremely small, or if the file simply holds some global definitions or static functions? Also, in these cases, should the single file be a .cpp file or a .h file?
Split the code into header and source whenever you can.
Generally, this shouldn't be done in next cases :
Should be the header file (.h).
I favor putting code in .hpp files but am very often compelled to put the implementation in the .cpp for any of the following reasons:
inline
. Although, it won't matter; the optimizing compiler will likely inline if it wants to.There are logical motivations for keeping code inline in a .hpp file:
I am pretty sure though that the tradition of separate .cpp and .hpp files is far stronger than the reasoning however.
The rule I use is the following:
The reasons are multiple:
This way, the .cpp files carry the dependency information between parts of your source. It also lowers build times.
On the technical side, whenever you need to obey the one definition rule you have to separate declarations from definitions, since you will need to include the declarations many times in multiple translation units, but you must only provide one single definition.
In aesthetic terms, the answer could be something like "always", or "systematically". In any case, you should always have a header for every logical unit of code (e.g. one class or one collection of functions); and the source file is one that is possibly optional, depending on whether or not you have everything defined inline (exempting you from ODR), or if you have a template library.
As a meta strategy, you should seek to decouple the compilation units as much as possible, so that you can include only what's needed in a fine-grained way. This allows your project to grow without having compilation times become unbearable, and it makes it much easier to reuse code in other projects.
It's not always subjective; you will very good reasons to separate them in large projects. It's good to get into the practice of separating them, and learning when it is and is not appropriate to separate definition from declaration. It's hard to answer your question without knowing how complex your codebase will become.
It's still not necessarily a bad idea to separate them, in general.
The header should not contain global definitions which require static construction, unless necessary.
These do not belong anywhere in C++. Use inline, or anonymous namespace. If you mean within a class' body, "it depends on the instruction count, if you are hoping it will be inlined".
The single file should be a header. Rationale: You should not
#include
cpp files.And don't forget that intermodule (aka link-time) optimizations are getting better and better.
C++ compilation times are long, and it's very very very time consuming to fix this after the fact. I recommend that you get into the practice of using cpp files before your build times and dependencies explode.