table of Contents
Company C ++ Standard Learning
Grammar section
- Class and keyword selection struct: class represents a type of user-defined encapsulated, non-private static data members defined, struct represents a simple collection of data defined only initialization methods for the data members.
- The list must be used constructor initializes the display initialize all direct base classes and base class data members.
- The meaning of class must not prohibit copy constructor and assignment constructors with DISALLOW_COPY_AND_ASSIGN macro.
- DISALLOW_COPY_AND_ASSIGN macro function is to replicate and copy assignment operator is declared as private variables.
- Prohibit complex operations (such as application resource) that can go wrong in a constructor, complex operations () function is implemented in additional init.
- Explicit initialization can make the code more clear, easy mistake to call, but also to avoid the problems caused by secondary assignment efficiency, single-argument constructor try to add explicit.
- Has a default value of semantic class must display a default constructor.
- No default semantic type, must be explicitly defined or other private constructor declare a default constructor, not given implementation.
- If the type is a copy, we must also define the copy constructor and assignment constructors, if the movable type, the mobile must also define a constructor and move assignment function.
- If the default copy and move operations, use = default definitions.
- If the type does not need to copy, move operation, the means to be used = delete disabled.
- Data is added to the vessel, preferably used interface functions beginning emplace.
- In rare cases, when this kind of implicit conversion makes sense, we can not put a single argument constructor declared as explicit constructor, but you must be very careful and use documentation of notes.
- Single-argument constructor if not explicit keyword modification, it may be used by the compiler to do implicit type conversion.
- The meaning of the class must have given explicit copy constructor, and carefully specify its behavior (shallow copy and deep copy, etc.)
- Managed class resources, often not replicated meaningless. At this point it should be invoked to prevent user errors caused by leakage of resources, the consequences of repeated release.
- The compiler generates the default copy constructor, using a shallow copy strategy pointer data members, but this strategy is often not programmers want.
- Destructor:
- If class defines virtual functions must be defined virtual destructor.
- If the class is designed to be inherited, it should be defined or a protected virtual destructor non-virtual destructor disclosed.
- [RULE010] (not including structure) comprising pointer members, must explicitly given destructor, and carefully specify the behavior (whether the destruction of the pointer, how to destroy the like).
- It must not be allowed to make an exception to leave the destructor.
- According to public, protected, private declaration order of members. Declaration order types, methods, data members of the members, at the end of private sections DISALLOW_COPY_AND_ASSIGN.
- It is not very necessary, to avoid the use friend: friends violate encapsulation type, an increase coupling between classes, friend suitably used as a container with its iterator class involving the class.
- Delegation and extends Constructors C ++ 11 introduced by the constructor in order to reduce duplication of code developed two different characteristics. By special initialization list syntax, delegate constructor allows a class constructor call another constructor.
- C ++ 11 strongly typed enumeration
enum class Side
- Display overloaded virtual function declared with override.
- Prohibited from being overloaded function display prohibition by final.
- Lambda expressions:
- Not using the default capture (including &, =), capture shows to write about.
- In both cases only the right reference value, one is the definition of the type of mobile operating function, the other is the perfect time to define a template function forward. In addition, do not use the right reference value.
- Rvalue reference semantics more complicated, improper use can cause hard to track down the bug.
- The recommendations include path, file name and namespace consistent
- Use spaces to indent, do not use tabs. 4 spaces to indent the unit. Avoid over five heavy indentation.
- An exception is not recommended, unless the existing project / underlying library uses an exception, this time must catch all exceptions.
- All the code in the namespace should be defined, except for the main function.
- Inline prohibited (inline) virtual function, inline function should be less than 10 lines.
- Function still using the default parameters, you can use the default template parameters. - The default parameters are compile-binding, does not have the polymorphism.
- The auto automatic derivation types, can define local variables (except for the type declaration rear return).
- Prohibit the use of auto_ptr. If you need to pass an object, use std :: unique_ptr clear ownership transfer. If you want to share ownership, use std :: shared_ptr clear ownership share.
- [RULE033] to avoid floating-point numbers for equality or inequality comparisons.
- Avoid using non-Boolean variable or expression as a branch statement conditions.
- When equal for comparison, the recommended value of the constant or variable on the right of the right == operator.
- Macro (such as DISALLOW_COPY_AND_ASSIGN, CFATAL_LOG etc.) except for conditional compilation, debugging tracking, or be able to significantly reduce the amount of code and ensure readability, and should not be used.
- Before encouraging the use of the statement, in order to reduce the dependencies between files.
- As far as possible avoid the use of global variables, if necessary, you should use singleton pattern instead.
- Global functions / variables for internal use, must be declared as static functions, variables, external visible symbols can not appear in the target file.
- Not in the class header file defines global constants / struct type, to reduce code bloat.
- It may be used for inter-module communication across or involve explicitly specify the value of the enumeration value must be stored to avoid inconsistent versions cause strange errors.
- const keyword is used in front of the type name rather than later.
- Does not modify the internal state of the members of the method must be declared as const
- Return unmodifiable pointer or reference must be declared const
- Not modified reference / pointer parameter must be declared const
- Unless modified state is not guaranteed, it should not be used to remove the const attribute const_cast
- Avoid define an array of local variables exceeds 4 KB
Style / convention
- Must refer to the header file in the following order, and sub-section: .cpp corresponding header file (if any, // priority), C (standard) library, C ++ (standard) library, other libraries, the project's own .h file , include in each section in alphabetical order.
- Branch and spaces:
- A separate statement must be on a separate line.
- Avoid consecutive blank lines.
- Using an appropriate blank lines to logic of the code packets.
- The opening brace is not independent, the right brace on a separate line. And where the left-brace vertical alignment.
- Namespace is not indented.
- The constructor to initialize a list on the same line or side by side in at least eight-indent a few lines, change lines if required, to: on the first line.
- Shorter function declarations, the entire statement to a line; long function declaration, so that each parameter per line, and vertically aligned, the line feed parameters for at least eight spaces indentation.
- Relatively short function call statement, the entire statement per line; function call is too long, fold lines to ensure that each control line not more than 100, at least an additional indent after newline eight spaces.
- Space use:
- A space between parentheses if / switch / while / for / catch and behind, with no spaces between the inner and the judgment expression in parentheses.
- for statements within parentheses before the semicolon without space, a space after the semicolon.
- Function call, no spaces between the function name and parentheses.
- Colon initialization list of class inheritance and constructors around a space.
- Comma expression or the parameter list, no space before a comma, a space after the comma.
- Unary operators without front and rear spaces. Before and after a binary, ternary expressions each of a space.
- Member Access or without spaces before and after scoping operator. Such as:. Ab &, A-> B, A B, A-> B, A :: B.
- Parentheses and the front and rear brackets operator and not the interior spaces.
- .h Do not define complex functions, complex functions in the header file if necessary, should be placed .hpp suffix header file.
- Not to use the function parameters, the parameters of the Notes together, does not allow unnamed parameters appear.
- : The order parameter function, it is recommended to arrange the input parameters, and then arranges the output parameters.
// 输入参数对象类型使用const引用 void foo(const std::string& input1, const MyClass& input2);
Naming conventions
- Globally visible, but can not name by name identifier space constraints, the library must name as a prefix to avoid conflicts.
- Name should be as descriptive, do not use non-generic abbreviations (especially a character abbreviation province as creat, usr, etc.), do not use ambiguous abbreviations, do not use any of meaningless characters.
- The larger the scope identifier, naming it should be more clear.
- Possible to use a plurality of unit variable names (such as a variable representing the time), the unit shall be abbreviated name suffix.
- All file names by lowercase letters, with an interval of _; example: this_is_my_awesome_file.cpp.
- Test unit using the file name <tested filename> _test.h (.cpp) naming.
- If you want to publish it lib for people to use, it is recommended only exposed to a library name of the same name, or <library name> _ <feature set> .h header files of api, then the relevant header files to include this header file api. Such as: mylib.h, mylib_utils.h, mylib_api.h.
- Use all lowercase underscore separated nomenclature namespace.
- Namespace name can be abbreviated, and should ensure a short and difficult conflict but at the same time meaningful.
- Function names use underscores to separate all lowercase nomenclature.
- Function verb phrases commonly used name.
- Custom class types using the first letter capitalized hump nomenclature, generally do not use the prefix.
- Members of the enumerated type, use all capital snake nomenclature (ie all capital letters, separated by an underscore between words).
- Do not use global variables as much as possible, if you must use, it must be prefixed g, and must be long enough to avoid name clashes.
- Global variables using all lowercase underscore separated nomenclature.
- Local variable names separated by an underscore to use all lowercase nomenclature.
- If the local variable scope is small, it can be suitably used abbreviations.
- Global static variables s_ as a prefix. Static class members to use s prefix underscore-separated lowercase nomenclature.
- const constants and enumerations constants are separated by an underscore all uppercase nomenclature
- Variables determine the need of different sizes, the length of clear plastic type recommended <stdint.h> defined, e.g. int64_t, int32_t like.
Multi-line comments must be written at the top is interpreted content. Single-line comments can be written above or to the right of the comment statement.
- You should only declare a variable within his party.
- Local variables should be delayed until the first use of the statement.
- Local variables should be given initial statement, the pointer type of the local variables must be declared at the initial value.
- This specification does not apply outside the company for the introduction of third-party code, third-party code to comply with its own coding style is modified.
By explicitly or implicitly like macro definitions derived from the third-party code affirmed classes, corresponding to the third-party code itself must comply with the coding style.