Guidelines for the Use of the C++14 Language in Critical and Safety-Related Systems AUTOSAR AP Release 18-03
Total Page:16
File Type:pdf, Size:1020Kb
Guidelines for the use of the C++14 language in critical and safety-related systems AUTOSAR AP Release 18-03 Guidelines for the use of the Document Title C++14 language in critical and safety-related systems Document Owner AUTOSAR Document Responsibility AUTOSAR Document Identification No 839 Document Status Final Part of AUTOSAR Standard Adaptive Platform Part of Standard Release 18-03 Document Change History Date Release Changed by Description • New rules resulting from the analysis of JSF, HIC, CERT, C++ Core Guideline AUTOSAR • Improvements of already existing 2018-03-29 18-03 Release rules, more details in the Changelog Management (D.2) • Covered smart pointers usage • Reworked checked/unchecked exception definitions and rules • Updated traceability for HIC, CERT, AUTOSAR C++ Core Guideline 2017-10-27 17-10 Release • Partially included MISRA review of Management the 2017-03 release • Changes and fixes for existing rules, more details in the Changelog (D.1) AUTOSAR 2017-03-31 17-03 Release • Initial release Management 1 of 491 Document ID 839: AUTOSAR_RS_CPP14Guidelines — AUTOSAR CONFIDENTIAL — Guidelines for the use of the C++14 language in critical and safety-related systems AUTOSAR AP Release 18-03 Disclaimer This work (specification and/or software implementation) and the material contained in it, as released by AUTOSAR, is for the purpose of information only. AUTOSAR and the companies that have contributed to it shall not be liable for any use of the work. The material contained in this work is protected by copyright and other types of intellectual property rights. The commercial exploitation of the material contained in this work requires a license to such intellectual property rights. This work may be utilized or reproduced without any modification, in any form or by any means, for informational purposes only. For any other purpose, no part of the work may be utilized or reproduced, in any form or by any means, without permission in writing from the publisher. The work has been developed for automotive applications only. It has neither been developed, nor tested for non-automotive applications. The word AUTOSAR and the AUTOSAR logo are registered trademarks. 2 of 491 Document ID 839: AUTOSAR_RS_CPP14Guidelines — AUTOSAR CONFIDENTIAL — Guidelines for the use of the C++14 language in critical and safety-related systems AUTOSAR AP Release 18-03 Contents 1 Background8 2 The vision9 2.1 Rationale for the production of AUTOSAR C++14............9 2.2 Objectives of AUTOSAR C++14......................9 3 Scope 11 3.1 Allowed features of C++ language..................... 11 3.2 Limitations.................................. 14 4 Using AUTOSAR C++14 15 5 Introduction to the rules 16 5.1 Rule classification.............................. 16 5.1.1 Rule classification according to compatibility with MISRA.. 16 5.1.2 Rule classification according to obligation level........ 16 5.1.3 Rule classification according to enforcement by static analysis 16 5.1.4 Rule classification according to allocated target........ 17 5.2 Organization of rules............................ 17 5.3 Exceptions to the rules........................... 17 5.4 Redundancy in the rules.......................... 17 5.5 Presentation of rules............................ 18 5.6 Understanding the issue references.................... 18 5.7 Scope of rules................................ 18 6 AUTOSAR C++14 coding rules 19 6.0 Language independent issues....................... 19 6.0.1 Unnecessary constructs..................... 19 6.0.2 Storage.............................. 28 6.0.3 Runtime failures......................... 28 6.0.4 Arithmetic............................. 29 6.1 General................................... 32 6.1.1 Scope............................... 32 6.1.2 Normative references...................... 35 6.1.4 Implementation compliance................... 35 6.2 Lexical conventions............................. 36 6.2.3 Character sets.......................... 36 6.2.5 Alternative tokens........................ 37 6.2.7 Comments............................ 39 6.2.8 Header names.......................... 42 6.2.10 Identifiers............................. 43 6.2.11 Keywords............................. 48 6.2.13 Literals.............................. 48 6.3 Basic concepts............................... 53 3 of 491 Document ID 839: AUTOSAR_RS_CPP14Guidelines — AUTOSAR CONFIDENTIAL — Guidelines for the use of the C++14 language in critical and safety-related systems AUTOSAR AP Release 18-03 6.3.1 Declarations and definitions................... 53 6.3.2 One Definition Rule....................... 58 6.3.3 Scope............................... 59 6.3.4 Name lookup........................... 63 6.3.8 Object lifetime.......................... 63 6.3.9 Types............................... 66 6.4 Standard conversions........................... 67 6.4.5 Integral promotions....................... 67 6.4.7 Integral conversion........................ 70 6.4.10 Pointer conversions....................... 72 6.5 Expressions................................. 73 6.5.0 General.............................. 73 6.5.1 Primary expression....................... 86 6.5.2 Postfix expressions....................... 94 6.5.3 Unary expressions........................ 103 6.5.5 Pointer-to-member........................ 107 6.5.6 Multiplicative operators..................... 109 6.5.8 Shift operators.......................... 110 6.5.10 Equality operators........................ 110 6.5.14 Logical AND operator...................... 111 6.5.16 Conditional operator....................... 111 6.5.18 Assignment and compound assignment operation...... 112 6.5.19 Comma operator......................... 112 6.5.20 Constant expression....................... 112 6.6 Statements................................. 112 6.6.2 Expression statement...................... 112 6.6.3 Compound statement or block................. 115 6.6.4 Selection statements...................... 115 6.6.5 Iteration statements....................... 118 6.6.6 Jump statements......................... 123 6.7 Declaration................................. 125 6.7.1 Specifiers............................. 125 6.7.2 Enumeration declaration.................... 135 6.7.3 Namespaces........................... 140 6.7.4 The asm declaration....................... 143 6.7.5 Linkage specification...................... 144 6.7.6 Attributes............................. 149 6.8 Declarators................................. 150 6.8.0 General.............................. 150 6.8.2 Ambiguity resolution....................... 150 6.8.3 Meaning of declarators..................... 151 6.8.4 Function definitions....................... 151 6.8.5 Initializers............................. 168 6.9 Classes................................... 177 6.9.3 Member function......................... 177 6.9.5 Unions.............................. 180 4 of 491 Document ID 839: AUTOSAR_RS_CPP14Guidelines — AUTOSAR CONFIDENTIAL — Guidelines for the use of the C++14 language in critical and safety-related systems AUTOSAR AP Release 18-03 6.9.6 Bit-fields............................. 181 6.10 Derived Classes............................... 183 6.10.0 General.............................. 183 6.10.1 Multiple base Classes...................... 184 6.10.2 Member name lookup...................... 186 6.10.3 Virtual functions......................... 187 6.10.4 Abstract Classes......................... 193 6.11 Member access control........................... 194 6.11.0 General.............................. 194 6.11.3 Friends.............................. 196 6.12 Special member functions......................... 197 6.12.0 General.............................. 197 6.12.1 Constructors........................... 202 6.12.4 Destructors............................ 209 6.12.6 Initialization............................ 212 6.12.7 Construction and destructions................. 214 6.12.8 Copying and moving class objects............... 216 6.13 Overloading................................. 229 6.13.1 Overloadable declarations................... 229 6.13.2 Declaration matching...................... 231 6.13.3 Overload resolution....................... 234 6.13.5 Overloaded operators...................... 235 6.13.6 Build-in operators........................ 239 6.14 Templates.................................. 240 6.14.0 General.............................. 240 6.14.1 Template parameters...................... 240 6.14.5 Template declarations...................... 243 6.14.6 Name resolution......................... 245 6.14.7 Template instantiation and specialization........... 245 6.14.8 Function template specializations............... 248 6.15 Exception handling............................. 250 6.15.0 General.............................. 253 6.15.1 Throwing an exception..................... 268 6.15.2 Constructors and destructors.................. 279 6.15.3 Handling an exception...................... 283 6.15.4 Exception specifications..................... 293 6.15.5 Special functions......................... 301 6.16 Preprocessing directives.......................... 310 6.16.0 General.............................. 310 6.16.1 Conditional inclusion....................... 313 6.16.2 Source file inclusion....................... 313 6.16.3 Macro replacement....................... 316 6.16.6 Error directive.......................... 316 6.16.7 Pragma directive......................... 317 6.17 Library introduction - partial........................ 318 6.17.1 General.............................