|
|
|
Document Number: N4713
Date:
2017-11-27
Revises:
N4700
Reply to:
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
fomatting.
Contents
1
Scope
1
2
Normative references
2
3
Terms and definitions
3
4
General principles
6
4.1
Implementation compliance
6
4.2
Structure of this document .
8
4.3
Syntax notation
8
4.4
Acknowledgments
8
5
Lexical conventions
9
5.1
Separate translation
9
5.2
Phases of translation
9
5.3
Character sets
10
5.4
Preprocessing tokens
11
5.5
Alternative tokens
12
5.6
Tokens
12
5.7
Comments
12
5.8
Header names
12
5.9
Preprocessing numbers
13
5.10
Identifiers
13
5.11
Keywords
14
5.12
Operators and punctuators
15
5.13
Literals
15
6
Basic concepts
24
6.1
Declarations and definitions
24
6.2
One-definition rule
26
6.3
Scope
29
6.4
Name lookup
34
6.5
Program and linkage
46
6.6
Memory and objects
48
6.7
Types
57
6.8
Program execution
63
7
Standard conversions
75
7.1
Lvalue-to-rvalue conversion
76
7.2
Array-to-pointer conversion
76
7.3
Function-to-pointer conversion .
76
7.4
Temporary materialization conversion
76
7.5
Qualification conversions
77
7.6
Integral promotions
77
7.7
Floating-point promotion
78
7.8
Integral conversions
78
7.9
Floating-point conversions
78
7.10
Floating-integral conversions
78
7.11
Pointer conversions
78
7.12
Pointer-to-member conversions
79
7.13
Function pointer conversions
79
7.14
Boolean conversions
79
Contents
ii
8
Expressions
80
8.1
Preamble
80
8.2
Properties of expressions
80
8.3
Usual arithmetic conversions .
83
8.4
Primary expressions
84
8.5
Compound expressions
99
8.6
Constant expressions
126
9
Statements
130
9.1
Labeled statement
130
9.2
Expression statement
131
9.3
Compound statement or block
131
9.4
Selection statements
131
9.5
Iteration statements
133
9.6
Jump statements
135
9.7
Declaration statement
137
9.8
Ambiguity resolution
137
10
Declarations
139
10.1
Specifiers
140
10.2
Enumeration declarations . .
156
10.3
Namespaces
159
10.4
The asm declaration
171
10.5
Linkage specifications
171
10.6
Attributes
173
11
Declarators
179
11.1
Type names
180
11.2
Ambiguity resolution
181
11.3
Meaning of declarators
182
11.4
Function definitions
192
11.5
Structured binding declarations
195
11.6
Initializers
196
12
Classes
211
12.1
Class names
213
12.2
Class members
214
12.3
Unions
224
12.4
Local class declarations
226
13
Derived classes
227
13.1
Multiple base classes
228
13.2
Member name lookup
229
13.3
Virtual functions
232
13.4
Abstract classes
236
14
Member access control
238
14.1
Access specifiers
239
14.2
Accessibility of base classes and base class members .
240
14.3
Friends
242
14.4
Protected member access
245
14.5
Access to virtual functions
245
14.6
Multiple access
246
14.7
Nested classes
246
15
Special member functions
247
15.1
Constructors
247
15.2
Temporary objects
249
15.3
Conversions
252
15.4
Destructors
255
Contents
iii
15.5
Free store
257
15.6
Initialization
259
15.7
Construction and destruction
265
15.8
Copying and moving class objects
267
15.9
Comparisons
274
16
Overloading
276
16.1
Overloadable declarations
276
16.2
Declaration matching
278
16.3
Overload resolution
279
16.4
Address of overloaded function
298
16.5
Overloaded operators
299
16.6
Built-in operators
303
17
Templates
306
17.1
Template parameters
307
17.2
Names of template specializations
312
17.3
Template arguments
314
17.4
Template constraints
319
17.5
Type equivalence
322
17.6
Template declarations
323
17.7
Name resolution
341
17.8
Template instantiation and specialization
354
17.9
Function template specializations . .
366
17.10
Deduction guides
385
18
Exception handling
386
18.1
Throwing an exception
387
18.2
Constructors and destructors
388
18.3
Handling an exception
389
18.4
Exception specifications
390
18.5
Special functions
393
19
Preprocessing directives
395
19.1
Conditional inclusion
396
19.2
Source file inclusion
398
19.3
Macro replacement
399
19.4
Line control
404
19.5
Error directive
404
19.6
Pragma directive
404
19.7
Null directive
404
19.8
Predefined macro names
404
19.9
Pragma operator
406
20
Library introduction
407
20.1
General
407
20.2
The C standard library
408
20.3
Definitions
408
20.4
Method of description (Informative)
410
20.5
Library-wide requirements
415
21
Language support library
434
21.1
General
434
21.2
Common definitions
434
21.3
Implementation properties
438
21.4
Integer types
446
21.5
Start and termination
447
21.6
Dynamic memory management
448
21.7
Type identification
455
Contents
iv
21.8
Exception handling
457
21.9
Initializer lists
461
21.10
Comparisons
462
21.11
Other runtime support
469
22
Diagnostics library
472
22.1
General
472
22.2
Exception classes
472
22.3
Assertions
475
22.4
Error numbers
476
22.5
System error support
477
23
General utilities library
487
23.1
General
487
23.2
Utility components
487
23.3
Compile-time integer sequences
491
23.4
Pairs
492
23.5
Tuples
495
23.6
Optional objects
505
23.7
Variants
518
23.8
Storage for any type
528
23.9
Bitsets
533
23.10
Memory
539
23.11
Smart pointers
552
23.12
Memory resources
579
23.13
Class template scoped_allocator_adaptor .
588
23.14
Function objects
593
23.15
Metaprogramming and type traits
614
23.16
Compile-time rational arithmetic
636
23.17
Time utilities
639
23.18
Class type_index
653
23.19
Execution policies
654
23.20
Primitive numeric conversions . .
656
24
Strings library
660
24.1
General
660
24.2
Character traits
660
24.3
String classes
665
24.4
String view classes
694
24.5
Null-terminated sequence utilities
704
25
Localization library
708
25.1
General
708
25.2
Header <locale> synopsis
708
25.3
Locales
709
25.4
Standard locale categories
716
25.5
C library locales
748
26
Containers library
750
26.1
General
750
26.2
Container requirements
750
26.3
Sequence containers
782
26.4
Associative containers
809
26.5
Unordered associative containers
826
26.6
Container adaptors
847
27
Iterators library
855
27.1
General
855
27.2
Iterator requirements
855
Contents
v
27.3
Header <iterator> synopsis
860
27.4
Iterator primitives
863
27.5
Iterator adaptors
865
27.6
Stream iterators
876
27.7
Range access
882
27.8
Container access
883
28
Algorithms library
884
28.1
General
884
28.2
Header <algorithm> synopsis
884
28.3
Algorithms requirements
901
28.4
Parallel algorithms
902
28.5
Non-modifying sequence operations .
905
28.6
Mutating sequence operations
912
28.7
Sorting and related operations
921
28.8
C library algorithms
938
29
Numerics library
940
29.1
General
940
29.2
Definitions
940
29.3
Numeric type requirements
940
29.4
The floating-point environment . .
941
29.5
Complex numbers
942
29.6
Random number generation
950
29.7
Numeric arrays
987
29.8
Generalized numeric operations . .
1005
29.9
Mathematical functions for floating-point types
1016
30
Input/output library
1032
30.1
General
1032
30.2
Iostreams requirements
1032
30.3
Forward declarations
1033
30.4
Standard iostream objects
1035
30.5
Iostreams base classes
1037
30.6
Stream buffers
1052
30.7
Formatting and manipulators
1059
30.8
String-based streams
1082
30.9
File-based streams
1091
30.10
Synchronized output streams
1104
30.11
File systems
1109
30.12
C library files
1154
31
Regular expressions library
1158
31.1
General
1158
31.2
Definitions
1158
31.3
Requirements
1159
31.4
Header <regex> synopsis
1160
31.5
Namespace std::regex_constants
1166
31.6
Class regex_error
1169
31.7
Class template regex_traits
1169
31.8
Class template basic_regex
1171
31.9
Class template sub_match
1176
31.10
Class template match_results
1181
31.11
Regular expression algorithms
1185
31.12
Regular expression iterators
1190
31.13
Modified ECMAScript regular expression grammar
1195
Contents
vi
32
Atomic operations library
1198
32.1
General
1198
32.2
Header <atomic> synopsis
1198
32.3
Type aliases
1201
32.4
Order and consistency . .
1201
32.5
Lock-free property
1203
32.6
Class template atomic . .
1204
32.7
Non-member functions . .
1211
32.8
Flag type and operations .
1212
32.9
Fences
1213
33
Thread support library
1214
33.1
General
1214
33.2
Requirements
1214
33.3
Threads
1216
33.4
Mutual exclusion
1221
33.5
Condition variables
1239
33.6
Futures
1245
A Grammar summary
1259
A.1
Keywords
1259
A.2
Lexical conventions
1259
A.3
Basic concepts
1263
A.4
Expressions
1263
A.5
Statements
1267
A.6
Declarations
1268
A.7
Declarators
1272
A.8
Classes
1274
A.9
Derived classes
1275
A.10
Special member functions
1275
A.11
Overloading
1275
A.12
Templates
1275
A.13
Exception handling
1277
A.14
Preprocessing directives .
1277
B Implementation quantities
1279
C Compatibility
1281
C.1
C++ and ISO C
1281
C.2
C++ and ISO C++ 2003
1289
C.3
C++ and ISO C++ 2011
1295
C.4
C++ and ISO C++ 2014
1296
C.5
C++ and ISO C++ 2017
1300
C.6
C standard library
1301
D Compatibility features
1303
D.1
Redeclaration of static constexpr data members .
1303
D.2
Implicit declaration of copy functions
1303
D.3
Deprecated exception specifications
1303
D.4
C++ standard library headers
1303
D.5
C standard library headers
1304
D.6
Relational operators
1304
D.7
char* streams
1305
D.8
uncaught_exception
1312
D.9
Old adaptable function bindings
1312
D.10
The default allocator
1317
D.11
Raw storage iterator
1318
D.12
Temporary buffers
1319
D.13
Deprecated type traits
1319
Contents
vii
D.14
Deprecated iterator primitives
1320
D.15
Deprecated shared_ptr observers
1320
D.16
Deprecated shared_ptr atomic access
1320
D.17
Deprecated standard code conversion facets
1322
D.18
Deprecated convenience conversion interfaces
1324
Bibliography
1328
Cross references
1329
Cross references from ISO C++ 2017
1348
Index
1349
Index of grammar productions
1379
Index of library headers
1382
Index of library names
1384
Index of implementation-defined behavior
1437
Contents
viii
1
Scope
[intro.scope]
1
This document specifies requirements for implementations of the C++ programming language. The first such
requirement is that they implement the language, so this document also defines C++. Other requirements
and relaxations of the first requirement appear at various places within this document.
2
C++ is a general purpose programming language based on the C programming language as described in
ISO/IEC 9899:2011 Programming languages — C (hereinafter referred to as the C standard). In addition to
the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces,
operator overloading, function name overloading, references, free store management operators, and additional
library facilities.
Scope
1
2
Normative references
[intro.refs]
1
The following documents are referred to in the text in such a way that some or all of their content constitutes
requirements 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 9945:2003, Information Technology — Portable Operating System Interface (POSIX)
(1.5)
—
ISO/IEC 10646-1:1993, Information technology — Universal Multiple-Octet Coded Character Set (UCS)
— Part 1: Architecture and Basic Multilingual Plane
(1.6)
—
ISO/IEC/IEEE 60559:2011, Information technology — Microprocessor Systems — Floating-Point
arithmetic
(1.7)
—
ISO 80000-2:2009, Quantities and units — Part 2: Mathematical signs and symbols to be used in the
natural 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) With the qualifications noted in Clause 21 through Clause 33 and in C.6, the C standard library is a subset of the C++
standard library.
Normative references
2
3
Terms and definitions
[intro.defs]
1
For the purposes of this document, the terms and definitions given in ISO/IEC 2382-1:1993, the terms,
definitions, and symbols given in ISO 80000-2:2009, and the following apply.
2
ISO and IEC maintain terminological databases for use in standardization at the following addresses:
(2.1)
—
ISO Online browsing platform: available at http://www.iso.org/obp
(2.2)
—
IEC Electropedia: available at http://www.electropedia.org/
3
20.3 defines additional terms that are used only in Clause 20 through Clause 33 and Annex D.
4
Terms that are used only in a small portion of this document are defined where they are used and italicized
where they are defined.
3.1
[defns.access]
access
〈execution-time action〉 read or modify the value of an object
3.2
[defns.argument]
argument
〈function call expression〉 expression in the comma-separated list bounded by the parentheses (8.5.1.2)
3.3
[defns.argument.macro]
argument
〈function-like macro〉 sequence of preprocessing tokens in the comma-separated list bounded by the parentheses
(19.3)
3.4
[defns.argument.throw]
argument
〈throw expression〉 operand of throw (8.5.17)
3.5
[defns.argument.templ]
argument
〈template instantiation〉 constant-expression, type-id, or id-expression in the comma-separated list bounded
by the angle brackets (17.3)
3.6
[defns.block]
block
wait for some condition (other than for the implementation to execute the execution steps of the thread of
execution) to be satisfied before continuing execution past the blocking operation
3.7
[defns.cond.supp]
conditionally-supported
program construct that an implementation is not required to support
[Note 1 to entry: Each implementation documents all conditionally-supported constructs that it does not
support.
— end note ]
3.8
[defns.diagnostic]
diagnostic message
message belonging to an implementation-defined subset of the implementation’s output messages
3.9
[defns.dynamic.type]
dynamic type
〈glvalue〉 type of the most derived object (6.6.2) to which the glvalue refers
[ Example: If a pointer (11.3.1) p whose static type is “pointer to class B” is pointing to an object of class D,
derived from B (Clause 13), the dynamic type of the expression *p is “D”. References (11.3.2) are treated
similarly.
— end example ]
§ 3.9
3
3.10
[defns.dynamic.type.prvalue]
dynamic type
〈prvalue〉 static type of the prvalue expression
3.11
[defns.ill.formed]
ill-formed program
program that is not well-formed (3.29)
3.12
[defns.impl.defined]
implementation-defined behavior
behavior, for a well-formed program construct and correct data, that depends on the implementation and
that each implementation documents
3.13
[defns.impl.limits]
implementation limits
restrictions imposed upon programs by the implementation
3.14
[defns.locale.specific]
locale-specific behavior
behavior that depends on local conventions of nationality, culture, and language that each implementation
documents
3.15
[defns.multibyte]
multibyte character
sequence of one or more bytes representing a member of the extended character set of either the source or
the execution environment
[ Note 1 to entry: The extended character set is a superset of the basic character set (5.3).
— end note ]
3.16
[defns.parameter]
parameter
〈function or catch clause〉 object or reference declared as part of a function declaration or definition or in the
catch clause of an exception handler that acquires a value on entry to the function or handler
3.17
[defns.parameter.macro]
parameter
〈function-like macro〉 identifier from the comma-separated list bounded by the parentheses immediately
following the macro name
3.18
[defns.parameter.templ]
parameter
〈template〉 member of a template-parameter-list
3.19
[defns.signature]
signature
〈function〉 name, parameter type list (11.3.5), enclosing namespace (if any), and trailing requires-clause (Clause
11)
(if any)
[ Note 1 to entry: Signatures are used as a basis for name mangling and linking.
— end note ]
3.20
[defns.signature.templ]
signature
〈function template〉 name, parameter type list (11.3.5), enclosing namespace (if any), return type, template-
head, and trailing requires-clause (Clause 11) (if any)
3.21
[defns.signature.spec]
signature
〈function template specialization〉 signature of the template of which it is a specialization and its template
arguments (whether explicitly specified or deduced)
§ 3.21
4
3.22
[defns.signature.member]
signature
〈class member function〉 name, parameter type list (11.3.5), class of which the function is a member,
cv-qualifiers (if any), ref-qualifier (if any), and trailing requires-clause (Clause 11) (if any)
3.23
[defns.signature.member.templ]
signature
〈class member function template〉 name, parameter type list (11.3.5), class of which the function is a
member, cv-qualifiers (if any), ref-qualifier (if any), return type (if any), template-head, and trailing requires-
clause (Clause 11) (if any)
3.24
[defns.signature.member.spec]
signature
〈class member function template specialization〉 signature of the member function template of which it is a
specialization and its template arguments (whether explicitly specified or deduced)
3.25
[defns.static.type]
static type
type of an expression (6.7) resulting from analysis of the program without considering execution semantics
[Note 1 to entry: The static type of an expression depends only on the form of the program in which the
expression appears, and does not change while the program is executing.
— end note ]
3.26
[defns.unblock]
unblock
satisfy a condition that one or more blocked threads of execution are waiting for
3.27
[defns.undefined]
undefined behavior
behavior for which this document imposes no requirements
[ Note 1 to entry: Undefined behavior may be expected when this document omits any explicit definition of
behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior
ranges from ignoring the situation completely with unpredictable results, to behaving during translation or
program execution in a documented manner characteristic of the environment (with or without the issuance
of 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.
Evaluation of a constant expression never exhibits behavior explicitly specified as undefined (8.6).
— end
note ]
3.28
[defns.unspecified]
unspecified behavior
behavior, for a well-formed program construct and correct data, that depends on the implementation
[Note 1 to entry: The implementation is not required to document which behavior occurs. The range of
possible behaviors is usually delineated by this document.
— end note ]
3.29
[defns.well.formed]
well-formed program
C++ program constructed according to the syntax rules, diagnosable semantic rules, and the one-definition
rule (6.2)
§ 3.29
5
4
General principles
[intro]
4.1
Implementation compliance
[intro.compliance]
1
The set of diagnosable rules consists of all syntactic and semantic rules in this document except for those
rules containing an explicit notation that “no diagnostic is required” or which are described as resulting in
“undefined behavior”.
2
Although this document states only requirements on C++ implementations, those requirements are often
easier to understand if they are phrased as requirements on programs, parts of programs, or execution of
programs. Such requirements have the following meaning:
(2.1)
—
If a program contains no violations of the rules in this document, a conforming implementation 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 in
this document 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 document places no
requirement on implementations with respect to that program.
[Note: During template argument deduction and substitution, certain constructs that in other contexts
require a diagnostic are treated differently; see 17.9.2.
— end note ]
3
For classes and class templates, the library Clauses specify partial definitions. Private members (Clause 14)
are not specified, but each implementation shall supply them to complete the definitions according to the
description 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 (10.3). A C++ translation unit (5.2) obtains access
to these names by including the appropriate standard library header (19.2).
6
The templates, classes, functions, and objects in the library have external linkage (6.5). The implementation
provides definitions for standard library entities, as necessary, while combining translation units to form a
complete C++ program (5.2).
7
Two kinds of implementations are defined: a hosted implementation and a freestanding implementation. For a
hosted implementation, this document defines the set of available libraries. A freestanding implementation is
one in which execution may take place without the benefit of an operating system, and has an implementation-
defined set of libraries that includes certain language-support libraries (20.5.1.3).
8
A conforming implementation may have extensions (including additional library functions), provided they
do not alter the behavior of any well-formed program. Implementations are required to diagnose programs
that use such extensions that are ill-formed according to this document. Having done so, however, they can
compile and execute such programs.
9
Each implementation shall include documentation that identifies all conditionally-supported constructs that
it does not support and defines all locale-specific characteristics.3
4.1.1
Abstract machine
[intro.abstract]
1
The semantic descriptions in this document define a parameterized nondeterministic abstract machine. This
document places no requirement on the structure of conforming implementations. In particular, they need
not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required
to emulate (only) the observable behavior of the abstract machine as explained below.4
2) “Correct execution” can include undefined behavior, depending on the data being processed; see Clause 3 and 6.8.1.
3) This documentation also defines implementation-defined behavior; see 6.8.1.
4) This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this
document as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable
behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that
its value is not used and that no side effects affecting the observable behavior of the program are produced.
§ 4.1.1
6
2
Certain aspects and operations of the abstract machine are described in this document as implementation-
defined (for example, sizeof(int)). These constitute the parameters of the abstract machine. Each
implementation shall include documentation describing its characteristics and behavior in these respects.5
Such documentation shall define the instance of the abstract machine that corresponds to that implementation
(referred to as the “corresponding instance” below).
3
Certain other aspects and operations of the abstract machine are described in this document as unspeci-
fied (for example, evaluation of expressions in a new-initializer if the allocation function fails to allocate
memory (8.5.2.4)). Where possible, this document defines a set of allowable behaviors. These define the
nondeterministic aspects of the abstract machine. An instance of the abstract machine can thus have more
than one possible execution for a given program and a given input.
4
Certain other operations are described in this document as undefined (for example, the effect of attempting
to modify a const object). [ Note: This document imposes no requirements on the behavior of programs that
contain undefined behavior.
— end note ]
5
A conforming implementation executing a well-formed program shall produce the same observable behavior as
one of the possible executions of the corresponding instance of the abstract machine with the same program
and the same input. However, if any such execution contains an undefined operation, this document places
no requirement on the implementation executing that program with that input (not even with regard to
operations preceding the first undefined operation).
6
The least requirements on a conforming implementation are:
(6.1)
—
Accesses through volatile glvalues are evaluated strictly according to the rules of the abstract machine.
(6.2)
—
At program termination, all data written into files shall be identical to one of the possible results that
execution of the program according to the abstract semantics would have produced.
(6.3)
—
The input and output dynamics of interactive devices shall take place in such a fashion that prompting
output is actually delivered before a program waits for input. What constitutes an interactive device is
implementation-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.
— end
note ]
7
[Note: Operators can be regrouped according to the usual mathematical rules only where the operators
really are associative or commutative.6 For example, in the following fragment
int a, b;
/* ... */
a = a + 32760 + b + 5;
the expression statement behaves exactly the same as
a = (((a + 32760) + b) + 5);
due to the associativity and precedence of these operators. Thus, the result of the sum (a + 32760) is next
added to b, and that result is then added to 5 which results in the value assigned to a. On a machine in which
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 exception
while the original expression would not; nor can the expression be rewritten either as
a = ((a + 32765) + b);
or
a = (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 in
which overflows do not produce an exception and in which the results of overflows are reversible, the above
expression statement can be rewritten by the implementation in any of the above ways because the same
result will occur.
— end note ]
5) This documentation also includes conditionally-supported constructs and locale-specific behavior. See 4.1.
6) Overloaded operators are never assumed to be associative or commutative.
§ 4.1.1
7
4.2
Structure of this document
[intro.structure]
1
Clause 5 through Clause 19 describe the C++ programming language. That description includes detailed
syntactic specifications in a form described in 4.3. For convenience, Annex A repeats all such syntactic
specifications.
2
Clause 21 through Clause 33 and Annex D (the library clauses) describe the C++ standard library. That
description includes detailed descriptions of the entities and macros that constitute the library, in a form
described in Clause 20.
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 D
describes those features.
5
Throughout this document, each example is introduced by “[ Example: ” and terminated by “ — end example ]”.
Each note is introduced by “[ Note: ” and terminated by “ — end note ]”. Examples and notes may be nested.
4.3
Syntax notation
[syntax]
1
In the syntax notation used in this document, syntactic categories are indicated by italic type, and literal
words and characters in constant width type. Alternatives are listed on separate lines except in a few cases
where a long set of alternatives is marked by the phrase “one of”. If the text of an alternative is too long to
fit on a line, the text is continued on subsequent lines indented from the first one. An optional terminal 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:
(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., identifier-list is a sequence of identifiers
separated by commas).
4.4
Acknowledgments
[intro.ack]
1
The C++ programming language as described in this document is based on the language as described in
Chapter R (Reference Manual) of Stroustrup: The C++ Programming Language (second edition, Addison-
Wesley Publishing Company, ISBN 0-201-53992-6, copyright ©1991 AT&T). That, in turn, is based on the C
programming language as described in Appendix A of Kernighan and Ritchie: The C Programming Language
(Prentice-Hall, 1978, ISBN 0-13-110163-3, copyright ©1978 AT&T).
2
Portions of the library Clauses of this document are based on work by P.J. Plauger, which was published as
The Draft Standard C++ Library (Prentice-Hall, ISBN 0-13-117003-1, copyright ©1995 P.J. Plauger).
3
POSIX® is a registered trademark of the Institute of Electrical and Electronic Engineers, Inc.
4
ECMAScript® is a registered trademark of Ecma International.
5
All rights in these originals are reserved.
§ 4.4
8
5
Lexical conventions
[lex]
5.1
Separate translation
[lex.separate]
1
The text of the program is kept in units called source files in this document. A source file together with
all the headers (20.5.1.2) and source files included (19.2) via the preprocessing directive #include, less any
source lines skipped by any of the conditional inclusion (19.1) preprocessing directives, is called a translation
unit. [ Note: A C++ program need not all be translated at the same time.
— end note ]
2
[Note: Previously translated translation units and instantiation units can be preserved individually or in
libraries. The separate translation units of a program communicate (6.5) by (for example) calls to functions
whose identifiers have external linkage, manipulation of objects whose identifiers have external linkage, or
manipulation of data files. Translation units can be separately translated and then later linked to produce an
executable program (6.5).
— end note ]
5.2
Phases of translation
[lex.phases]
1
The precedence among the syntax rules of translation is specified by the following phases.7
1. Physical source file characters are mapped, in an implementation-defined manner, to the basic source
character set (introducing new-line characters for end-of-line indicators) if necessary. The set of physical
source file characters accepted is implementation-defined. Any source file character not in the basic
source character set (5.3) is replaced by the universal-character-name that designates that character.
An implementation may use any internal encoding, so long as an actual extended character encountered
in the source file, and the same extended character expressed in the source file as a universal-character-
name (e.g., using the \uXXXX notation), are handled equivalently except where this replacement is
reverted (5.4) in a raw string literal.
2. Each instance of a backslash character (\) immediately followed by a new-line character is deleted,
splicing physical source lines to form logical source lines. Only the last backslash on any physical source
line shall be eligible for being part of such a splice. Except for splices reverted in a raw string literal,
if a splice results in a character sequence that matches the syntax of a universal-character-name, the
behavior is undefined. A source file that is not empty and that does not end in a new-line character,
or that ends in a new-line character immediately preceded by a backslash character before any such
splicing takes place, shall be processed as if an additional new-line character were appended to the file.
3. The source file is decomposed into preprocessing tokens (5.4) and sequences of white-space characters
(including comments). A source file shall not end in a partial preprocessing token or in a partial
comment.8
Each comment is replaced by one space character. New-line characters are retained.
Whether each nonempty sequence of white-space characters other than new-line is retained or replaced
by one space character is unspecified. The process of dividing a source file’s characters into preprocessing
tokens is context-dependent. [ Example: See the handling of < within a #include preprocessing directive.
— end example ]
4. Preprocessing directives are executed, macro invocations are expanded, and _Pragma unary operator
expressions are executed. If a character sequence that matches the syntax of a universal-character-name
is produced by token concatenation (19.3.3), the behavior is undefined. A #include preprocessing
directive causes the named header or source file to be processed from phase 1 through phase 4, recursively.
All preprocessing directives are then deleted.
5. Each source character set member in a character literal or a string literal, as well as each escape
sequence and universal-character-name in a character literal or a non-raw string literal, is converted to
the corresponding member of the execution character set (5.13.3, 5.13.5); if there is no corresponding
member, it is converted to an implementation-defined member other than the null (wide) character.9
7) Implementations must behave as if these separate phases occur, although in practice different phases might be folded
together.
8) A partial preprocessing token would arise from a source file ending in the first portion of a multi-character token that
requires a terminating sequence of characters, such as a header-name that is missing the closing " or >. A partial comment
would arise from a source file ending with an unclosed /* comment.
9) An implementation need not convert all non-corresponding source characters to the same execution character.
§ 5.2
9
6. Adjacent string literal tokens are concatenated.
7. White-space characters separating tokens are no longer significant. Each preprocessing token is converted
into a token (5.6). The resulting tokens are syntactically and semantically analyzed and translated
as a translation unit. [ Note: The process of analyzing and translating the tokens may occasionally
result in one token being replaced by a sequence of other tokens (17.2). — end note ]
[Note: Source
files, translation units and translated translation units need not necessarily be stored as files, nor need
there be any one-to-one correspondence between these entities and any external representation. The
description is conceptual only, and does not specify any particular implementation.
— end note ]
8. Translated translation units and instantiation units are combined as follows: [ Note: Some or all of
these may be supplied from a library.
— end note ] Each translated translation unit is examined to
produce a list of required instantiations.
[Note: This may include instantiations which have been
explicitly requested (17.8.2).
— end note ] The definitions of the required templates are located.
It is implementation-defined whether the source of the translation units containing these definitions
is required to be available.
[Note: An implementation could encode sufficient information into the
translated translation unit so as to ensure the source is not required here.
— end note ] All the required
instantiations are performed to produce instantiation units. [Note: These are similar to translated
translation units, but contain no references to uninstantiated templates and no template definitions.
— end note ] The program is ill-formed if any instantiation fails.
9. All external entity references are resolved. Library components are linked to satisfy external references
to entities not defined in the current translation. All such translator output is collected into a program
image which contains information needed for execution in its execution environment.
5.3
Character sets
[lex.charset]
1
The basic source character set consists of 96 characters: the space character, the control characters representing
horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters:10
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " ’
2
The universal-character-name construct provides a way to name other characters.
hex-quad:
hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit
universal-character-name:
\u hex-quad
\U hex-quad hex-quad
The character designated by the universal-character-name \UNNNNNNNN is that character whose character
short name in ISO/IEC 10646 is NNNNNNNN; the character designated by the universal-character-name \uNNNN
is that character whose character short name in ISO/IEC 10646 is 0000NNNN. If the hexadecimal value for a
universal-character-name corresponds to a surrogate code point (in the range 0xD800-0xDFFF, inclusive),
the program is ill-formed. Additionally, if the hexadecimal value for a universal-character-name outside the
c-char-sequence, s-char-sequence, or r-char-sequence of a character or string literal corresponds to a control
character (in either of the ranges 0x00-0x1F or 0x7F-0x9F, both inclusive) or to a character in the basic
source character set, the program is ill-formed.11
3
The basic execution character set and the basic execution wide-character set shall each contain all the
members of the basic source character set, plus control characters representing alert, backspace, and carriage
return, plus a null character (respectively, null wide character ), whose value is 0. For each basic execution
character set, the values of the members shall be non-negative and distinct from one another. In both the
source and execution basic character sets, the value of each character after 0 in the above list of decimal
digits shall be one greater than the value of the previous. The execution character set and the execution
wide-character set are implementation-defined supersets of the basic execution character set and the basic
10) The glyphs for the members of the basic source character set are intended to identify characters from the subset of ISO/IEC
10646 which corresponds to the ASCII character set. However, because the mapping from source file characters to the source
character set (described in translation phase 1) is specified as implementation-defined, an implementation is required to document
how the basic source characters are represented in source files.
11) A sequence of characters resembling a universal-character-name in an r-char-sequence (5.13.5) does not form a universal-
character-name.
§ 5.3
10
execution wide-character set, respectively. The values of the members of the execution character sets and the
sets of additional members are locale-specific.
5.4
Preprocessing tokens
[lex.pptoken]
preprocessing-token:
header-name
identifier
pp-number
character-literal
user-defined-character-literal
string-literal
user-defined-string-literal
preprocessing-op-or-punc
each non-white-space character that cannot be one of the above
1
Each preprocessing token that is converted to a token (5.6) shall have the lexical form of a keyword, an
identifier, a literal, an operator, or a punctuator.
2
A preprocessing token is the minimal lexical element of the language in translation phases 3 through 6. The
categories of preprocessing token are: header names, identifiers, preprocessing numbers, character literals
(including user-defined character literals), string literals (including user-defined string literals), preprocessing
operators and punctuators, and single non-white-space characters that do not lexically match the other
preprocessing token categories. If a ’ or a " character matches the last category, the behavior is undefined.
Preprocessing tokens can be separated by white space; this consists of comments (5.7), or white-space
characters (space, horizontal tab, new-line, vertical tab, and form-feed), or both. As described in Clause
19, in certain circumstances during translation phase 4, white space (or the absence thereof) serves as more
than preprocessing token separation. White space can appear within a preprocessing token only as part of a
header name or between the quotation characters in a character literal or string literal.
3
If the input stream has been parsed into preprocessing tokens up to a given character:
—
(3.1)
If the next character begins a sequence of characters that could be the prefix and initial double quote
of a raw string literal, such as R", the next preprocessing token shall be a raw string literal. Between
the initial and final double quote characters of the raw string, any transformations performed in phases
1 and 2 (universal-character-names and line splicing) are reverted; this reversion shall apply before any
d-char, r-char, or delimiting parenthesis is identified. The raw string literal is defined as the shortest
sequence of characters that matches the raw-string pattern
encoding-prefixopt R raw-string
(3.2)
—
Otherwise, if the next three characters are <:: and the subsequent character is neither : nor >, the <
is treated as a preprocessing token by itself and not as the first character of the alternative token <:.
(3.3)
—
Otherwise, the next preprocessing token is the longest sequence of characters that could constitute
a preprocessing token, even if that would cause further lexical analysis to fail, except that a header-
name (5.8) is only formed within a #include directive (19.2).
[ Example:
#define R "x"
const char* s = R"y";
// ill-formed raw string, not "x" "y"
— end example ]
4
[Example: The program fragment 0xe+foo is parsed as a preprocessing number token (one that is not a
valid floating or integer literal token), even though a parse as three preprocessing tokens 0xe, +, and foo
might produce a valid expression (for example, if foo were a macro defined as 1). Similarly, the program
fragment 1E1 is parsed as a preprocessing number (one that is a valid floating literal token), whether or not
E is a macro name.
— end example ]
5
[ Example: The program fragment x+++++y is parsed as x ++ ++ + y, which, if x and y have integral types,
violates a constraint on increment operators, even though the parse x ++ + ++ y might yield a correct
expression.
— end example ]
§ 5.4
11
5.5
Alternative tokens
[lex.digraph]
1
Alternative token representations are provided for some operators and punctuators.12
2
In all respects of the language, each alternative token behaves the same, respectively, as its primary token,
except for its spelling.13 The set of alternative tokens is defined in Table 1.
Table 1 — Alternative tokens
Alternative Primary
Alternative Primary
Alternative Primary
<%
{
and
&&
and_eq
&=
%>
}
bitor
|
or_eq
|=
<:
[
or
||
xor_eq
^=
:>
]
xor
^
not
!
%:
#
compl
~
not_eq
!=
%:%:
##
bitand
&
5.6
Tokens
[lex.token]
token:
identifier
keyword
literal
operator
punctuator
1
There are five kinds of tokens: identifiers, keywords, literals,14 operators, and other separators. Blanks,
horizontal and vertical tabs, newlines, formfeeds, and comments (collectively, “white space”), as described
below, are ignored except as they serve to separate tokens. [ Note: Some white space is required to separate
otherwise adjacent identifiers, keywords, numeric literals, and alternative tokens containing alphabetic
characters.
— end note ]
5.7
Comments
[lex.comment]
1
The characters /* start a comment, which terminates with the characters */. These comments do not nest.
The characters // start a comment, which terminates immediately before the next new-line character. If
there is a form-feed or a vertical-tab character in such a comment, only white-space characters shall appear
between it and the new-line that terminates the comment; no diagnostic is required. [ Note: The comment
characters //, /*, and */ have no special meaning within a // comment and are treated just like other
characters. Similarly, the comment characters // and /* have no special meaning within a /* comment.
— end note ]
5.8
Header names
[lex.header]
header-name:
< h-char-sequence >
" q-char-sequence "
h-char-sequence:
h-char
h-char-sequence h-char
h-char:
any member of the source character set except new-line and >
q-char-sequence:
q-char
q-char-sequence q-char
q-char:
any member of the source character set except new-line and "
12) These include “digraphs” and additional reserved words. The term “digraph” (token consisting of two characters) is not
perfectly descriptive, since one of the alternative preprocessing-tokens is %:%: and of course several primary tokens contain two
characters. Nonetheless, those alternative tokens that aren’t lexical keywords are colloquially known as “digraphs”.
13) Thus the “stringized” values (19.3.2) of [ and <: will be different, maintaining the source spelling, but the tokens can
otherwise be freely interchanged.
14) Literals include strings and character and numeric literals.
§ 5.8
12
1
[Note: Header name preprocessing tokens only appear within a #include preprocessing directive (see 5.4).
— end note ] The sequences in both forms of header-names are mapped in an implementation-defined manner
to headers or to external source file names as specified in 19.2.
2
The appearance of either of the characters ’ or \ or of either of the character sequences /* or // in a
q-char-sequence or an h-char-sequence is conditionally-supported with implementation-defined semantics, as
is the appearance of the character " in an h-char-sequence.15
5.9
Preprocessing numbers
[lex.ppnumber]
pp-number:
digit
. digit
pp-number digit
pp-number identifier-nondigit
pp-number ’ digit
pp-number ’ nondigit
pp-number e sign
pp-number E sign
pp-number p sign
pp-number P sign
pp-number .
1
Preprocessing number tokens lexically include
all integer literal tokens (5.13.2) and all floating literal
tokens (5.13.4).
2
A preprocessing number does not have a type or a value; it acquires both after a successful conversion to an
integer literal token or a floating literal token.
5.10
Identifiers
[lex.name]
identifier:
identifier-nondigit
identifier identifier-nondigit
identifier digit
identifier-nondigit:
nondigit
universal-character-name
nondigit: one of
a b c d e f g h i j k l m
n o p q r s t u v w x y z
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z _
digit: one of
0 1 2 3 4 5 6 7 8 9
1
An identifier is an arbitrarily long sequence of letters and digits. Each universal-character-name in an
identifier shall designate a character whose encoding in ISO 10646 falls into one of the ranges specified in
Table 2. The initial element shall not be a universal-character-name designating a character whose encoding
falls into one of the ranges specified in Table 3. Upper- and lower-case letters are different. All characters are
significant.16
2
The identifiers in Table 4 have a special meaning when appearing in a certain context. When referred to
in the grammar, these identifiers are used explicitly rather than using the identifier grammar production.
Unless otherwise specified, any ambiguity as to whether a given identifier has a special meaning is resolved
to interpret the token as a regular identifier.
3
In addition, some identifiers are reserved for use by C++ implementations and shall not be used otherwise; no
diagnostic is required.
15) Thus, a sequence of characters that resembles an escape sequence might result in an error, be interpreted as the character
corresponding to the escape sequence, or have a completely different meaning, depending on the implementation.
16) On systems in which linkers cannot accept extended characters, an encoding of the universal-character-name may be used
in forming valid external identifiers. For example, some otherwise unused character or sequence of characters may be used to
encode the \u in a universal-character-name. Extended characters may produce a long external identifier, but C++ does not
place a translation limit on significant characters for external identifiers. In C++, upper- and lower-case letters are considered
different for all identifiers, including external identifiers.
§ 5.10
13
Table 2 — Ranges of characters allowed
00A8
00AA
00AD
00AF
00B2-00B5
00B7-00BA
00BC-00BE
00C0-00D6
00D8-00F6
00F8-00FF
0100-167F
1681-180D
180F-1FFF
200B-200D
202A-202E
203F-2040
2054
2060-206F
2070-218F
2460-24FF
2776-2793
2C00-2DFF
2E80-2FFF
3004-3007
3021-302F
3031-D7FF
F900-FD3D
FD40-FDCF
FDF0-FE44
FE47-FFFD
10000-1FFFD
20000-2FFFD
30000-3FFFD
40000-4FFFD
50000-5FFFD
60000-6FFFD
70000-7FFFD
80000-8FFFD
90000-9FFFD
A0000-AFFFD
B0000-BFFFD
C0000-CFFFD
D0000-DFFFD
E0000-EFFFD
Table 3 — Ranges of characters disallowed initially (combining characters)
0300-036F
1DC0-1DFF
20D0-20FF FE20-FE2F
Table 4 — Identifiers with special meaning
override final
(3.1)
—
Each identifier that contains a double underscore __ or begins with an underscore followed by an
uppercase letter is reserved to the implementation for any use.
(3.2)
—
Each identifier that begins with an underscore is reserved to the implementation for use as a name in
the global namespace.
5.11
Keywords
[lex.key]
1
The identifiers shown in Table 5 are reserved for use as keywords (that is, they are unconditionally treated as
keywords in phase 7) except in an attribute-token (10.6.1):
Table 5 — Keywords
alignas
const_cast
for
public
thread_local
alignof
continue
friend
register
throw
asm
decltype
goto
reinterpret_cast
true
auto
default
if
requires
try
bool
delete
inline
return
typedef
break
do
int
short
typeid
case
double
long
signed
typename
catch
dynamic_cast
mutable
sizeof
union
char
else
namespace
static
unsigned
char16_t
enum
new
static_assert
using
char32_t
explicit
noexcept
static_cast
virtual
class
export
nullptr
struct
void
concept
extern
operator
switch
volatile
const
false
private
template
wchar_t
constexpr
float
protected
this
while
[ Note: The export and register keywords are unused but are reserved for future use. — end note ]
2
Furthermore, the alternative representations shown in Table 6 for certain operators and punctuators (5.5)
are reserved and shall not be used otherwise:
Table 6 — Alternative representations
and
and_eq bitand bitor compl not
not_eq
or
or_eq xor
xor_eq
§ 5.11
14
5.12
Operators and punctuators
[lex.operators]
1
The lexical representation of C++ programs includes a number of preprocessing tokens which are used in the
syntax of the preprocessor or are converted into tokens for operators and punctuators:
preprocessing-op-or-punc: one of
{
}
[
]
#
##
(
)
<:
:>
<%
%>
%:
%:%:
;
:
new
delete
?
::
.*
->
->*
~
!
+
-
/
%
^
&
|
=
+=
-=
*=
/=
%=
^=
&=
|=
==
!=
<
>
<=
>=
<=>
&&
||
<<
>>
<<=
>>=
++
--
,
and
or
xor
not
bitand
bitor
compl
and_eq
or_eq
xor_eq
not_eq
Each preprocessing-op-or-punc is converted to a single token in translation phase
7
(5.2).
5.13
Literals
[lex.literal]
5.13.1
Kinds of literals
[lex.literal.kinds]
1
There are several kinds of literals.17
literal:
integer-literal
character-literal
floating-literal
string-literal
boolean-literal
pointer-literal
user-defined-literal
5.13.2
Integer literals
[lex.icon]
integer-literal:
binary-literal integer-suffixopt
octal-literal integer-suffixopt
decimal-literal integer-suffixopt
hexadecimal-literal integer-suffixopt
binary-literal:
0b binary-digit
0B binary-digit
binary-literal ’opt binary-digit
octal-literal:
0
octal-literal ’opt octal-digit
decimal-literal:
nonzero-digit
decimal-literal ’opt digit
hexadecimal-literal:
hexadecimal-prefix hexadecimal-digit-sequence
binary-digit:
0
1
octal-digit: one of
0 1 2 3 4 5 6 7
nonzero-digit: one of
1 2 3 4 5 6 7 8 9
hexadecimal-prefix: one of
0x 0X
17) The term “literal” generally designates, in this document, those tokens that are called “constants” in ISO C.
§ 5.13.2
15
hexadecimal-digit-sequence:
hexadecimal-digit
hexadecimal-digit-sequence ’opt hexadecimal-digit
hexadecimal-digit: one of
0 1 2 3 4 5 6 7 8 9
a b c d e f
A B C D E F
integer-suffix:
unsigned-suffix long-suffixopt
unsigned-suffix long-long-suffixopt
long-suffix unsigned-suffixopt
long-long-suffix unsigned-suffixopt
unsigned-suffix: one of
u U
long-suffix: one of
l L
long-long-suffix: one of
ll LL
1
An integer literal is a sequence of digits that has no period or exponent part, with optional separating single
quotes that are ignored when determining its value. An integer literal may have a prefix that specifies its base
and a suffix that specifies its type. The lexically first digit of the sequence of digits is the most significant. A
binary integer literal (base two) begins with 0b or 0B and consists of a sequence of binary digits. An octal
integer literal (base eight) begins with the digit 0 and consists of a sequence of octal digits.18
A decimal
integer literal (base ten) begins with a digit other than 0 and consists of a sequence of decimal digits. A
hexadecimal integer literal (base sixteen) begins with 0x or 0X and consists of a sequence of hexadecimal
digits, which include the decimal digits and the letters a through f and A through F with decimal values ten
through fifteen. [ Example: The number twelve can be written 12, 014, 0XC, or 0b1100. The integer literals
1048576, 1’048’576, 0X100000, 0x10’0000, and 0’004’000’000 all have the same value. — end example ]
2
The type of an integer literal is the first of the corresponding list in Table 7 in which its value can be
represented.
Table 7 — Types of integer literals
Suffix
Decimal literal
Binary, octal, or hexadecimal literal
none
int
int
long int
unsigned int
long long int
long int
unsigned long
int
long long int
unsigned long
long int
u or U
unsigned int
unsigned int
unsigned long int
unsigned long
int
unsigned long long int
unsigned long
long int
l or L
long int
long int
long long int
unsigned long
int
long long int
unsigned long
long int
Both u or U
unsigned long int
unsigned long
int
and l or L
unsigned long long int
unsigned long
long int
ll or LL
long long int
long long int
unsigned long
long int
Both u or U
unsigned long long int
unsigned long
long int
and ll or LL
18) The digits 8 and 9 are not octal digits.
§ 5.13.2
16
3
If an integer literal cannot be represented by any type in its list and an extended integer type (6.7.1) can
represent its value, it may have that extended integer type. If all of the types in the list for the integer literal
are signed, the extended integer type shall be signed. If all of the types in the list for the integer literal are
unsigned, the extended integer type shall be unsigned. If the list contains both signed and unsigned types,
the extended integer type may be signed or unsigned. A program is ill-formed if one of its translation units
contains an integer literal that cannot be represented by any of the allowed types.
5.13.3
Character literals
[lex.ccon]
character-literal:
encoding-prefixopt ’ c-char-sequence ’
encoding-prefix: one of
u8
u U L
c-char-sequence:
c-char
c-char-sequence c-char
c-char:
any member of the source character set except
the single-quote ’, backslash \, or new-line character
escape-sequence
universal-character-name
escape-sequence:
simple-escape-sequence
octal-escape-sequence
hexadecimal-escape-sequence
simple-escape-sequence: one of
\’
\"
\?
\\
\a
\b
\f
\n
\r
\t
\v
octal-escape-sequence:
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence:
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
1
A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by u8,
u, U, or L, as in u8’w’, u’x’, U’y’, or L’z’, respectively.
2
A character literal that does not begin with u8, u, U, or L is an ordinary character literal. An ordinary
character literal that contains a single c-char representable in the execution character set has type char,
with value equal to the numerical value of the encoding of the c-char in the execution character set. An
ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter
literal, or an ordinary character literal containing a single c-char not representable in the execution character
set, is conditionally-supported, has type int, and has an implementation-defined value.
3
A character literal that begins with u8, such as u8’w’, is a character literal of type char, known as a UTF-8
character literal. The value of a UTF-8 character literal is equal to its ISO 10646 code point value, provided
that the code point value is representable with a single UTF-8 code unit (that is, provided it is in the C0
Controls and Basic Latin Unicode block). If the value is not representable with a single UTF-8 code unit,
the program is ill-formed. A UTF-8 character literal containing multiple c-char s is ill-formed.
4
A character literal that begins with the letter u, such as u’x’, is a character literal of type char16_t. The
value of a char16_t character literal containing a single c-char is equal to its ISO 10646 code point value,
provided that the code point value is representable with a single 16-bit code unit (that is, provided it is in
the basic multi-lingual plane). If the value is not representable with a single 16-bit code unit, the program is
ill-formed. A char16_t character literal containing multiple c-char s is ill-formed.
5
A character literal that begins with the letter U, such as U’y’, is a character literal of type char32_t. The
value of a char32_t character literal containing a single c-char is equal to its ISO 10646 code point value. A
char32_t character literal containing multiple c-char s is ill-formed.
§ 5.13.3
17
6
A character literal that begins with the letter L, such as L’z’, is a wide-character literal. A wide-character
literal has type wchar_t.19 The value of a wide-character literal containing a single c-char has value equal
to the numerical value of the encoding of the c-char in the execution wide-character set, unless the c-char
has no representation in the execution wide-character set, in which case the value is implementation-defined.
[Note: The type wchar_t is able to represent all members of the execution wide-character set (see 6.7.1).
— end note ] The value of a wide-character literal containing multiple c-char s is implementation-defined.
7
Certain non-graphic characters, the single quote ’, the double quote ", the question mark ?,20 and the
backslash \, can be represented according to Table 8. The double quote " and the question mark ?, can
be represented as themselves or by the escape sequences \" and \? respectively, but the single quote ’
and the backslash \ shall be represented by the escape sequences \’ and \\ respectively. Escape sequences
in which the character following the backslash is not listed in Table 8 are conditionally-supported, with
implementation-defined semantics. An escape sequence specifies a single character.
Table 8 — Escape sequences
new-line
NL(LF)
\n
horizontal tab
HT
\t
vertical tab
VT
\v
backspace
BS
\b
carriage return
CR
\r
form feed
FF
\f
alert
BEL
\a
backslash
\
\\
question mark
?
\?
single quote
’
\’
double quote
"
\"
octal number
ooo
\ooo
hex number
hhh
\xhhh
8
The escape \ooo consists of the backslash followed by one, two, or three octal digits that are taken to specify
the value of the desired character. The escape \xhhh consists of the backslash followed by x followed by one
or more hexadecimal digits that are taken to specify the value of the desired character. There is no limit to
the number of digits in a hexadecimal sequence. A sequence of octal or hexadecimal digits is terminated by
the first character that is not an octal digit or a hexadecimal digit, respectively. The value of a character
literal is implementation-defined if it falls outside of the implementation-defined range defined for char (for
character literals with no prefix) or wchar_t (for character literals prefixed by L). [ Note: If the value of a
character literal prefixed by u, u8, or U is outside the range defined for its type, the program is ill-formed.
— end note ]
9
A universal-character-name is translated to the encoding, in the appropriate execution character set, of the
character named. If there is no such encoding, the universal-character-name is translated to an implementation-
defined encoding. [ Note: In translation phase 1, a universal-character-name is introduced whenever an actual
extended character is encountered in the source text. Therefore, all extended characters are described in
terms of universal-character-names. However, the actual compiler implementation may use its own native
character set, so long as the same results are obtained.
— end note ]
5.13.4
Floating literals
[lex.fcon]
floating-literal:
decimal-floating-literal
hexadecimal-floating-literal
decimal-floating-literal:
fractional-constant exponent-partopt floating-suffixopt
digit-sequence exponent-part floating-suffixopt
hexadecimal-floating-literal:
hexadecimal-prefix hexadecimal-fractional-constant binary-exponent-part floating-suffixopt
hexadecimal-prefix hexadecimal-digit-sequence binary-exponent-part floating-suffixopt
19) They are intended for character sets where a character does not fit into a single byte.
20) Using an escape sequence for a question mark is supported for compatibility with ISO C++ 2014 and ISO C.
§ 5.13.4
18
fractional-constant:
digit-sequenceopt . digit-sequence
digit-sequence .
hexadecimal-fractional-constant:
hexadecimal-digit-sequenceopt . hexadecimal-digit-sequence
hexadecimal-digit-sequence .
exponent-part:
e signopt digit-sequence
E signopt digit-sequence
binary-exponent-part:
p signopt digit-sequence
P signopt digit-sequence
sign: one of
+ -
digit-sequence:
digit
digit-sequence ’opt digit
floating-suffix: one of
f l F L
1
A floating literal consists of an optional prefix specifying a base, an integer part, a radix point, a fraction
part, an e, E, p or P, an optionally signed integer exponent, and an optional type suffix. The integer and
fraction parts both consist of a sequence of decimal (base ten) digits if there is no prefix, or hexadecimal
(base sixteen) digits if the prefix is 0x or 0X. The floating literal is a decimal floating literal in the former
case and a hexadecimal floating literal in the latter case. Optional separating single quotes in a digit-sequence
or hexadecimal-digit-sequence are ignored when determining its value.
[Example: The floating literals
1.602’176’565e-19 and 1.602176565e-19 have the same value. — end example ] Either the integer part or
the fraction part (not both) can be omitted. Either the radix point or the letter e or E and the exponent (not
both) can be omitted from a decimal floating literal. The radix point (but not the exponent) can be omitted
from a hexadecimal floating literal. The integer part, the optional radix point, and the optional fraction part,
form the significand of the floating literal. In a decimal floating literal, the exponent, if present, indicates
the power of 10 by which the significand is to be scaled. In a hexadecimal floating literal, the exponent
indicates the power of 2 by which the significand is to be scaled. [ Example: The floating literals 49.625 and
0xC.68p+2 have the same value. — end example ] If the scaled value is in the range of representable values
for its type, the result is the scaled value if representable, else the larger or smaller representable value nearest
the scaled value, chosen in an implementation-defined manner. The type of a floating literal is double unless
explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double.
If the scaled value is not in the range of representable values for its type, the program is ill-formed.
5.13.5
String literals
[lex.string]
string-literal:
encoding-prefixopt " s-char-sequenceopt "
encoding-prefixopt R raw-string
s-char-sequence:
s-char
s-char-sequence s-char
s-char:
any member of the source character set except
the double-quote ", backslash \, or new-line character
escape-sequence
universal-character-name
raw-string:
" d-char-sequenceopt ( r-char-sequenceopt ) d-char-sequenceopt "
r-char-sequence:
r-char
r-char-sequence r-char
§ 5.13.5
19
r-char:
any member of the source character set, except
a right parenthesis ) followed by the initial d-char-sequence
(which may be empty) followed by a double quote ".
d-char-sequence:
d-char
d-char-sequence d-char
d-char:
any member of the basic source character set except:
space, the left parenthesis (, the right parenthesis ), the backslash \,
and the control characters representing horizontal tab,
vertical tab, form feed, and newline.
1
A string-literal is a sequence of characters (as defined in 5.13.3) surrounded by double quotes, optionally
prefixed by R, u8, u8R, u, uR, U, UR, L, or LR, as in "...", R"(...)", u8"...", u8R"**(...)**", u"...",
uR"*~(...)*~", U"...", UR"zzz(...)zzz", L"...", or LR"(...)", respectively.
2
A string-literal that has an R in the prefix is a raw string literal. The d-char-sequence serves as a delimiter. The
terminating d-char-sequence of a raw-string is the same sequence of characters as the initial d-char-sequence.
A d-char-sequence shall consist of at most 16 characters.
3
[Note: The characters ’(’ and ’)’ are permitted in a raw-string. Thus, R"delimiter((a|b))delimiter"
is equivalent to "(a|b)".
— end note ]
4
[Note: A source-file new-line in a raw string literal results in a new-line in the resulting execution string
literal. Assuming no whitespace at the beginning of lines in the following example, the assert will succeed:
const char* p = R"(a\
b
c)";
assert(std::strcmp(p, "a\\\nb\nc") == 0);
— end note ]
5
[ Example: The raw string
R"a(
)\
a"
)a"
is equivalent to "\n)\\\na\"\n". The raw string
R"(x = "\"y\"")"
is equivalent to "x = \"\\\"y\\\"\"".
— end example ]
6
After translation phase 6, a string-literal that does not begin with an encoding-prefix is an ordinary string
literal, and is initialized with the given characters.
7
A string-literal that begins with u8, such as u8"asdf", is a UTF-8 string literal.
8
Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow
string literal has type “array of n const char”, where n is the size of the string as defined below, and has
static storage duration (6.6.4).
9
For a UTF-8 string literal, each successive element of the object representation (6.7) has the value of the
corresponding code unit of the UTF-8 encoding of the string.
10
A string-literal that begins with u, such as u"asdf", is a char16_t string literal. A char16_t string literal
has type “array of n const char16_t”, where n is the size of the string as defined below; it is initialized
with the given characters. A single c-char may produce more than one char16_t character in the form of
surrogate pairs.
11
A string-literal that begins with U, such as U"asdf", is a char32_t string literal. A char32_t string literal
has type “array of n const char32_t”, where n is the size of the string as defined below; it is initialized
with the given characters.
12
A string-literal that begins with L, such as L"asdf", is a wide string literal. A wide string literal has type
“array of n const wchar_t”, where n is the size of the string as defined below; it is initialized with the given
characters.
§ 5.13.5
20
13
In translation phase 6 (5.2), adjacent string-literals are concatenated. If both string-literals have the same
encoding-prefix, the resulting concatenated string literal has that encoding-prefix. If one string-literal has
no encoding-prefix, it is treated as a string-literal of the same encoding-prefix as the other operand. If a
UTF-8 string literal token is adjacent to a wide string literal token, the program is ill-formed. Any other
concatenations are conditionally-supported with implementation-defined behavior. [ Note: This concatenation
is an interpretation, not a conversion. Because the interpretation happens in translation phase 6 (after
each character from a string literal has been translated into a value from the appropriate character set), a
string-literal’s initial rawness has no effect on the interpretation or well-formedness of the concatenation.
— end note ] Table 9 has some examples of valid concatenations.
Table 9 — String literal concatenations
Source
Means
Source
Means
Source
Means
u"a" u"b" u"ab"
U"a" U"b" U"ab"
L"a" L"b" L"ab"
u"a" "b" u"ab"
U"a" "b" U"ab"
L"a" "b" L"ab"
"a" u"b" u"ab"
"a" U"b" U"ab"
"a" L"b" L"ab"
Characters in concatenated strings are kept distinct.
[ Example:
"\xA" "B"
contains the two characters ’\xA’ and ’B’ after concatenation (and not the single hexadecimal character
’\xAB’). — end example ]
14
After any necessary concatenation, in translation phase 7 (5.2), ’\0’ is appended to every string literal so
that programs that scan a string can find its end.
15
Escape sequences and universal-character-names in non-raw string literals have the same meaning as in
character literals (5.13.3), except that the single quote ’ is representable either by itself or by the escape
sequence \’, and the double quote " shall be preceded by a \, and except that a universal-character-name in
a char16_t string literal may yield a surrogate pair. In a narrow string literal, a universal-character-name
may map to more than one char element due to multibyte encoding. The size of a char32_t or wide string
literal is the total number of escape sequences, universal-character-names, and other characters, plus one
for the terminating U’\0’ or L’\0’. The size of a char16_t string literal is the total number of escape
sequences, universal-character-names, and other characters, plus one for each character requiring a surrogate
pair, plus one for the terminating u’\0’.
[Note: The size of a char16_t string literal is the number of
code units, not the number of characters.
— end note ] Within char32_t and char16_t string literals, any
universal-character-names shall be within the range 0x0 to 0x10FFFF. The size of a narrow string literal is
the total number of escape sequences and other characters, plus at least one for the multibyte encoding of
each universal-character-name, plus one for the terminating ’\0’.
16
Evaluating a string-literal results in a string literal object with static storage duration, initialized from
the given characters as specified above. Whether all string literals are distinct (that is, are stored in
nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different
object is unspecified. [ Note: The effect of attempting to modify a string literal is undefined.
— end note ]
5.13.6
Boolean literals
[lex.bool]
boolean-literal:
false
true
1
The Boolean literals are the keywords false and true. Such literals are prvalues and have type bool.
5.13.7
Pointer literals
[lex.nullptr]
pointer-literal:
nullptr
1
The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t
is a distinct type that is neither a pointer type nor a pointer-to-member type; rather, a prvalue of this type
is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 7.11
and 7.12.
— end note ]
§ 5.13.7
21
///////////////////////////////////////
|
|