没有合适的资源?快使用搜索试试~ 我知道了~
C++ Coding Standard
需积分: 10 9 下载量 190 浏览量
2015-08-23
23:46:02
上传
评论
收藏 766KB PDF 举报
温馨提示
试读
155页
最新4.0版本编程规范,源自:http://www.codingstandard.com/。
资源推荐
资源详情
资源评论
Coding Standard
Version 4.0
www.codingstandard.com
3 October 2013
Programming Research Ltd
Mark House - 9/11 Queens Road - Hersham - Surrey KT12 5LU - United Kingdom
Tel: +44 (0) 1932 888 080 - Fax: +44 (0) 1932 888 081
info@programmingresearch.com - www.programmingresearch.com
Registered in England No. 2844401
© 2013 Programming Research Ltd
Contents
Contents
0 Introduction 7
0.1 Typographical Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
0.2 Escalation policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
0.3 Base Standard and Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
0.3.1 ISO Standard C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
0.3.2 Statically detectable restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
0.3.3 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
0.4 Basis of requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
0.5 Rule Enforcement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
0.6 Deviations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
0.7 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1 General 10
1.1 Implementation compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.1.1 Ensure that code complies with the 2011 ISO C++ Language Standard . . . . . . . . . . . . 10
1.2 Redundancy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.1 Ensure that all statements are reachable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.2 Ensure that no expression or sub-expression is redundant . . . . . . . . . . . . . . . . . . . . 11
1.3 Deprecated features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3.1 Do not use the increment operator (++) on a variable of type bool . . . . . . . . . . . . . . . 12
1.3.2 Do not use the register keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3.3 Do not use the C Standard Library .h headers . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.3.4 Do not use deprecated STL library features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.3.5 Do not use throw exception specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2 Lexical conventions 15
2.1 Character sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.1 Do not use tab characters in source files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2 Trigraph sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.2.1 Do not use digraphs or trigraphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.3 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.1 Do not use the C comment delimiters /* ... */ . . . . . . . . . . . . . . . . . . . . . . . . 16
2.3.2 Do not comment out code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.4 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4.1 Ensure that each identifier is distinct from any other visible identifier . . . . . . . . . . . . . . 18
2.5 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.5.1 Do not concatenate strings with different encoding prefixes . . . . . . . . . . . . . . . . . . . 18
2.5.2 Do not use octal constants (other than zero) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.5.3 Use nullptr for the null pointer constant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3 Basic concepts 21
3.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.1.1 Do not hide declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2 Program and linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2.1 Do not declare functions at block scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.3 Storage duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3.1 Do not use variables with static storage duration . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.4 Object lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.4.1 Do not return a reference or a pointer to an automatic variable defined within the function . . 23
3.4.2 Do not assign the address of a variable to a pointer with a greater lifetime . . . . . . . . . . . 24
© 2013 Programming Research Ltd 1
Contents
3.4.3 Use RAII for resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.5 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.5.1 Do not make any assumptions about the internal representation of a value or object . . . . . 27
4 Standard conversions 29
4.1 Array-to-pointer conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.1.1 Ensure that a function argument does not undergo an array-to-pointer conversion . . . . . . 29
4.2 Integral conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.2.1 Ensure that the U suffix is applied to a literal used in a context requiring an unsigned integral
expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4.2.2 Ensure that data loss does not demonstrably occur in an integral expression . . . . . . . . . 30
4.3 Floating point conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.3.1 Do not convert an expression of wider floating point type to a narrower floating point type . . 32
4.4 Floating-integral conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
4.4.1 Do not convert floating values to integral types except through use of standard library functions 33
5 Expressions 34
5.1 Primary expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.1.1 Use symbolic names instead of literal values in code . . . . . . . . . . . . . . . . . . . . . . . 34
5.1.2 Do not rely on the sequence of evaluation within an expression . . . . . . . . . . . . . . . . . 34
5.1.3 Use parentheses in expressions to specify the intent of the expression . . . . . . . . . . . . . 35
5.1.4 Do not capture variables implicitly in a lambda . . . . . . . . . . . . . . . . . . . . . . . . . . 36
5.1.5 Include a (possibly empty) parameter list in every lambda expression . . . . . . . . . . . . . . 37
5.1.6 Do not code side effects into the right-hand operands of: &&, ||, sizeof, typeid or a function
passed to condition variable::wait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.2 Postfix expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.2.1 Ensure that pointer or array access is demonstrably within bounds of a valid object . . . . . . 39
5.2.2 Ensure that functions do not call themselves, either directly or indirectly . . . . . . . . . . . . 40
5.3 Unary expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
5.3.1 Do not apply unary minus to operands of unsigned type . . . . . . . . . . . . . . . . . . . . . 41
5.3.2 Allocate memory using new and release it using delete . . . . . . . . . . . . . . . . . . . . . 41
5.3.3 Ensure that the form of delete matches the form of new used to allocate the memory . . . . 42
5.4 Explicit type conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.4.1 Only use casting forms: static cast (excl. void*), dynamic cast or explicit constructor call 43
5.4.2 Do not cast an expression to an enumeration type . . . . . . . . . . . . . . . . . . . . . . . . 44
5.4.3 Do not convert from a base class to a derived class . . . . . . . . . . . . . . . . . . . . . . . 45
5.5 Multiplicative operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.5.1 Ensure that the right hand operand of the division or remainder operators is demonstrably
non-zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.6 Shift operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.6.1 Do not use bitwise operators with signed operands . . . . . . . . . . . . . . . . . . . . . . . . 46
5.7 Equality operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.7.1 Do not write code that expects floating point calculations to yield exact results . . . . . . . . . 47
5.7.2 Ensure that a pointer to member that is a virtual function is only compared (==) with nullptr 47
5.8 Conditional operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.8.1 Do not use the conditional operator (?:) as a sub-expression . . . . . . . . . . . . . . . . . . 48
6 Statements 49
6.1 Selection statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.1.1 Enclose the body of a selection or an iteration statement in a compound statement . . . . . . 49
6.1.2 Explicitly cover all paths through multi-way selection statements . . . . . . . . . . . . . . . . 49
6.1.3 Ensure that a non-empty case statement block does not fall through to the next label . . . . . 50
6.1.4 Ensure that a switch statement has at least two case labels, distinct from the default label . . 50
© 2013 Programming Research Ltd 2
Contents
6.2 Iteration statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
6.2.1 Implement a loop that only uses element values as a range-based loop . . . . . . . . . . . . 52
6.2.2 Ensure that a loop has a single loop counter, an optional control variable, and is not degenerate 53
6.2.3 Do not alter a control or counter variable more than once in a loop . . . . . . . . . . . . . . . 54
6.2.4 Only modify a for loop counter in the for expression . . . . . . . . . . . . . . . . . . . . . . . . 54
6.3 Jump statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.3.1 Ensure that the label(s) for a jump statement or a switch condition appear later, in the same
or an enclosing block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.3.2 Ensure that execution of a function with a non-void return type ends in a return statement
with a value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
6.4 Declaration statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
6.4.1 Postpone variable definitions as long as possible . . . . . . . . . . . . . . . . . . . . . . . . . 57
7 Declarations 59
7.1 Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.1.1 Declare each identifier on a separate line in a separate declaration . . . . . . . . . . . . . . . 59
7.1.2 Use const whenever possible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.1.3 Do not place type specifiers before non-type specifiers in a declaration . . . . . . . . . . . . . 60
7.1.4 Place CV-qualifiers on the right hand side of the type they apply to . . . . . . . . . . . . . . . 61
7.1.5 Do not inline large functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
7.1.6 Use class types or typedefs to abstract scalar quantities and standard integer types . . . . . 62
7.1.7 Use a trailing return type in preference to type disambiguation using typename . . . . . . . . 63
7.1.8 Use auto id = expr when declaring a variable to have the same type as its initializer func-
tion call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
7.1.9 Do not explicitly specify the return type of a lambda . . . . . . . . . . . . . . . . . . . . . . . 65
7.1.10 Use static assert for assertions involving compile time constants . . . . . . . . . . . . . . 65
7.2 Enumeration declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
7.2.1 Use an explicit enumeration base and ensure that it is large enough to store all enumerators 66
7.2.2 Initialize none, the first only or all enumerators in an enumeration . . . . . . . . . . . . . . . . 67
7.3 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.3.1 Do not use using directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.4 Linkage specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.4.1 Ensure that any objects, functions or types to be used from a single translation unit are
defined in an unnamed namespace in the main source file . . . . . . . . . . . . . . . . . . . . 68
7.4.2 Ensure that an inline function, a function template, or a type used from multiple translation
units is defined in a single header file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
7.4.3 Ensure that an object or a function used from multiple translation units is declared in a single
header file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
7.5 The asm declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
7.5.1 Do not use the asm declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
8 Definitions 71
8.1 Type names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8.1.1 Do not use multiple levels of pointer indirection . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8.2 Meaning of declarators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
8.2.1 Make parameter names absent or identical in all declarations . . . . . . . . . . . . . . . . . . 71
8.2.2 Do not declare functions with an excessive number of parameters . . . . . . . . . . . . . . . 72
8.2.3 Pass small objects with a trivial copy constructor by value . . . . . . . . . . . . . . . . . . . . 73
8.2.4 Do not pass std::unique ptr by const reference . . . . . . . . . . . . . . . . . . . . . . . . 73
8.3 Function definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
8.3.1 Do not write functions with an excessive McCabe Cyclomatic Complexity . . . . . . . . . . . 74
8.3.2 Do not write functions with a high static program path count . . . . . . . . . . . . . . . . . . . 75
8.3.3 Do not use default arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
© 2013 Programming Research Ltd 3
Contents
8.3.4 Define =delete functions with parameters of type rvalue reference to const . . . . . . . . . . 76
8.4 Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
8.4.1 Do not access an invalid object or an object with indeterminate value . . . . . . . . . . . . . . 76
8.4.2 Ensure that a braced aggregate initializer matches the layout of the aggregate object . . . . . 78
9 Classes 79
9.1 Member functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.1.1 Declare static any member function that does not require this. Alternatively, declare const
any member function that does not modify the externally visible state of the object . . . . . . 79
9.1.2 Make default arguments the same or absent when overriding a virtual function . . . . . . . . 79
9.1.3 Do not return non-const handles to class data from const member functions . . . . . . . . . . 80
9.1.4 Do not write member functions which return non-const handles to data less accessible than
the member function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
9.1.5 Do not introduce virtual functions in a final class . . . . . . . . . . . . . . . . . . . . . . . . . 82
9.2 Bit-fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
9.2.1 Declare bit-fields with an explicitly unsigned integral or enumeration type . . . . . . . . . . . 83
10 Derived classes 84
10.1 Multiple base classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
10.1.1 Ensure that access to base class subobjects does not require explicit disambiguation . . . . 84
10.2 Virtual functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
10.2.1 Use the override special identifier when overriding a virtual function . . . . . . . . . . . . . . 85
10.3 Abstract classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
10.3.1 Ensure that a derived class has at most one base class which is not an interface class . . . . 86
11 Member access control 88
11.1 Access specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
11.1.1 Declare all data members private . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
11.2 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
11.2.1 Do not use friend declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
12 Special member functions 91
12.1 Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
12.1.1 Do not declare implicit user defined conversions . . . . . . . . . . . . . . . . . . . . . . . . . 91
12.2 Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
12.2.1 Declare virtual, private or protected the destructor of a type used as a base class . . . . 91
12.3 Free store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
12.3.1 Correctly declare overloads for operator new and delete . . . . . . . . . . . . . . . . . . . . 92
12.4 Initializing bases and members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
12.4.1 Do not use the dynamic type of an object unless the object is fully constructed . . . . . . . . 93
12.4.2 Ensure that a constructor initializes explicitly all base classes and non-static data members . 94
12.4.3 Do not specify both an NSDMI and a member initializer in a constructor for the same non
static member . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
12.4.4 Write members in an initialization list in the order in which they are declared . . . . . . . . . . 96
12.4.5 Use delegating constructors to reduce code duplication . . . . . . . . . . . . . . . . . . . . . 96
12.5 Copying and moving class objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
12.5.1 Define explicitly =default or =delete implicit special member functions of concrete classes . 98
12.5.2 Define special members =default if the behavior is equivalent . . . . . . . . . . . . . . . . . 99
12.5.3 Ensure that a user defined move/copy constructor only moves/copies base and member
objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
12.5.4 Declare noexcept the move constructor and move assignment operator . . . . . . . . . . . . 101
12.5.5 Correctly reset moved-from handles to resources in the move constructor . . . . . . . . . . . 102
© 2013 Programming Research Ltd 4
剩余154页未读,继续阅读
资源评论
特仑苏_T
- 粉丝: 10
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- SW3518S全协议快充USB Type-c接口电源模块硬件参考设计评估版硬件(原理图 +pcb)+封装库文件.zip
- 基于深度强化学习算法实现多星对区域目标观测的规划python源码+数据集+模型+超详细注释.zip
- RT1052+SDRAM(IS42S16160) +SIM7600CE(PCIE接口封装)控制板硬件(原理图+PCB)+封装库
- 2017大学英语四级词汇-excel-列表版(精品文档).xls
- 2017版国家医保药品目录(excel版)完整版.xls
- 基于STM32F103单片机设计的无刷电机控制板硬件(原理图+PCB+BOM)+MCU软件控制源码+文档资料.zip
- 肺结节检测数据集VOC+YOLO格式1186张1类别.zip
- Faster-RCNN基于知识蒸馏的目标检测模型增量深度学习方法python源码+项目运行说明.zip
- 2018年考勤表——excel版.xls
- 基于Labview的转子动平衡测控系统源码+系统软件说明书.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功