Document Number: Date: Revises: Reply to: N4659 2017-03-21 N4640 Richard Smith Google Inc cxxeditor@gmail.com Working Draft, Standard for Programming Language C++ Note: this is an early draft It’s known to be incomplet and incorrekt, and it has lots of ba d formatting N4659 © ISO/IEC Contents Contents ii List of Tables x List of Figures xiv Scope Normative references Terms and definitions General principles 4.1 Implementation compliance 4.2 Structure of this document 4.3 Syntax notation 4.4 The C++ memory model 4.5 The C++ object model 4.6 Program execution 4.7 Multi-threaded executions and data races 4.8 Acknowledgments 7 8 11 15 20 Lexical conventions 5.1 Separate translation 5.2 Phases of translation 5.3 Character sets 5.4 Preprocessing tokens 5.5 Alternative tokens 5.6 Tokens 5.7 Comments 5.8 Header names 5.9 Preprocessing numbers 5.10 Identifiers 5.11 Keywords 5.12 Operators and punctuators 5.13 Literals 22 22 22 23 24 25 25 26 26 26 27 28 29 29 Basic 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 39 39 41 44 50 63 66 70 74 Contents concepts Declarations and definitions One-definition rule Scope Name lookup Program and linkage Start and termination Storage duration Object lifetime ii N4659 © ISO/IEC 6.9 6.10 6.11 Types Lvalues and rvalues Alignment Standard conversions 7.1 Lvalue-to-rvalue conversion 7.2 Array-to-pointer conversion 7.3 Function-to-pointer conversion 7.4 Temporary materialization conversion 7.5 Qualification conversions 7.6 Integral promotions 7.7 Floating-point promotion 7.8 Integral conversions 7.9 Floating-point conversions 7.10 Floating-integral conversions 7.11 Pointer conversions 7.12 Pointer to member conversions 7.13 Function pointer conversions 7.14 Boolean conversions 7.15 Integer conversion rank 77 83 84 86 87 87 88 88 88 89 89 89 90 90 90 90 91 91 91 Expressions 8.1 Primary expressions 8.2 Postfix expressions 8.3 Unary expressions 8.4 Explicit type conversion (cast notation) 8.5 Pointer-to-member operators 8.6 Multiplicative operators 8.7 Additive operators 8.8 Shift operators 8.9 Relational operators 8.10 Equality operators 8.11 Bitwise AND operator 8.12 Bitwise exclusive OR operator 8.13 Bitwise inclusive OR operator 8.14 Logical AND operator 8.15 Logical OR operator 8.16 Conditional operator 8.17 Throwing an exception 8.18 Assignment and compound assignment operators 8.19 Comma operator 8.20 Constant expressions 93 96 108 120 129 130 131 131 132 133 133 135 135 135 135 135 136 137 138 139 139 Statements 9.1 Labeled statement 9.2 Expression statement 9.3 Compound statement or block 9.4 Selection statements 9.5 Iteration statements 9.6 Jump statements 9.7 Declaration statement 144 145 145 145 145 147 150 152 Contents iii N4659 © ISO/IEC 9.8 Ambiguity resolution 10 Declarations 10.1 Specifiers 10.2 Enumeration declarations 10.3 Namespaces 10.4 The asm declaration 10.5 Linkage specifications 10.6 Attributes 152 155 157 174 178 191 191 194 11 Declarators 11.1 Type names 11.2 Ambiguity resolution 11.3 Meaning of declarators 11.4 Function definitions 11.5 Structured binding declarations 11.6 Initializers 201 202 203 204 216 219 220 12 Classes 12.1 Class names 12.2 Class members 12.3 Unions 12.4 Local class declarations 237 239 241 251 254 13 Derived classes 13.1 Multiple base classes 13.2 Member name lookup 13.3 Virtual functions 13.4 Abstract classes 256 257 259 262 266 base class members 269 270 271 274 277 278 278 278 280 280 283 285 288 291 293 300 303 16 Overloading 16.1 Overloadable declarations 16.2 Declaration matching 311 311 313 14 Member access control 14.1 Access specifiers 14.2 Accessibility of base classes and 14.3 Friends 14.4 Protected member access 14.5 Access to virtual functions 14.6 Multiple access 14.7 Nested classes 15 Special member functions 15.1 Constructors 15.2 Temporary objects 15.3 Conversions 15.4 Destructors 15.5 Free store 15.6 Initialization 15.7 Construction and destruction 15.8 Copying and moving class objects Contents iv N4659 © ISO/IEC 16.3 16.4 16.5 16.6 314 335 336 341 17 Templates 17.1 Template parameters 17.2 Names of template specializations 17.3 Template arguments 17.4 Type equivalence 17.5 Template declarations 17.6 Name resolution 17.7 Template instantiation and specialization 17.8 Function template specializations 17.9 Deduction guides 344 345 349 350 356 357 375 390 402 423 18 Exception handling 18.1 Throwing an exception 18.2 Constructors and destructors 18.3 Handling an exception 18.4 Exception specifications 18.5 Special functions 424 425 427 427 429 432 19 Preprocessing directives 19.1 Conditional inclusion 19.2 Source file inclusion 19.3 Macro replacement 19.4 Line control 19.5 Error directive 19.6 Pragma directive 19.7 Null directive 19.8 Predefined macro names 19.9 Pragma operator 434 435 437 438 443 444 444 444 444 446 20 Library introduction 20.1 General 20.2 The C standard library 20.3 Definitions 20.4 Method of description (Informative) 20.5 Library-wide requirements 447 447 448 448 451 456 21 Language support library 21.1 General 21.2 Common definitions 21.3 Implementation properties 21.4 Integer types 21.5 Start and termination 21.6 Dynamic memory management 21.7 Type identification 21.8 Exception handling 21.9 Initializer lists 478 478 478 483 492 493 494 502 504 509 Contents Overload resolution Address of overloaded function Overloaded operators Built-in operators v N4659 © ISO/IEC 21.10 Other runtime support 22 Diagnostics library 22.1 General 22.2 Exception classes 22.3 Assertions 22.4 Error numbers 22.5 System error support 510 513 513 513 517 517 519 23 General utilities library 23.1 General 23.2 Utility components 23.3 Compile-time integer sequences 23.4 Pairs 23.5 Tuples 23.6 Optional objects 23.7 Variants 23.8 Storage for any type 23.9 Bitsets 23.10 Memory 23.11 Smart pointers 23.12 Memory resources 23.13 Class template scoped_allocator_adaptor 23.14 Function objects 23.15 Metaprogramming and type traits 23.16 Compile-time rational arithmetic 23.17 Time utilities 23.18 Class type_index 23.19 Execution policies 530 530 530 538 539 543 554 569 582 587 594 609 635 646 652 676 702 705 722 723 24 Strings library 24.1 General 24.2 Character traits 24.3 String classes 24.4 String view classes 24.5 Null-terminated sequence utilities 726 726 726 732 766 776 25 Localization library 25.1 General 25.2 Header synopsis 25.3 Locales 25.4 Standard locale categories 25.5 C library locales 782 782 782 784 791 829 26 Containers library 26.1 General 26.2 Container requirements 26.3 Sequence containers 26.4 Associative containers 26.5 Unordered associative containers 26.6 Container adaptors 830 830 830 868 900 921 945 Contents vi N4659 © ISO/IEC 27 Iterators library 27.1 General 27.2 Iterator requirements 27.3 Header synopsis 27.4 Iterator primitives 27.5 Iterator adaptors 27.6 Stream iterators 27.7 Range access 27.8 Container access 955 955 955 961 964 967 980 987 988 28 Algorithms library 28.1 General 28.2 Header synopsis 28.3 Algorithms requirements 28.4 Parallel algorithms 28.5 Non-modifying sequence operations 28.6 Mutating sequence operations 28.7 Sorting and related operations 28.8 C library algorithms 989 989 989 1008 1009 1012 1020 1030 1049 29 Numerics library 29.1 General 29.2 Definitions 29.3 Numeric type requirements 29.4 The floating-point environment 29.5 Complex numbers 29.6 Random number generation 29.7 Numeric arrays 29.8 Generalized numeric operations 29.9 Mathematical functions for floating-point types 1050 1050 1050 1050 1051 1052 1062 1105 1125 1139 30 Input/output library 30.1 General 30.2 Iostreams requirements 30.3 Forward declarations 30.4 Standard iostream objects 30.5 Iostreams base classes 30.6 Stream buffers 30.7 Formatting and manipulators 30.8 String-based streams 30.9 File-based streams 30.10 File systems 30.11 C library files 1157 1157 1158 1158 1160 1162 1179 1188 1214 1225 1239 1293 31 Regular expressions library 31.1 General 31.2 Definitions 31.3 Requirements 31.4 Header synopsis 31.5 Namespace std::regex_constants 31.6 Class regex_error 1297 1297 1297 1298 1300 1306 1309 Contents vii N4659 © ISO/IEC 31.7 31.8 31.9 31.10 31.11 31.12 31.13 Class template regex_traits Class template basic_regex Class template sub_match Class template match_results Regular expression algorithms Regular expression iterators Modified ECMAScript regular expression grammar 1310 1312 1318 1323 1328 1333 1339 32 Atomic operations library 32.1 General 32.2 Header synopsis 32.3 Type aliases 32.4 Order and consistency 32.5 Lock-free property 32.6 Class template atomic 32.7 Non-member functions 32.8 Flag type and operations 32.9 Fences 1342 1342 1342 1346 1346 1348 1348 1356 1356 1357 33 Thread support library 33.1 General 33.2 Requirements 33.3 Threads 33.4 Mutual exclusion 33.5 Condition variables 33.6 Futures 1359 1359 1359 1362 1367 1388 1395 A Grammar summary A.1 Keywords A.2 Lexical conventions A.3 Basic concepts A.4 Expressions A.5 Statements A.6 Declarations A.7 Declarators A.8 Classes A.9 Derived classes A.10 Special member functions A.11 Overloading A.12 Templates A.13 Exception handling A.14 Preprocessing directives 1412 1412 1412 1417 1417 1421 1422 1426 1428 1429 1430 1430 1430 1431 1432 B Implementation quantities C Compatibility C.1 C++ and ISO C C.2 C++ and ISO C++ 2003 C.3 C++ and ISO C++ 2011 C.4 C++ and ISO C++ 2014 C.5 C standard library Contents 1434 1436 1436 1445 1451 1453 1457 viii N4659 © ISO/IEC D Compatibility features D.1 Redeclaration of static constexpr data members D.2 Implicit declaration of copy functions D.3 Deprecated exception specifications D.4 C++ standard library headers D.5 C standard library headers D.6 char* streams D.7 uncaught_exception D.8 Old adaptable function bindings D.9 The default allocator D.10 Raw storage iterator D.11 Temporary buffers D.12 Deprecated type traits D.13 Deprecated iterator primitives D.14 Deprecated shared_ptr observers D.15 Deprecated standard code conversion facets D.16 Deprecated convenience conversion interfaces 1460 1460 1460 1460 1460 1461 1461 1470 1470 1475 1476 1478 1478 1479 1479 1479 1481 Cross references 1486 Cross references from ISO C++ 2014 1507 Index 1509 Index of grammar productions 1542 Index of library names 1546 Index of implementation-defined behavior 1604 Contents ix N4659 © ISO/IEC List of Tables Alternative tokens Ranges of characters allowed Ranges of characters disallowed initially (combining characters) Identifiers with special meaning Keywords Alternative representations Types of integer literals Escape sequences String literal concatenations 25 27 27 28 28 28 30 33 36 10 Relations on const and volatile 83 11 simple-type-specifiers and the types they specify 168 12 13 Relationship between operator and function call notation Conversions 319 328 14 Value of folding empty sequences 363 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Library categories C++ library headers C++ headers for C library facilities C standard Annex K names C++ headers for freestanding implementations EqualityComparable requirements LessThanComparable requirements DefaultConstructible requirements MoveConstructible requirements CopyConstructible requirements (in addition to MoveConstructible) MoveAssignable requirements CopyAssignable requirements (in addition to MoveAssignable) Destructible requirements NullablePointer requirements Hash requirements Descriptive variable definitions Allocator requirements 447 457 457 458 459 460 460 460 460 461 461 461 461 463 464 464 465 32 Language support library summary 478 33 Diagnostics library summary 513 34 35 36 37 38 General utilities library summary optional::operator=(const optional&) effects optional::operator=(optional&&) effects optional::operator=(const optional&) effects optional::operator=(optional&&) effects 530 560 560 561 562 List of Tables x N4659 © ISO/IEC allocator_traits, 604 skipws, 1177 sleep_for this_thread, 1367 sleep_until this_thread, 1367 slice, 1118 constructor, 1118 size, 1118 start, 1118 stride, 1118 slice_array, 1118 operator*=, 1119 operator+=, 1119 operator-=, 1119 operator/=, 1119 operator=, 1119 operator%=, 1119 operator&=, 1119 operator^=, 1119 operator|=, 1119 snextc basic_streambuf, 1183 snprintf, 1293 sort, 1031 forward_list, 885 list, 892 sort_heap, 1044 space, 1290 sph_bessel, 1155 sph_besself, 1155 sph_bessell, 1155 sph_legendre, 1155 sph_legendref, 1155 sph_legendrel, 1155 sph_neumann, 1156 sph_neumannf, 1156 sph_neumannl, 1156 splice list, 890, 891 splice_after forward_list, 884 sprintf, 1293 sputbackc basic_streambuf, 1183 sputc basic_streambuf, 1184 sputn basic_streambuf, 1184 Index of library names sqrt, 1139 complex, 1061 valarray, 1117 sqrtf, 1139 sqrtl, 1139 srand, 479, 1105 sscanf, 1293 , 1214 stable_partition, 1037 stable_sort, 1031 , 946 stack, 952 constructor, 953, 954 swap, 954 start gslice, 1121 slice, 1118 state fpos, 1171 wbuffer_convert, 1484 wstring_convert, 1483 state_type char_traits, 728 wbuffer_convert, 1484 wstring_convert, 1483 static_pointer_cast shared_ptr, 628 status, 1290, 1291 directory_entry, 1273 status_known, 1292 , 1458, 1460 , 510 , 1458, 1461 stddev normal_distribution, 1095 stderr, 1293 , 513 stdin, 1293 , 1294 , 1461 stdout, 1293 steady_clock, 721 stem path, 1259 stod, 764, 765 stof, 764, 765 stoi, 764, 765 stol, 764, 765 stold, 764, 765 stoll, 764, 765 store 1594 N4659 © ISO/IEC atomic, 1350 atomic, 1350 atomic, 1350 stoul, 764, 765 stoull, 764, 765 str basic_istringstream, 1221 basic_ostringstream, 1223 basic_stringbuf, 1217 basic_stringstream, 1225 istrstream, 1468 match_results, 1326 ostrstream, 1469 strstream, 1470 strstreambuf, 1464 sub_match, 1318 strcat, 777 strchr, 777 strcmp, 777 strcoll, 777 strcpy, 777 strcspn, 777 , 1179 streambuf, 1158, 1179 streamoff, 1163, 1171 streamsize, 1163 strerror, 777 strftime, 721, 818 stride gslice, 1121 slice, 1118 , 732 string operator""s, 766 path, 1257 , 778 , 767 string_view operator""sv, 776 stringbuf, 1158, 1214 stringstream, 1158, 1214 strlen, 777, 1464, 1468 strncat, 777 strncmp, 777 strncpy, 777 strpbrk, 777 strrchr, 777 strspn, 777 strstr, 777 , 1461 strstream, 1469 Index of library names constructor, 1469 destructor, 1470 freeze, 1470 pcount, 1470 rdbuf, 1470 str, 1470 strstreambuf, 1461 constructor, 1463, 1464 destructor, 1464 freeze, 1464 overflow, 1465 pbackfail, 1465 pcount, 1464 seekoff, 1466 seekpos, 1466 setbuf, 1467 str, 1464 underflow, 1465 strtod, 479 strtof, 479 strtoimax, 1294 strtok, 777 strtol, 479 strtold, 479 strtoll, 479 strtoul, 479 strtoull, 479 strtoumax, 1294 strxfrm, 777 student_t_distribution, 1099 constructor, 1099 mean, 1100 sub_match, 1318 compare, 1319 constructor, 1318 length, 1318 operator basic_string, 1318 operator!=, 1319–1322 operator= 128, 1152 effect of calling cylindrical Neumann functions with nu >= 128, 1153 effect of calling Hermite polynomials with n >= 128, 1154 effect of calling ios_base::sync_with_stdio after any input or output operation on standard streams, 1169 effect of calling irregular modified cylindrical Bessel functions with nu >= 128, 1153 effect of calling Laguerre polynomials with n >= 128, 1154 1604 © ISO/IEC effect of calling Legendre polynomials with l >= 128, 1155 effect of calling regular modified cylindrical Bessel functions with nu >= 128, 1152 effect of calling spherical associated Legendre functions with l >= 128, 1156 effect of calling spherical Bessel functions with n >= 128, 1155 effect of calling spherical Neumann functions with n >= 128, 1156 effect of filesystem::copy, 1280 effect on C locale of calling locale::global, 790 encoding of universal character name not in execution character set, 32 error_category for errors originating outside the operating system, 476 exception type when random_device constructor fails, 1083 exception type when random_device::operator() fails, 1083 exception type when shared_ptr constructor fails, 623, 624 exceptions thrown by standard library functions that have a potentially-throwing exception specification, 476 execution character set and execution wide-character set, 24 exit status, 494 extended signed integer types, 79 file type of the file argument of filesystem::status, 1291 formatted character sequence generated by time_put::do_put in C locale, 818 forward progress guarantees for implicit threads of parallel algorithms (if not defined for thread), 1010 growth factor for monotonic_buffer_resource, 646 headers for freestanding implementation, 458 how random_device::operator() generates values, 1083 interactive device, 12 interpretation of the path character sequence with format path::auto_format, 1265 largest supported value to configure the largest allocation satisfied directly by a pool, 643 Index of implementation-defined behavior N4659 largest supported value to configure the maximum number of blocks to replenish a pool, 643 linkage of main, 66 linkage of names from C standard library, 459 linkage of objects between C++ and other languages, 194 locale names, 788 lvalue-to-rvalue conversion of an invalid pointer value, 87 manner of search for included source file, 438 mapping from name to catalog when calling messages::do_open, 825 mapping from physical source file characters to basic source character set, 23, 1453 mapping header name to header or external source file, 26 mapping of pointer to integer, 117 mapping physical source file characters to basic source character set, 22 mapping to message when calling messages::do_get, 825 maximum depth of recursive template instantiations, 394 maximum size of an allocated object, 123, 501 meaning of ’, \, /*, or // in a q-char-sequence or an h-char-sequence, 26 meaning of asm declaration, 191 meaning of attribute declaration, 156 meaning of dot-dot in root-directory, 1251 negative value of character literal in preprocessor, 437 nesting limit for #include directives, 438 NTCTS in basic_ostream& operator