Guidelines for the Use of the C++14 Language in Critical and Safety-Related Systems AUTOSAR AP Release 17-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 17-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 17-03 Document Change History Date Release Changed by Description AUTOSAR 2017-03-31 17-03 Release • Initial release Management 1 of 371 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 17-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 371 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 17-03 Table of Contents 1 Background7 2 The vision8 2.1 Rationale for the production of AUTOSAR C++14............8 2.2 Objectives of AUTOSAR C++14......................8 3 Scope 10 3.1 Allowed features of C++ language..................... 10 3.2 Limitations.................................. 13 4 Using AUTOSAR C++14 14 5 Introduction to the rules 15 5.1 Rule classification.............................. 15 5.1.1 Rule classification according to compatibility with MISRA.. 15 5.1.2 Rule classification according to obligation level........ 15 5.1.3 Rule classification according to enforcement by static analysis 15 5.1.4 Rule classification according to allocated target........ 16 5.2 Organization of rules............................ 16 5.3 Exceptions to the rules........................... 16 5.4 Redundancy in the rules.......................... 16 5.5 Presentation of rules............................ 17 5.6 Understanding the issue references.................... 17 5.7 Scope of rules................................ 17 6 AUTOSAR C++14 coding rules 18 6.0 Language independent issues....................... 18 6.0.1 Unnecessary constructs..................... 18 6.0.2 Storage.............................. 23 6.0.3 Runtime failures......................... 23 6.0.4 Arithmetic............................. 24 6.1 General................................... 26 6.1.1 Scope............................... 26 6.1.2 Normative references...................... 28 6.1.4 Implementation compliance................... 29 6.2 Lexical conventions............................. 30 6.2.3 Character sets.......................... 30 6.2.5 Trigraph sequences....................... 31 6.2.6 Alternative tokens........................ 31 6.2.8 Comments............................ 32 6.2.9 Header names.......................... 36 6.2.11 Identifiers............................. 36 6.2.14 Literals.............................. 41 6.3 Basic concepts............................... 44 3 of 371 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 17-03 6.3.1 Declarations and definitions................... 44 6.3.2 One Definition Rule....................... 46 6.3.3 Scope............................... 47 6.3.4 Name lookup........................... 51 6.3.9 Types............................... 51 6.4 Standard conversions........................... 52 6.4.5 Integral promotions....................... 52 6.4.7 Integral conversion........................ 55 6.4.10 Pointer conversions....................... 57 6.5 Expressions................................. 58 6.5.0 General.............................. 58 6.5.1 Primary expression....................... 67 6.5.2 Postfix expressions....................... 75 6.5.3 Unary expressions........................ 83 6.5.6 Multiplicative operators..................... 84 6.5.8 Shift operators.......................... 85 6.5.10 Equality operators........................ 86 6.5.14 Logical AND operator...................... 86 6.5.16 Conditional operator....................... 87 6.5.18 Assignment and compound assignment operation...... 87 6.5.19 Comma operator......................... 87 6.5.20 Constant expression....................... 88 6.6 Statements................................. 88 6.6.2 Expression statement...................... 88 6.6.3 Compound statement or block................. 89 6.6.4 Selection statements...................... 89 6.6.5 Iteration statements....................... 91 6.6.6 Jump statements......................... 94 6.7 Declaration................................. 96 6.7.1 Specifiers............................. 96 6.7.2 Enumeration declaration.................... 104 6.7.3 Namespaces........................... 109 6.7.4 The asm declaration....................... 109 6.7.5 Linkage specification...................... 111 6.8 Declarators................................. 115 6.8.0 General.............................. 115 6.8.2 Ambiguity resolution....................... 115 6.8.3 Meaning of declarators..................... 116 6.8.4 Function definitions....................... 116 6.8.5 Initilizers............................. 118 6.9 Classes................................... 125 6.9.3 Member function......................... 125 6.9.5 Unions.............................. 128 6.9.6 Bit-fields............................. 128 6.10 Derived Classes............................... 130 6.10.1 Multiple base Classes...................... 130 4 of 371 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 17-03 6.10.2 Member name lookup...................... 132 6.10.3 Virtual functions......................... 133 6.11 Member access control........................... 139 6.11.0 General.............................. 139 6.11.3 Friends.............................. 142 6.12 Special member functions......................... 142 6.12.0 General.............................. 142 6.12.1 Constructors........................... 143 6.12.4 Destructors............................ 149 6.12.6 Initialization............................ 151 6.12.7 Construction and destructions................. 153 6.12.8 Copying and moving class objects............... 155 6.13 Overloading................................. 167 6.13.1 Overloadable declarations................... 167 6.13.2 Declaration matching...................... 170 6.13.3 Overload resolution....................... 173 6.13.5 Overloaded operators...................... 174 6.13.6 Build-in operators........................ 175 6.14 Templates.................................. 176 6.14.0 General.............................. 176 6.14.1 Template parameters...................... 176 6.14.5 Template declarations...................... 179 6.14.6 Name resolution......................... 179 6.14.7 Template instantiation and specialization........... 179 6.14.8 Function template specializations............... 181 6.15 Exception handling............................. 183 6.15.0 General.............................. 186 6.15.1 Throwing an exception..................... 201 6.15.2 Constructors and destructors.................. 211 6.15.3 Handling an exception...................... 215 6.15.4 Exception specifications..................... 225 6.15.5 Special functions......................... 234 6.16 Preprocessing directives.......................... 241 6.16.0 General.............................. 241 6.16.1 Conditional inclusion....................... 244 6.16.2 Source file inclusion....................... 245 6.16.3 Macro replacement....................... 247 6.16.6 Error directive.......................... 248 6.16.7 Pragma directive......................... 249 6.17 Library introduction - partial........................ 249 6.17.1 General.............................. 249 6.17.2 The C standard library...................... 251 6.17.3 Definitions............................ 252 6.18 Language support library - partial..................... 253 6.18.0 General.............................. 253 6.18.1 Types............................... 254 5 of 371 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 17-03 6.18.2 Implementation properties................... 258 6.18.5 Dynamic memory management................ 259 6.18.9 Other runtime support...................... 268 6.19 Diagnostics library - partial......................... 272 6.19.4 Error numbers.......................... 272 6.23 Containers library - partial........................