Source files
SF.2
A header file must not contain object definitions or non-inline function definitions
Reason
Including entities subject to the one-definition rule leads to linkage errors.
Example
// file.h:
namespace Foo {
int x = 7;
int xx() { return x+x; }
}
// file1.cpp:
#include <file.h>
// ... more ...
// file2.cpp:
#include <file.h>
// ... more ...
Linking file1.cpp and file2.cpp will give two linker errors.
Alternative formulation: A header file must contain only:
#includes of other header files (possibly with include guards)- templates
- class definitions
- function declarations
externdeclarationsinlinefunction definitionsconstexprdefinitionsconstdefinitionsusingalias definitions- ???
Enforcement
Check the positive list above.