CbmRoot
|
In general, every .cxx file should have an associated .h file. There are some common exceptions, such as unit tests and small .cxx files containing just a main() function. Correct use of header files can make a huge difference to the readability, size and performance of your code. The following rules will guide you through the various pitfalls of using header files.
Header files should be self-contained (compile on their own) and end in .h. Non-header files that are meant for inclusion should end in .inc and be used sparingly. All header files should be self-contained. Users and refactoring tools should not have to adhere to special conditions to include the header. Specifically, a header should have header guards and include all other headers it needs.
Prefer placing the definitions for template and inline functions in the same file as their declarations. The definitions of these constructs must be included into every .cxx file that uses them, or the program may fail to link in some build configurations. If declarations and definitions are in different files, including the former should transitively include the latter. Do not move these definitions to separately included header files (-inl.h); this practice was common in the past, but is no longer allowed.
As an exception, a template that is explicitly instantiated for all relevant sets of template arguments, or that is a private implementation detail of a class, is allowed to be defined in the one and only .cc file that instantiates the template.
There are rare cases where a file designed to be included is not self-contained. These are typically intended to be included at unusual locations, such as the middle of another file. They might not use header guards, and might not include their prerequisites. Name such files with the .inc extension. Use sparingly, and prefer self-contained headers when possible.
All header files should have #define guards to prevent multiple inclusion.
The format of the symbol name should be
<CLASSNAME>_H
Since there should be no class with the same name this should guarantee uniqueness. For example, the file sim/detectors/much/qa/CbmMuchTransportQa.h should have the following guard:
If a source or header file refers to a symbol defined elsewhere, the file should directly include a header file which properly intends to provide a declaration or definition of that symbol. It should not include header files for any other reason.
Do not rely on transitive inclusions. This allows people to remove no-longer-needed #include statements from their headers without breaking clients. This also applies to related headers - foo.cxx should include bar.h if it uses a symbol from it even if foo.h includes bar.h.
You may forward declare ordinary classes in order to avoid unnecessary #include statements.
A "forward declaration" is a declaration of a class, function, or template without an associated definition. #include lines can often be replaced with forward declarations of whatever symbols are actually used by the client code.
Pros:
Cons:
Decision:
Always #include the file that actually provides the declarations/definitions you need; do not rely on the symbol being brought in transitively via headers not directly included.
Define functions inline only when they are small, say, 10 lines or fewer.
Definition:
Pros:
Cons:
Decision:
Headers should be included in an order from local to global. Separate each non-empty group with one blank line. This results in the following order:
All of a project's header files should be listed without the use of a directory.
Note that the C headers such as stddef.h are essentially interchangeable with their C++ counterparts (cstddef). Either style is acceptable, but prefer consistency with existing code.
Within each section the includes should be ordered alphabetically.
There are two types of #include statements: #include <myFile.h> and #include "myFile.h". The difference between both is the order of directory paths used to access the header files
Include headers from external libraries using angle brackets.
Include headers from your own project/libraries using double quotes.
Don't use using directives in header files.