c++ international standard -...

1587
© ISO 2016 — All rights reserved ISO/IEC JTC1 SC22 WG21 N 4604 Date: 2016-07-12 ISO/IEC FDIS 14882 ISO/IEC JTC1 SC22 Secretariat: ANSI Programming Languages — C ++ Langages de programmation — C ++ Warning This document is not an ISO International Standard. It is distributed for review and comment. It is subject to change without notice and may not be referred to as an International Standard. Recipients of this draft are invited to submit, with their comments, notification of any relevant patent rights of which they are aware and to provide supporting documentation. Document type: Draft International Standard Document stage: (40) Enquiry Document Language: E

Upload: others

Post on 02-Apr-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

  • © ISO 2016 — All rights reserved

    ISO/IEC JTC1 SC22 WG21 N4604Date: 2016-07-12

    ISO/IEC FDIS 14882ISO/IEC JTC1 SC22

    Secretariat: ANSI

    Programming Languages — C++Langages de programmation — C++

    Warning

    This document is not an ISO International Standard. It is distributed for review and comment. It is subjectto change without notice and may not be referred to as an International Standard.

    Recipients of this draft are invited to submit, with their comments, notification of any relevant patent rightsof which they are aware and to provide supporting documentation.

    Document type: Draft International StandardDocument stage: (40) EnquiryDocument Language: E

  • © ISO/IEC N4604

    Copyright notice

    All rights reserved. Unless otherwise specified, no part of this publication may be reproduced or utilizedotherwise in any form or by any means, electronic or mechanical, including photocopying, or posting on theinternet or an intranet, without prior written permission. Permission can be requested from either ISO atthe address below or ISO’s member body in the country of the requester.

    ISO copyright officeCase postale 56, CH-1211 Geneva 20Tel. + 41 22 749 01 11Fax + 41 22 749 09 47E-mail [email protected] www.iso.org

    ii

  • © ISO/IEC N4604

    ContentsContents iii

    List of Tables xi

    List of Figures xv

    1 General 11.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Normative references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Terms and definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.4 Implementation compliance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.5 Structure of this International Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.6 Syntax notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51.7 The C++ memory model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.8 The C++ object model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.9 Program execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81.10 Multi-threaded executions and data races . . . . . . . . . . . . . . . . . . . . . . . . . . . 121.11 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    2 Lexical conventions 182.1 Separate translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182.2 Phases of translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182.3 Character sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.4 Preprocessing tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202.5 Alternative tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212.6 Tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222.7 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222.8 Header names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222.9 Preprocessing numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.10 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.11 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.12 Operators and punctuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242.13 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    3 Basic concepts 353.1 Declarations and definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353.2 One-definition rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.3 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.4 Name lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473.5 Program and linkage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 603.6 Start and termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633.7 Storage duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673.8 Object lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713.9 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 743.10 Lvalues and rvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813.11 Alignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

    Contents iii

  • © ISO/IEC N4604

    4 Standard conversions 844.1 Lvalue-to-rvalue conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854.2 Array-to-pointer conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 854.3 Function-to-pointer conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864.4 Temporary materialization conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864.5 Qualification conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864.6 Integral promotions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874.7 Floating point promotion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874.8 Integral conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874.9 Floating point conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884.10 Floating-integral conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884.11 Pointer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884.12 Pointer to member conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 884.13 Function pointer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894.14 Boolean conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 894.15 Integer conversion rank . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

    5 Expressions 915.1 Primary expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 945.2 Postfix expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065.3 Unary expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1185.4 Explicit type conversion (cast notation) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1275.5 Pointer-to-member operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1285.6 Multiplicative operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1295.7 Additive operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1305.8 Shift operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1305.9 Relational operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1315.10 Equality operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1325.11 Bitwise AND operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1335.12 Bitwise exclusive OR operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1335.13 Bitwise inclusive OR operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1335.14 Logical AND operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1335.15 Logical OR operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1345.16 Conditional operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1345.17 Throwing an exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1355.18 Assignment and compound assignment operators . . . . . . . . . . . . . . . . . . . . . . . 1365.19 Comma operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1375.20 Constant expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

    6 Statements 1426.1 Labeled statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1436.2 Expression statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1436.3 Compound statement or block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1436.4 Selection statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1436.5 Iteration statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1466.6 Jump statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1486.7 Declaration statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1506.8 Ambiguity resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

    7 Declarations 1537.1 Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

    Contents iv

  • © ISO/IEC N4604

    7.2 Enumeration declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1737.3 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1777.4 The asm declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1907.5 Linkage specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1907.6 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

    8 Declarators 2008.1 Type names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2018.2 Ambiguity resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2028.3 Meaning of declarators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2038.4 Function definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2158.5 Decomposition declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2198.6 Initializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

    9 Classes 2379.1 Class names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2409.2 Class members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2419.3 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2529.4 Local class declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

    10 Derived classes 25610.1 Multiple base classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25710.2 Member name lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25910.3 Virtual functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26210.4 Abstract classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

    11 Member access control 26911.1 Access specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27011.2 Accessibility of base classes and base class members . . . . . . . . . . . . . . . . . . . . . . 27211.3 Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27411.4 Protected member access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27711.5 Access to virtual functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27811.6 Multiple access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27911.7 Nested classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

    12 Special member functions 28012.1 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28012.2 Temporary objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28312.3 Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28512.4 Destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28812.5 Free store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29112.6 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29312.7 Construction and destruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30112.8 Copying and moving class objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

    13 Overloading 31213.1 Overloadable declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31213.2 Declaration matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31413.3 Overload resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31513.4 Address of overloaded function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33613.5 Overloaded operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

    Contents v

  • © ISO/IEC N4604

    13.6 Built-in operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

    14 Templates 34514.1 Template parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34614.2 Names of template specializations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35014.3 Template arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35114.4 Type equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35714.5 Template declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35814.6 Name resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37614.7 Template instantiation and specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . 39214.8 Function template specializations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40514.9 Deduction guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

    15 Exception handling 42715.1 Throwing an exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42815.2 Constructors and destructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43015.3 Handling an exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43015.4 Exception specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43215.5 Special functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

    16 Preprocessing directives 44016.1 Conditional inclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44116.2 Source file inclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44316.3 Macro replacement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44416.4 Line control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45016.5 Error directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45016.6 Pragma directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45016.7 Null directive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45016.8 Predefined macro names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45016.9 Pragma operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452

    17 Library introduction 45317.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45317.2 The C standard library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45417.3 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45417.4 Additional definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45717.5 Method of description (Informative) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45717.6 Library-wide requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46217.7 Header synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483

    18 Language support library 48518.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48518.2 Common definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48518.3 Implementation properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48618.4 Integer types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49618.5 Start and termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49718.6 Dynamic memory management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49818.7 Type identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50618.8 Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50918.9 Initializer lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51318.10 Other runtime support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514

    Contents vi

  • © ISO/IEC N4604

    19 Diagnostics library 51719.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51719.2 Exception classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51719.3 Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52119.4 Error numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52119.5 System error support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

    20 General utilities library 53420.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53420.2 Utility components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53420.3 Compile-time integer sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54020.4 Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54020.5 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54520.6 Optional objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55620.7 Variants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56720.8 Storage for any type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58120.9 Class template bitset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58620.10 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59320.11 Smart pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60820.12 Memory resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63420.13 Class template scoped_allocator_adaptor . . . . . . . . . . . . . . . . . . . . . . . . . . 64620.14 Function objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65220.15 Metaprogramming and type traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67620.16 Compile-time rational arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70120.17 Time utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70320.18 Class type_index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72020.19 Execution policies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 722

    21 Strings library 72421.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72421.2 Character traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72421.3 String classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73021.4 String view classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76421.5 Null-terminated sequence utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774

    22 Localization library 77922.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77922.2 Header synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77922.3 Locales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78022.4 Standard locale categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79222.5 Standard code conversion facets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83122.6 C library locales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833

    23 Containers library 83423.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83423.2 Container requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83723.3 Sequence containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87123.4 Associative containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90223.5 Unordered associative containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92223.6 Container adaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 942

    Contents vii

  • © ISO/IEC N4604

    24 Iterators library 95224.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95224.2 Iterator requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95224.3 Header synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95724.4 Iterator primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96024.5 Iterator adaptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96324.6 Stream iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97724.7 Range access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98324.8 Container access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 985

    25 Algorithms library 98625.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98625.2 Parallel algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100525.3 Non-modifying sequence operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100825.4 Mutating sequence operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101425.5 Sorting and related operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102325.6 C library algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1036

    26 Numerics library 103726.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103726.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103726.3 Numeric type requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103726.4 The floating-point environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103826.5 Complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103926.6 Random number generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104926.7 Numeric arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109326.8 Generalized numeric operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111426.9 Mathematical functions for floating point types . . . . . . . . . . . . . . . . . . . . . . . . 1123

    27 Input/output library 114127.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114127.2 Iostreams requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114227.3 Forward declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114227.4 Standard iostream objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114427.5 Iostreams base classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114627.6 Stream buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116427.7 Formatting and manipulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117327.8 String-based streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120027.9 File-based streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121127.10 File systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122427.11 C library files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1275

    28 Regular expressions library 127928.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127928.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127928.3 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128028.4 Header synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128228.5 Namespace std::regex_constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128928.6 Class regex_error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129228.7 Class template regex_traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129228.8 Class template basic_regex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1296

    Contents viii

  • © ISO/IEC N4604

    28.9 Class template sub_match . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130128.10 Class template match_results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130728.11 Regular expression algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131228.12 Regular expression iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131828.13 Modified ECMAScript regular expression grammar . . . . . . . . . . . . . . . . . . . . . . 1324

    29 Atomic operations library 132729.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132729.2 Header synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132729.3 Order and consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133029.4 Lock-free property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133229.5 Atomic types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133229.6 Operations on atomic types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133629.7 Flag type and operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134229.8 Fences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1343

    30 Thread support library 134530.1 General . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134530.2 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134530.3 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134830.4 Mutual exclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135330.5 Condition variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137330.6 Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1381

    A Grammar summary 1397A.1 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1397A.2 Lexical conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1397A.3 Basic concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1402A.4 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1402A.5 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1406A.6 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1407A.7 Declarators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1410A.8 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1412A.9 Derived classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1413A.10 Special member functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1414A.11 Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1414A.12 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1414A.13 Exception handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1415A.14 Preprocessing directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1415

    B Implementation quantities 1417

    C Compatibility 1419C.1 C++ and ISO C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1419C.2 C++ and ISO C++ 2003 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1428C.3 C++ and ISO C++ 2011 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1435C.4 C++ and ISO C++ 2014 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1437C.5 C standard library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1440

    D Compatibility features 1445D.1 Redeclaration of static constexpr data members . . . . . . . . . . . . . . . . . . . . . . 1445

    Contents ix

  • © ISO/IEC N4604

    D.2 Implicit declaration of copy functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1445D.3 Dynamic exception specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1445D.4 C standard library headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1445D.5 char* streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1446D.6 Violating exception-specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1455D.7 uncaught_exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1455D.8 Old adaptable function bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1455D.9 The default allocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1460D.10 Raw storage iterator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1462D.11 Temporary buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1463D.12 Deprecated Type Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1463D.13 Deprecated Iterator primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1464

    E Universal character names for identifier characters 1465E.1 Ranges of characters allowed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1465E.2 Ranges of characters disallowed initially . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1465

    Cross references 1466

    Index 1487

    Index of grammar productions 1519

    Index of library names 1523

    Index of implementation-defined behavior 1570

    Contents x

  • © ISO/IEC N4604

    List of Tables1 Alternative tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Identifiers with special meaning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Alternative representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 Types of integer literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Escape sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 String literal concatenations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

    8 Relations on const and volatile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80

    9 simple-type-specifiers and the types they specify . . . . . . . . . . . . . . . . . . . . . . . . . . 167

    10 Relationship between operator and function call notation . . . . . . . . . . . . . . . . . . . . . 32011 Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

    12 Value of folding empty sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365

    13 Library categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45314 C++ library headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46315 C++ headers for C library facilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46416 C standard Annex K names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46517 C++ headers for freestanding implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46518 EqualityComparable requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46619 LessThanComparable requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46720 DefaultConstructible requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46721 MoveConstructible requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46722 CopyConstructible requirements (in addition to MoveConstructible) . . . . . . . . . . . . . 46723 MoveAssignable requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46724 CopyAssignable requirements (in addition to MoveAssignable) . . . . . . . . . . . . . . . . . 46725 Destructible requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46726 NullablePointer requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46927 Hash requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47028 Descriptive variable definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47029 Allocator requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

    30 Language support library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

    31 Diagnostics library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

    32 General utilities library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53433 optional::operator=(const optional&) effects . . . . . . . . . . . . . . . . . . . . . . . . . 56034 optional::operator=(optional&&) effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56135 optional::swap(optional&) effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56236 Primary type category predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68437 Composite type category predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68538 Type property predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 685

    List of Tables xi

  • © ISO/IEC N4604

    39 Type property queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69240 Type relationship predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69341 Const-volatile modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69542 Reference modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69543 Sign modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69644 Array modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69745 Pointer modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69746 Other transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69847 Expressions used to perform ratio arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70248 Clock requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707

    49 Strings library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72450 Character traits requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72551 basic_string(const Allocator&) effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74052 basic_string(const basic_string&) effects . . . . . . . . . . . . . . . . . . . . . . . . . . . 74053 basic_string(const basic_string&, size_type, const Allocator&)

    and basic_string(const basic_string&, size_type, size_type, const Allocator&) effects 74054 basic_string(const charT*, size_type, const Allocator&) effects . . . . . . . . . . . . . 74155 basic_string(const charT*, const Allocator&) effects . . . . . . . . . . . . . . . . . . . . 74156 basic_string(size_t, charT, const Allocator&) effects . . . . . . . . . . . . . . . . . . . 74157 basic_string(const basic_string&, const Allocator&)

    and basic_string(basic_string&&, const Allocator&) effects . . . . . . . . . . . . . . . . 74258 operator=(const basic_string&) effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74259 compare() results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75660 basic_string_view(const charT*) effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76861 basic_string_view(const charT*, size_type) effects . . . . . . . . . . . . . . . . . . . . . 76862 compare() results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77063 Additional basic_string_view comparison overloads . . . . . . . . . . . . . . . . . . . . . . . 773

    64 Localization library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77965 Locale category facets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78366 Required specializations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78467 do_in/do_out result values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80168 do_unshift result values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80269 Integer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80570 Length modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80671 Integer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80972 Floating-point conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81073 Length modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81074 Numeric conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81075 Fill padding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81176 do_get_date effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81877 Potential setlocale data races . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 833

    78 Containers library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83479 Container types with compatible nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83480 Container requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83781 Reversible container requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84082 Optional container operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84183 Allocator-aware container requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84284 Sequence container requirements (in addition to container) . . . . . . . . . . . . . . . . . . . . 844

    List of Tables xii

  • © ISO/IEC N4604

    85 Optional sequence container operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84786 Associative container requirements (in addition to container) . . . . . . . . . . . . . . . . . . . 84987 Unordered associative container requirements (in addition to container) . . . . . . . . . . . . . 860

    88 Iterators library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95289 Relations among iterator categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95290 Iterator requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95491 Input iterator requirements (in addition to Iterator) . . . . . . . . . . . . . . . . . . . . . . . . 95492 Output iterator requirements (in addition to Iterator) . . . . . . . . . . . . . . . . . . . . . . . 95593 Forward iterator requirements (in addition to input iterator) . . . . . . . . . . . . . . . . . . . 95694 Bidirectional iterator requirements (in addition to forward iterator) . . . . . . . . . . . . . . . . 95695 Random access iterator requirements (in addition to bidirectional iterator) . . . . . . . . . . . 957

    96 Algorithms library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986

    97 Numerics library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103798 Seed sequence requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105199 Uniform random bit generator requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1052100 Random number engine requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1053101 Random number distribution requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1056

    102 Input/output library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1141103 fmtflags effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1151104 fmtflags constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1151105 iostate effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1151106 openmode effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152107 seekdir effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1152108 Position type requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1156109 basic_ios::init() effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1158110 basic_ios::copyfmt() effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1160111 seekoff positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1205112 newoff values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1205113 File open modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1215114 seekoff effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1217115 filesystem_error(const string&, error_code) effects . . . . . . . . . . . . . . . . . . . . . 1250116 filesystem_error(const string&, const path&, error_code) effects . . . . . . . . . . . . 1250117 filesystem_error(const string&, const path&, const path&, error_code) effects . . . 1250118 Enum class file_type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1251119 Enum class copy_options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1252120 Enum class perms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1253121 Enum class directory_options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1253122 absolute(const path&, const path&) return value . . . . . . . . . . . . . . . . . . . . . . . 1261123 Effects of permission bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1270

    124 Regular expressions library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1279125 Regular expression traits class requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1280126 syntax_option_type effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1290127 regex_constants::match_flag_type effects when obtaining a match against a character con-

    tainer sequence [first, last). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1291128 error_type values in the C locale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1292129 Character class names and corresponding ctype masks . . . . . . . . . . . . . . . . . . . . . . . 1295

    List of Tables xiii

  • © ISO/IEC N4604

    130 match_results assignment operator effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1309131 Effects of regex_match algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1313132 Effects of regex_search algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1315

    133 Atomics library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1327134 Named atomic types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1336135 Atomic typedefs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1337136 Atomic arithmetic computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1341

    137 Thread support library summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1345

    138 Standard macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1441139 Standard values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1441140 Standard types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1441141 Standard structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1441142 Standard functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1442

    143 C headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1445144 strstreambuf(streamsize) effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1447145 strstreambuf(void* (*)(size_t), void (*)(void*)) effects . . . . . . . . . . . . . . . . . 1448146 strstreambuf(charT*, streamsize, charT*) effects . . . . . . . . . . . . . . . . . . . . . . . 1448147 seekoff positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1450148 newoff values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1451

    List of Tables xiv

  • © ISO/IEC N4604

    List of Figures1 Expression category taxonomy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

    2 Directed acyclic graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2573 Non-virtual base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2584 Virtual base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2595 Virtual and non-virtual base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2596 Name lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

    7 Stream position, offset, and size types [non-normative] . . . . . . . . . . . . . . . . . . . . . . . 1141

    List of Figures xv

  • © ISO/IEC N4604

    1 General [intro]1.1 Scope [intro.scope]

    1 This International Standard specifies requirements for implementations of the C++ programming language.The first such requirement is that they implement the language, and so this International Standard alsodefines C++. Other requirements and relaxations of the first requirement appear at various places within thisInternational Standard.

    2 C++ is a general purpose programming language based on the C programming language as described inISO/IEC 9899:1999 Programming languages — C (hereinafter referred to as the C standard). In addition tothe facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces,operator overloading, function name overloading, references, free store management operators, and additionallibrary facilities.

    1.2 Normative references [intro.refs]1 The following referenced documents are indispensable for the application of this document. For dated

    references, only the edition cited applies. For undated references, the latest edition of the referenced document(including any amendments) applies.

    —(1.1) Ecma International, ECMAScript Language Specification, Standard Ecma-262, third edition, 1999.

    —(1.2) ISO/IEC 2382 (all parts), Information technology — Vocabulary

    —(1.3) ISO/IEC 9899:2011, Programming languages — C

    —(1.4) ISO/IEC 9899:2011/Cor.1:2012(E), Programming languages — C, Technical Corrigendum 1

    —(1.5) ISO/IEC 9945:2003, Information Technology — Portable Operating System Interface (POSIX)

    —(1.6) ISO/IEC 10646-1:1993, Information technology — Universal Multiple-Octet Coded Character Set (UCS)— Part 1: Architecture and Basic Multilingual Plane

    —(1.7) ISO 80000-2:2009, Quantities and units — Part 2: Mathematical signs and symbols to be used in thenatural sciences and technology

    2 The library described in Clause 7 of ISO/IEC 9899:2011 is hereinafter called the C standard library.1

    3 The operating system interface described in ISO/IEC 9945:2003 is hereinafter called POSIX .4 The ECMAScript Language Specification described in Standard Ecma-262 is hereinafter called ECMA-262 .

    1.3 Terms and definitions [intro.defs]1 For the purposes of this document, the terms and definitions given in ISO/IEC 2382-1:1993 and the terms,

    definitions, and symbols given in ISO 80000-2:2009 apply, as do the following definitions.2 17.3 defines additional terms that are used only in Clauses 17 through 30 and Annex D.3 Terms that are used only in a small portion of this International Standard are defined where they are used

    and italicized where they are defined.

    1) With the qualifications noted in Clauses 18 through 30 and in C.5, the C standard library is a subset of the C++ standardlibrary.

    § 1.3 1

  • © ISO/IEC N4604

    1.3.1 [defns.access]access〈execution-time action〉 to read or modify the value of an object

    1.3.2 [defns.argument]argument〈function call expression〉 expression in the comma-separated list bounded by the parentheses (5.2.2)

    1.3.3 [defns.argument.macro]argument〈function-like macro〉 sequence of preprocessing tokens in the comma-separated list bounded by the parenthe-ses (16.3)

    1.3.4 [defns.argument.throw]argument〈throw expression〉 the operand of throw (5.17)

    1.3.5 [defns.argument.templ]argument〈template instantiation〉 constant-expression, type-id, or id-expression in the comma-separated list boundedby the angle brackets (14.3)

    1.3.6 [defns.cond.supp]conditionally-supportedprogram construct that an implementation is not required to support[Note: Each implementation documents all conditionally-supported constructs that it does not support.—endnote ]

    1.3.7 [defns.diagnostic]diagnostic messagemessage belonging to an implementation-defined subset of the implementation’s output messages

    1.3.8 [defns.dynamic.type]dynamic type〈glvalue〉 type of the most derived object (1.8) to which the glvalue refers[Example: if a pointer (8.3.1) p whose static type is “pointer to class B” is pointing to an object of classD, derived from B (Clause 10), the dynamic type of the expression *p is “D”. References (8.3.2) are treatedsimilarly. —end example ]

    1.3.9 [defns.dynamic.type.prvalue]dynamic type〈prvalue〉 static type of the prvalue expression

    1.3.10 [defns.ill.formed]ill-formed programprogram that is not well-formed (1.3.27)

    1.3.11 [defns.impl.defined]implementation-defined behaviorbehavior, for a well-formed program construct and correct data, that depends on the implementation andthat each implementation documents

    § 1.3.11 2

  • © ISO/IEC N4604

    1.3.12 [defns.impl.limits]implementation limitsrestrictions imposed upon programs by the implementation

    1.3.13 [defns.locale.specific]locale-specific behaviorbehavior that depends on local conventions of nationality, culture, and language that each implementationdocuments

    1.3.14 [defns.multibyte]multibyte charactersequence of one or more bytes representing a member of the extended character set of either the source orthe execution environment[Note: The extended character set is a superset of the basic character set (2.3).—end note ]

    1.3.15 [defns.parameter]parameter〈function or catch clause〉 object or reference declared as part of a function declaration or definition or in thecatch clause of an exception handler that acquires a value on entry to the function or handler

    1.3.16 [defns.parameter.macro]parameter〈function-like macro〉 identifier from the comma-separated list bounded by the parentheses immediatelyfollowing the macro name

    1.3.17 [defns.parameter.templ]parameter〈template〉 template-parameter

    1.3.18 [defns.signature]signature〈function〉 name, parameter type list (8.3.5), and enclosing namespace (if any)[Note: Signatures are used as a basis for name mangling and linking.—end note ]

    1.3.19 [defns.signature.templ]signature〈function template〉 name, parameter type list (8.3.5), enclosing namespace (if any), return type, and templateparameter list

    1.3.20 [defns.signature.spec]signature〈function template specialization〉 signature of the template of which it is a specialization and its templatearguments (whether explicitly specified or deduced)

    1.3.21 [defns.signature.member]signature〈class member function〉 name, parameter type list (8.3.5), class of which the function is a member, cv-qualifiers(if any), and ref-qualifier (if any)

    1.3.22 [defns.signature.member.templ]signature〈class member function template〉 name, parameter type list (8.3.5), class of which the function is a member,cv-qualifiers (if any), ref-qualifier (if any), return type (if any), and template parameter list

    § 1.3.22 3

  • © ISO/IEC N4604

    1.3.23 [defns.signature.member.spec]signature〈class member function template specialization〉 signature of the member function template of which it is aspecialization and its template arguments (whether explicitly specified or deduced)

    1.3.24 [defns.static.type]static typetype of an expression (3.9) resulting from analysis of the program without considering execution semantics[Note: The static type of an expression depends only on the form of the program in which the expressionappears, and does not change while the program is executing. —end note ]

    1.3.25 [defns.undefined]undefined behaviorbehavior for which this International Standard imposes no requirements[Note: Undefined behavior may be expected when this International Standard omits any explicit definition ofbehavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behaviorranges from ignoring the situation completely with unpredictable results, to behaving during translation orprogram execution in a documented manner characteristic of the environment (with or without the issuanceof a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed.—end note ]

    1.3.26 [defns.unspecified]unspecified behaviorbehavior, for a well-formed program construct and correct data, that depends on the implementation[Note: The implementation is not required to document which behavior occurs. The range of possiblebehaviors is usually delineated by this International Standard. —end note ]

    1.3.27 [defns.well.formed]well-formed programC++ program constructed according to the syntax rules, diagnosable semantic rules, and the one-definitionrule (3.2).

    1.4 Implementation compliance [intro.compliance]1 The set of diagnosable rules consists of all syntactic and semantic rules in this International Standard except

    for those rules containing an explicit notation that “no diagnostic is required” or which are described asresulting in “undefined behavior.”

    2 Although this International Standard states only requirements on C++ implementations, those requirementsare often easier to understand if they are phrased as requirements on programs, parts of programs, orexecution of programs. Such requirements have the following meaning:

    —(2.1) If a program contains no violations of the rules in this International Standard, a conforming implemen-tation shall, within its resource limits, accept and correctly execute2 that program.

    —(2.2) If a program contains a violation of any diagnosable rule or an occurrence of a construct described inthis Standard as “conditionally-supported” when the implementation does not support that construct,a conforming implementation shall issue at least one diagnostic message.

    —(2.3) If a program contains a violation of a rule for which no diagnostic is required, this InternationalStandard places no requirement on implementations with respect to that program.

    2) “Correct execution” can include undefined behavior, depending on the data being processed; see 1.3 and 1.9.

    § 1.4 4

  • © ISO/IEC N4604

    3 For classes and class templates, the library Clauses specify partial definitions. Private members (Clause 11)are not specified, but each implementation shall supply them to complete the definitions according to thedescription in the library Clauses.

    4 For functions, function templates, objects, and values, the library Clauses specify declarations. Implementa-tions shall supply definitions consistent with the descriptions in the library Clauses.

    5 The names defined in the library have namespace scope (7.3). A C++ translation unit (2.2) obtains access tothese names by including the appropriate standard library header (16.2).

    6 The templates, classes, functions, and objects in the library have external linkage (3.5). The implementationprovides definitions for standard library entities, as necessary, while combining translation units to form acomplete C++ program (2.2).

    7 Two kinds of implementations are defined: a hosted implementation and a freestanding implementation. Fora hosted implementation, this International Standard defines the set of available libraries. A freestandingimplementation is one in which execution may take place without the benefit of an operating system, andhas an implementation-defined set of libraries that includes certain language-support libraries (17.6.1.3).

    8 A conforming implementation may have extensions (including additional library functions), provided they donot alter the behavior of any well-formed program. Implementations are required to diagnose programs thatuse such extensions that are ill-formed according to this International Standard. Having done so, however,they can compile and execute such programs.

    9 Each implementation shall include documentation that identifies all conditionally-supported constructs thatit does not support and defines all locale-specific characteristics.3

    1.5 Structure of this International Standard [intro.structure]1 Clauses 2 through 16 describe the C++ programming language. That description includes detailed syntactic

    specifications in a form described in 1.6. For convenience, Annex A repeats all such syntactic specifications.2 Clauses 18 through 30 and Annex D (the library clauses) describe the Standard C++ library. That description

    includes detailed descriptions of the templates, classes, functions, constants, and macros that constitute thelibrary, in a form described in Clause 17.

    3 Annex B recommends lower bounds on the capacity of conforming implementations.4 Annex C summarizes the evolution of C++ since its first published description, and explains in detail the

    differences between C++ and C. Certain features of C++ exist solely for compatibility purposes; Annex Ddescribes those features.

    5 Throughout this International Standard, each example is introduced by “[Example: ” and terminated by“—end example ]”. Each note is introduced by “[Note: ” and terminated by “—end note ]”. Examples andnotes may be nested.

    1.6 Syntax notation [syntax]1 In the syntax notation used in this International Standard, syntactic categories are indicated by italic type,

    and literal words and characters in constant width type. Alternatives are listed on separate lines except ina few cases where a long set of alternatives is marked by the phrase “one of.” If the text of an alternative istoo long to fit on a line, the text is continued on subsequent lines indented from the first one. An optionalterminal or non-terminal symbol is indicated by the subscript “opt”, so

    { expressionopt }

    indicates an optional expression enclosed in braces.2 Names for syntactic categories have generally been chosen according to the following rules:

    3) This documentation also defines implementation-defined behavior; see 1.9.

    § 1.6 5

  • © ISO/IEC N4604

    —(2.1) X-name is a use of an identifier in a context that determines its meaning (e.g., class-name, typedef-name).

    —(2.2) X-id is an identifier with no context-dependent meaning (e.g., qualified-id).

    —(2.3) X-seq is one or more X ’s without intervening delimiters (e.g., declaration-seq is a sequence of declara-tions).

    —(2.4) X-list is one or more X ’s separated by intervening commas (e.g., expression-list is a sequence ofexpressions separated by commas).

    1.7 The C++ memory model [intro.memory]1 The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain

    any member of the basic execution character set (2.3) and the eight-bit code units of the Unicode UTF-8encoding form and is composed of a contiguous sequence of bits, the number of which is implementation-defined. The least significant bit is called the low-order bit; the most significant bit is called the high-orderbit. The memory available to a C++ program consists of one or more sequences of contiguous bytes. Everybyte has a unique address.

    2 [Note: The representation of types is described in 3.9. —end note ]3 A memory location is either an object of scalar type or a maximal sequence of adjacent bit-fields all having non-

    zero width. [Note: Various features of the language, such as references and virtual functions, might involveadditional memory locations that are not accessible to programs but are managed by the implementation.—end note ] Two or more threads of execution (1.10) can access separate memory locations without interferingwith each other.

    4 [Note: Thus a bit-field and an adjacent non-bit-field are in separate memory locations, and therefore can beconcurrently updated by two threads of execution without interference. The same applies to two bit-fields,if one is declared inside a nested struct declaration and the other is not, or if the two are separated by azero-length bit-field declaration, or if they are separated by a non-bit-field declaration. It is not safe toconcurrently update two bit-fields in the same struct if all fields between them are also bit-fields of non-zerowidth. —end note ]

    5 [Example: A structure declared asstruct {char a;int b:5,c:11,:0,d:8;struct {int ee:8;} e;

    }

    contains four separate memory locations: The field a and bit-fields d and e.ee are each separate memorylocations, and can be modified concurrently without interfering with each other. The bit-fields b and ctogether constitute the fourth memory location. The bit-fields b and c cannot be concurrently modified, butb and a, for example, can be. —end example ]

    1.8 The C++ object model [intro.object]1 The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is

    created by a definition (3.1), by a new-expression (5.3.4), when implicitly changing the active member of aunion (9.3), or when a temporary object is created (4.4, 12.2). An object occupies a region of storage in itsperiod of construction (12.7), throughout its lifetime (3.8), and in its period of destruction (12.7). [Note:A function is not an object, regardless of whether or not it occupies storage in the way that objects do.

    § 1.8 6

  • © ISO/IEC N4604

    —end note ] The properties of an object are determined when the object is created. An object can have aname (Clause 3). An object has a storage duration (3.7) which influences its lifetime (3.8). An object hasa type (3.9). The term object type refers to the type with which the object is created. Some objects arepolymorphic (10.3); the implementation generates information associated with each such object that makes itpossible to determine that object’s type during program execution. For other objects, the interpretation ofthe values found therein is determined by the type of the expressions (Clause 5) used to access them.

    2 Objects can contain other objects, called subobjects. A subobject can be a member subobject (9.2), a baseclass subobject (Clause 10), or an array element. An object that is not a subobject of any other object iscalled a complete object. If an object is created in storage associated with a member subobject or arrayelement e (which may or may not be within its lifetime), the created object is a subobject of e’s containingobject if:

    —(2.1) the lifetime of e’s containing object has begun and not ended, and

    —(2.2) the storage for the new object exactly overlays the storage location associated with e, and

    —(2.3) the new object is of the same type as e (ignoring cv-qualification).

    [Note: If the subobject contains a reference member or a const subobject, the name of the original subobjectcannot be used to access the new object (3.8). —end note ] [Example:

    struct X { const int n; };union U { X x; float f; };void tong() {U u = {{ 1 }};u.f = 5.f; // OK, creates new subobject of u (9.3)X *p = new (&u.x) X {2}; // OK, creates new subobject of uassert(p->n == 2); // OKassert(*std::launder(&u.x.n) == 2); // OKassert(u.x.n == 2); // undefined behavior, u.x does not name new subobject

    }

    —end example ]3 If a complete object is created (5.3.4) in storage associated with another object e of type “array of N

    unsigned char”, that array provides storage for the created object if:

    —(3.1) the lifetime of e has begun and not ended, and

    —(3.2) the storage for the new object fits entirely within e, and

    —(3.3) there is no smaller array object that satisfies these constraints.

    [Note: If that portion of the array previously provided storage for another object, the lifetime of that objectends because its storage was reused (3.8). —end note ] [Example:

    templatestruct AlignedUnion {alignas(T...) unsigned char data[max(sizeof(T)...)];

    };int f() {AlignedUnion au;int *p = new (au.data) int; // OK, au.data provides storagechar *c = new (au.data) char(); // OK, ends lifetime of *pchar *d = new (au.data + 1) char();

    § 1.8 7

  • © ISO/IEC N4604

    return *c + *d; // OK}

    —end example ]4 An object a is nested within another object b if:

    —(4.1) a is a subobject of b, or

    —(4.2) b provides storage for a, or

    —(4.3) there exists an object c where a is nested within c, and c is nested within b.

    5 For every object x, there is some object called the complete object of x, determined as follows:

    —(5.1) If x is a complete object, then x is the complete object of x.

    —(5.2) Otherwise, the complete object of x is the complete object of the (unique) object that contains x.6 If a complete object, a data member (9.2), or an array element is of class type, its type is considered the most

    derived class, to distinguish it from the class type of any base class subobject; an object of a most derivedclass type or of a non-class type is called a most derived object.

    7 Unless it is a bit-field (9.2.4), a most derived object shall have a non-zero size and shall occupy one or morebytes of storage. Base class subobjects may have zero size. An object of trivially copyable or standard-layouttype (3.9) shall occupy contiguous bytes of storage.

    8 Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the addressof the first byte it occupies. Two objects a and b with overlapping lifetimes that are not bit-fields may havethe same address if one is nested within the other, or if at least one is a base class subobject of zero size andthey are of different types; otherwise, they have distinct addresses.4

    [Example:static const char test1 = ’x’;static const char test2 = ’x’;const bool b = &test1 != &test2; // always true

    —end example ]9 [Note: C++ provides a variety of fundamental types and several ways of composing new types from existing

    types (3.9). —end note ]

    1.9 Program execution [intro.execution]1 The semantic descriptions in this International Standard define a parameterized nondeterministic abstract

    machine. This International Standard places no requirement on the structure of conforming implementations.In particular, they need not copy or emulate the structure of the abstract machine. Rather, conformingimplementations are required to emulate (only) the observable behavior of the abstract machine as explainedbelow.5

    2 Certain aspects and operations of the abstract machine are described in this International Standard asimplementation-defined (for example, sizeof(int)). These constitute the parameters of the abstract

    4) Under the “as-if” rule an implementation is allowed to store two objects at the same machine address or not store an objectat all if the program cannot observe the difference (1.9).

    5) This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of thisInternational Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from theobservable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it candeduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.

    § 1.9 8

  • © ISO/IEC N4604

    machine. Each implementation shall include documentation describing its characteristics and behavior inthese respects.6 Such documentation shall define the instance of the abstract machine that corresponds tothat implementation (referred to as the “corresponding instance” below).

    3 Certain other aspects and operations of the abstract machine are described in this International Standardas unspecified (for example, evaluation of expressions in a new-initializer if the allocation function fails toallocate memory (5.3.4)). Where possible, this International Standard defines a set of allowable behaviors.These define the nondeterministic aspects of the abstract machine. An instance of the abstract machine canthus have more than one possible execution for a given program and a given input.

    4 Certain other operations are described in this International Standard as undefined (for example, the effect ofattempting to modify a const object). [Note: This International Standard imposes no requirements on thebehavior of programs that contain undefined behavior. —end note ]

    5 A conforming implementation executing a well-formed program shall produce the same observable behavior asone of the possible executions of the corresponding instance of the abstract machine with the same programand the same input. However, if any such execution contains an undefined operation, this InternationalStandard places no requirement on the implementation executing that program with that input (not evenwith regard to operations preceding the first undefined operation).

    6 If a signal handler is executed as a result of a call to the raise function, then the execution of the handler issequenced after the invocation of the raise function and before its return. [Note: When a signal is receivedfor another reason, the execution of the signal handler is usually unsequenced with respect to the rest of theprogram. —end note ]

    7 An instance of each object with automatic storage duration (3.7.3) is associated with each entry into itsblock. Such an object exists and retains its last-stored value during the execution of the block and while theblock is suspended (by a call of a function or receipt of a signal).

    8 The least requirements on a conforming implementation are:—(8.1) Access to volatile objects are evaluated strictly according to the rules of the abstract machine.

    —(8.2) At program termination, all data written into files shall be identical to one of the possible results thatexecution of the program according to the abstract semantics would have produced.

    —(8.3) The input and output dynamics of interactive devices shall take place in such a fashion that promptingoutput is actually delivered before a program waits for input. What constitutes an interactive device isimplementation-defined.

    These collectively are referred to as the observable behavior of the program. [Note: More stringent cor-respondences between abstract and actual semantics may be defined by each implementation. —endnote ]

    9 [Note: Operators can be regrouped according to the usual mathematical rules only where the operatorsreally are associative or commutative.7 For example, in the following fragmentint a, b;/∗ ... ∗/a = a + 32760 + b + 5;

    the expression statement behaves exactly the same asa = (((a + 32760) + b) + 5);

    due to the associativity and precedence of these operators. Thus, the result of the sum (a + 32760) is nextadded to b, and that result is then added to 5 which results in the value assigned to a. On a machine in which

    6) This documentation also includes conditionally-supported constructs and locale-specific behavior. See 1.4.7) Overloaded operators are never assumed to be associative or commutative.

    § 1.9 9

  • © ISO/IEC N4604

    overflows produce an exception and in which the range of values representable by an int is [-32768, +32767],the implementation cannot rewrite this expression as

    a = ((a + b) + 32765);

    since if the values for a and b were, respectively, -32754 and -15, the sum a + b would produce an exceptionwhile the original expression would not; nor can the expression be rewritten either as

    a = ((a + 32765) + b);

    ora = (a + (b + 32765));

    since the values for a and b might have been, respectively, 4 and -8 or -17 and 12. However on a machine inwhich overflows do not produce an exception and in which the results of overflows are reversible, the aboveexpression statement can be rewritten by the implementation in any of the above ways because the sameresult will occur. —end note ]

    10 A full-expression is an expression that is not a subexpression of another expression. [Note: in some contexts,such as unevaluated operands, a syntactic subexpression is considered a full-expression (Clause 5). —endnote ] If a language construct is defined to produce an implicit call of a function, a use of the languageconstruct is considered to be an expression for the purposes of this definition. A call to a destructor generatedat the end of the lifetime of an object other than a temporary object is an implicit full-expression. Conversionsapplied to the result of an expression in order to satisfy the requirements of the language construct in whichthe expression appears are also considered to be part of the full-expression.[Example:

    struct S {S(int i): I(i) { }int& v() { return I; }

    private:int I;

    };

    S s1(1); // full-expression is call of S::S(int)S s2 = 2; // full-expression is call of S::S(int)

    void f() {if (S(3).v()) // full-expression includes lvalue-to-rvalue and

    // int to bool conversions, performed before// temporary is deleted at end of full-expression

    { }}

    —end example ]11 [Note: The evaluation of a full-expression can include the evaluation of subexpressions that are not lexically

    part of the full-expression. For example, subexpressions involved in evaluating default arguments (8.3.6) areconsidered to be created in the expression that calls the function, not the expression that defines the defaultargument. —end note ]

    12 Reading an object designated by a volatile glvalue (3.10), modifying an object, calling a library I/Ofunction, or calling a function that does any of those operations are all side effects, which are changes in thestate of the execution environment. Evaluation of an expression (or a sub-expression) in general includesboth value computations (including determining the identity of an object for glvalue evaluation and fetchinga value previously assigned to an object for prvalue evaluation) and initiation of side effects. When a call to

    § 1.9 10

  • © ISO/IEC N4604

    a library I/O function returns or an access to a volatile object is evaluated the side effect is consideredcomplete, even though some external actions implied by the call (such as the I/O itself) or by the volatileaccess may not have completed yet.

    13 Sequenced before is an asymmetric, transitive, pair-wise relation between evaluations executed by a singlethread (1.10), which induces a partial order among those evaluations. Given any two evaluations A and B,if A is sequenced before B (or, equivalently, B is sequenced after A), then the execution of A shall precedethe execution of B. If A is not sequenced before B and B is not sequenced before A, then A and B areunsequenced. [Note: The execution of unsequenced evaluations can overlap. —end note ] EvaluationsA and B are indeterminately sequenced when either A is sequenced before B or B is sequenced before A,but it is unspecified which. [Note: Indeterminately sequenced evaluations cannot overlap, but either couldbe executed first. —end note ] An expression X is said to be sequenced before an expression Y if everyvalue computation and every side effect associated with the expression X is sequenced before every valuecomputation and every side effect associated with the expression Y.

    14 Every value computation and side effect associated with a full-expression is sequenced before every valuecomputation and side effect associated with the next full-expression to be evaluated.8

    15 Except where noted, evaluations of operands of individual operators and of subexpressions of individualexpressions are unsequenced. [Note: In an expression that is evaluated more than once during the executionof a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not beperformed consistently in different evaluations. —end note ] The value computations of the operands ofan operator are sequenced before the value computation of the result of the operator. If a side effect on amemory location (1.7) is unsequenced relative to either another side effect on the same memory location or avalue computation using the value of any object in the same memory location, and they are not potentiallyconcurrent (1.10), the behavior is undefined. [Note: The next section imposes similar, but more complexrestrictions on potentially concurrent computations. —end note ][Example:

    void f(int, int);void g(int i, int* v) {i = v[i++]; // the behavior is undefinedi = 7, i++, i++; // i becomes 9

    i = i++ + 1; // the behavior is undefinedi = i + 1; // the value of i is incremented

    f(i = -1, i = -1); // the behavior is undefined}

    —end example ]16 When calling a function (whether or not the function is inline), every value computation and side effect

    associated with any argument expression, or with the postfix expression designating the called function, issequenced before execution of every expression or statement in the body of the called function. For eachfunction invocation F , for every evaluation A that occurs within F and every evaluation B that does notoccur within F but is evaluated on the same thread and as part of the same signal handler (if any), either Ais sequenced before B or B is sequenced before A. 9 [Note: If A and B would not otherwise be sequenced thenthey are indeterminately sequenced. —end note ] Several contexts in C++ cause evaluation of a function call,even though no corresponding function call syntax appears in the translation unit. [Example: Evaluation ofa new-expression invokes one or more allocation and constructor functions; see 5.3.4. For another example,

    8) As specified in 12.2, after a full-expression is evaluated, a sequence of zero or more invocations of destructor functions fortemporary objects takes place, usually in reverse order of the construction of each temporary object.

    9) In other words, function executions do not interleave with each other.

    § 1.9 11

  • © ISO/IEC N4604

    invocation of a conversion function (12.3.2) can arise in contexts in which no function call syntax appears.—end example ] The sequencing constraints on the execution of the called function (as described above) arefeatures of the function calls as evaluated, whatever the syntax of the expression that calls the function mightbe.

    1.10 Multi-threaded executions and data races [intro.multithread]1 A thread of execution (also known as a thread) is a single flow of control within a program, including the initial

    invocation of a specific top-level function, and recursively including every function invocation subsequentlyexecuted by the thread. [Note: When one thread creates another, the initial call to the top-level function ofthe new thread is executed by the new thread, not by the creating thread. —end note ] Every thread in aprogram can potentially access every object and function in a program.10 Under a hosted implementation, aC++ program can have more than one thread running concurrently. The execution of each thread proceedsas defined by the remainder of this standard. The execution of the entire program consists of an execution ofall of its threads. [Note: Usually the execution can be viewed as an interleaving of all its threads. However,some kinds of atomic operations, for example, allow executions inconsistent with a simple interleaving, asdescribed below. —end note ] Under a freestanding implementation, it is implementation-defined whether aprogram can have more than one thread of execution.

    2 A signal handler that is executed as a result of a call to the raise function belongs to the same thread ofexecution as the call to the raise function. Otherwise it is unspecified which thread of execution contains asignal handler invocation.

    1.10.1 Data races [intro.races]1 The value of an object visible to a thread T at a particular point is the initial value of the object, a value

    assigned to the object by T , or a value assigned to the object by another thread, according to the rulesbelow. [Note: In some cases, there may instead be undefined behavior. Much of this section is motivated bythe desire to support atomic operations with explicit and detailed visibility constraints. However, it alsoimplicitly supports a simpler view for more restricted programs. —end note ]

    2 Two expression evaluations conflict if one of them modifies a memory location (1.7) and the other one readsor modifies the same memory location.

    3 The library defines a number of atomic operations (Clause 29) and operations on mutexes (Clause 30) that arespecially identified as synchronization operations. These operations play a special role in making assignmentsin one thread visible to another. A synchronization operation on one or more memory locations is either aconsume operation, an acquire operation, a release operation, or both an acquire and release operation. Asynchronization operation without an associated memory location is a fence and can be either an acquirefence, a release fence, or both an acquire and release fence. In addition, there are relaxed atomic operations,which are not synchronization operations, and atomic read-modify-write operations, which have specialcharacteristics. [Note: For ex