关于c++风格 code style
- Header files should be self-contained.
- All header files should have #define guards to prevent multiple inclusion. The format of the symbol name should be <PROJECT>_<PATH>_<FILE>_H_.
- You may forward declare ordinary classes in order to avoid unnecessary #includes. 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.
- Use standard order for readability and to avoid hidden dependencies: Related header, C library, C++ library, other libraries' .h, your project's .h.
- dir2/foo2.h.
- C system files.
- C++ system files.
- Other libraries' .h files.
- Your project's .h files.
- In dir/foo.cc or dir/foo_test.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:
- Do not make nested classes public unless they are actually part of the interface, e.g., a class that holds a set of options for some method.
- Variables needed for if, while and for statements should normally be declared within those statements, so that such variables are confined to those scopes. E.g.:
- while (const char* p = strchr(str, '/')) str = p + 1;
- Constructors should never call virtual functions or attempt to raise non-fatal failures. If your object requires non-trivial initialization, consider using a factory function or Init() method.
- Use the C++ keyword explicit for constructors callable with one argument.
- Use delegating and inheriting constructors when they reduce code duplication.
- Inheriting constructors allow a derived class to have its base class's constructors available directly, just as with any of the base class's other member functions, instead of having to redeclare them. This is especially useful if the base has multiple constructors.
- Only very rarely is multiple implementation inheritance actually useful. We allow multiple inheritance only when at most one of the base classes has an implementation; all other base classes must be pure interface classes tagged with the Interface suffix. Multiple inheritance allows a sub-class to have more than one base class. We distinguish between base classes that are pure interfaces and those that have an implementation.
- All parameters passed by reference must be labeled const.
- Use streams only for logging.
- Use prefix form (++i) of the increment and decrement operators with iterators and other template objects.
- document that a variable is non-negative using assertions. Don't use an unsigned type.
- The names of variables and data members are all lowercase, with underscores between words. Data members of classes (but not structs) additionally have trailing underscores. For instance: a_local_variable, a_struct_data_member, a_class_data_member_. Data members of structs, both static and non-static, are named like ordinary nonmember variables. They do not have the trailing underscores that data members in classes have.
- There are no special requirements for global variables, which should be rare in any case, but if you use one, consider prefixing it with g_ or some other marker to easily distinguish it from local variables.
- Regular functions have mixed case; accessors and mutators match the name of the variable: MyExcitingFunction(), MyExcitingMethod(), my_exciting_member_variable(), set_my_exciting_member_variable().
- Use a k followed by mixed case, e.g., kDaysInAWeek, for constants defined globally or within a class.const int kDaysInAWeek = 7;
- Enumerators should be named either like constants or like macros: either kEnumName or ENUM_NAME.
- Empty loop bodies should use {} or continue, but not a single semicolon.
- both of the && logical AND operators are at the end of the line.
摘自:http://google-styleguide.googlecode.com/svn/trunk/cppguide.html
关于命名空间:
https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/naming/
命名空间和目录层相对应,不需要额外的缩进。
https://zh-google-styleguide.readthedocs.io/en/latest/google-cpp-styleguide/formatting/
布尔表达式的逻辑操作永远放在行尾。