ifae/guidelines/cpp
C++ Coding Guidelines
Notes:
C++ version used should be c++17
Naming
- Class, enums and global functions name must be PascalCase
- Method identifiers should be camelCase
- Variables identifiers should be snake_case
- Constants and constexpr identifiers (global or not) must start with “k_” and use snake_case (like k_buffer_size)
- Attributes of a class must start with “_” symbol
- Macros, if used, must use SNAKE_CASE (all uppercase)
- File names .hpp and .cpp should be snake_case
- Both names .hpp and .cpp should be equal (obviously changing the file extension)
Namespaces
Required only on the main software repository for IFAE commons
It’s recommended for each project the use of namespaces (following common sense criteria) but declaration and organization is set by each project.
Identifiers:
- Not too general and not too specific
- Maximum namespace levels after
- Always use lower case
- No use of Acronyms (Unique valid case is ifae and or projects namespace identifier)
Bad:
IFAE::cta::tools // IFAE should be lowercase ifae::CTA::tools // CTA should be lowercase ifae::cta::Tools // All namespace should be lowercaseGood:
Anonymous namespaces:
Anonymous namespaces can be used, and it’s use is specially valid as an alternative to a static variable declaration
Files
- Both files (hpp and cpp) should be in the same folder
Classes
Preferably Each “exportable” class shall have its own header and implementation file. Supporting classes with implementation details should be hidden, using an anonymous namespace and not being declared in include file
Classes element order:
class TestClassA{
public:
// methods: (First constructors and destructors)
protected:
// methods
// attributes
private:
// methods
// attributes initialized by the constructor
// attributes default initialized
// attributes manually initialized
//friend declarations
}- If one of the next constructors are declared, other five should be also declared (rule of five). “default” and “delete” operators can be used if needed #### Example: ##### hpp: information.hpp
class Information{
public:
Information() = default;
~Information() = default;
Information(const Information& information) = default;
Information(Information&& information) = default;
Information& operator=(const Information& information) = delete;
Information& operator=(Information&& information) = delete;
};If constructors, copy constructors and copy operators are all defaulted, neither of them should be declared (rule of zero).
If a class is not designed to be inherited must be marked as final
Enums
Includes
First include STL headers. Then third party libraries and finally header files of the cpp object.
Includes in a file should be sorted alphabetically
Avoid non needed includes, it makes modules more dependant and compilation slower. Prefer forward declaration than including a full include if able.
#pragma once directive: To avoid multiple including of a file use #pragma once instead of the old fashion #ifdef #endif
Correct:
##### line.hpp
#pragma once #include "linetype.hpp" //needed include class Point; // Forward declaration class Line { public: Line(const Point& point_a, const Point& point_b); ~Line() = default; Line(const Line& line); Line(Line&& line); Line& operator=(const Line& line); Line& operator=(Line&& line); private: LineType line_type; };##### line.cpp
