- tmp/tmp1q9vtrp1/{from.md → to.md} +690 -526
tmp/tmp1q9vtrp1/{from.md → to.md}
RENAMED
|
@@ -1,252 +1,35 @@
|
|
| 1 |
-
# General <a id="intro">[[intro]]</a>
|
| 2 |
-
|
| 3 |
-
## Scope <a id="intro.scope">[[intro.scope]]</a>
|
| 4 |
-
|
| 5 |
-
This International Standard specifies requirements for implementations
|
| 6 |
-
of the C++programming language. The first such requirement is that they
|
| 7 |
-
implement the language, and so this International Standard also defines
|
| 8 |
-
C++. Other requirements and relaxations of the first requirement appear
|
| 9 |
-
at various places within this International Standard.
|
| 10 |
-
|
| 11 |
-
C++is a general purpose programming language based on the C programming
|
| 12 |
-
language as described in ISO/IEC 9899:1999 *Programming languages — C*
|
| 13 |
-
(hereinafter referred to as the *C standard*). In addition to the
|
| 14 |
-
facilities provided by C, C++provides additional data types, classes,
|
| 15 |
-
templates, exceptions, namespaces, operator overloading, function name
|
| 16 |
-
overloading, references, free store management operators, and additional
|
| 17 |
-
library facilities.
|
| 18 |
-
|
| 19 |
-
## Normative references <a id="intro.refs">[[intro.refs]]</a>
|
| 20 |
-
|
| 21 |
-
The following referenced documents are indispensable for the application
|
| 22 |
-
of this document. For dated references, only the edition cited applies.
|
| 23 |
-
For undated references, the latest edition of the referenced document
|
| 24 |
-
(including any amendments) applies.
|
| 25 |
-
|
| 26 |
-
- Ecma International, *ECMAScript Language Specification*, Standard
|
| 27 |
-
Ecma-262, third edition, 1999.
|
| 28 |
-
- ISO/IEC 2382 (all parts), *Information technology — Vocabulary*
|
| 29 |
-
- ISO/IEC 9899:1999, *Programming languages — C*
|
| 30 |
-
- ISO/IEC 9899:1999/Cor.1:2001(E), *Programming languages — C, Technical
|
| 31 |
-
Corrigendum 1*
|
| 32 |
-
- ISO/IEC 9899:1999/Cor.2:2004(E), *Programming languages — C, Technical
|
| 33 |
-
Corrigendum 2*
|
| 34 |
-
- ISO/IEC 9899:1999/Cor.3:2007(E), *Programming languages — C, Technical
|
| 35 |
-
Corrigendum 3*
|
| 36 |
-
- ISO/IEC 9945:2003, *Information Technology — Portable Operating System
|
| 37 |
-
Interface (POSIX)*
|
| 38 |
-
- ISO/IEC 10646-1:1993, *Information technology — Universal
|
| 39 |
-
Multiple-Octet Coded Character Set (UCS) — Part 1: Architecture and
|
| 40 |
-
Basic Multilingual Plane*
|
| 41 |
-
- ISO/IEC TR 19769:2004, *Information technology — Programming
|
| 42 |
-
languages, their environments and system software interfaces —
|
| 43 |
-
Extensions for the programming language C to support new character
|
| 44 |
-
data types*
|
| 45 |
-
|
| 46 |
-
The library described in Clause 7 of ISO/IEC 9899:1999 and Clause 7 of
|
| 47 |
-
ISO/IEC 9899:1999/Cor.1:2001 and Clause 7 of ISO/IEC
|
| 48 |
-
9899:1999/Cor.2:2003 is hereinafter called the *C standard library*.[^1]
|
| 49 |
-
|
| 50 |
-
The library described in ISO/IEC TR 19769:2004 is hereinafter called the
|
| 51 |
-
*C Unicode TR*.
|
| 52 |
-
|
| 53 |
-
The operating system interface described in ISO/IEC 9945:2003 is
|
| 54 |
-
hereinafter called *POSIX*.
|
| 55 |
-
|
| 56 |
-
The ECMAScript Language Specification described in Standard Ecma-262 is
|
| 57 |
-
hereinafter called *ECMA-262*.
|
| 58 |
-
|
| 59 |
-
## Terms and definitions <a id="intro.defs">[[intro.defs]]</a>
|
| 60 |
-
|
| 61 |
-
For the purposes of this document, the following definitions apply.
|
| 62 |
-
|
| 63 |
-
[[definitions]] defines additional terms that are used only in Clauses
|
| 64 |
-
[[library]] through [[thread]] and Annex [[depr]].
|
| 65 |
-
|
| 66 |
-
Terms that are used only in a small portion of this International
|
| 67 |
-
Standard are defined where they are used and italicized where they are
|
| 68 |
-
defined.
|
| 69 |
-
|
| 70 |
-
#### 1 argument <a id="defns.argument">[[defns.argument]]</a>
|
| 71 |
-
|
| 72 |
-
\<function call expression\> expression in the comma-separated list
|
| 73 |
-
bounded by the parentheses
|
| 74 |
-
|
| 75 |
-
#### 2 argument <a id="defns.argument.macro">[[defns.argument.macro]]</a>
|
| 76 |
-
|
| 77 |
-
\<function-like macro\> sequence of preprocessing tokens in the
|
| 78 |
-
comma-separated list bounded by the parentheses
|
| 79 |
-
|
| 80 |
-
#### 3 argument <a id="defns.argument.throw">[[defns.argument.throw]]</a>
|
| 81 |
-
|
| 82 |
-
\<throw expression\> the operand of `throw`
|
| 83 |
-
|
| 84 |
-
#### 4 argument <a id="defns.argument.templ">[[defns.argument.templ]]</a>
|
| 85 |
-
|
| 86 |
-
\<template instantiation\> expression, *type-id* or *template-name* in
|
| 87 |
-
the comma-separated list bounded by the angle brackets
|
| 88 |
-
|
| 89 |
-
#### 5 conditionally-supported <a id="defns.cond.supp">[[defns.cond.supp]]</a>
|
| 90 |
-
|
| 91 |
-
program construct that an implementation is not required to support
|
| 92 |
-
Each implementation documents all conditionally-supported constructs
|
| 93 |
-
that it does not support.
|
| 94 |
-
|
| 95 |
-
#### 6 diagnostic message <a id="defns.diagnostic">[[defns.diagnostic]]</a>
|
| 96 |
-
|
| 97 |
-
message belonging to an *implementation-defined* subset of the
|
| 98 |
-
implementation’s output messages
|
| 99 |
-
|
| 100 |
-
#### 7 dynamic type <a id="defns.dynamic.type">[[defns.dynamic.type]]</a>
|
| 101 |
-
|
| 102 |
-
\<glvalue\> type of the most derived object ([[intro.object]]) to which
|
| 103 |
-
the glvalue denoted by a glvalue expression refers
|
| 104 |
-
if a pointer ([[dcl.ptr]]) `p` whose static type is “pointer to class
|
| 105 |
-
`B`” is pointing to an object of class `D`, derived from `B` (Clause
|
| 106 |
-
[[class.derived]]), the dynamic type of the expression `*p` is “`D`.”
|
| 107 |
-
References ([[dcl.ref]]) are treated similarly.
|
| 108 |
-
|
| 109 |
-
#### 8 dynamic type <a id="defns.dynamic.type.prvalue">[[defns.dynamic.type.prvalue]]</a>
|
| 110 |
-
|
| 111 |
-
\<prvalue\> static type of the prvalue expression
|
| 112 |
-
|
| 113 |
-
#### 9 ill-formed program <a id="defns.ill.formed">[[defns.ill.formed]]</a>
|
| 114 |
-
|
| 115 |
-
program that is not well formed
|
| 116 |
-
|
| 117 |
-
#### 10 implementation-defined behavior <a id="defns.impl.defined">[[defns.impl.defined]]</a>
|
| 118 |
-
|
| 119 |
-
behavior, for a well-formed program construct and correct data, that
|
| 120 |
-
depends on the implementation and that each implementation documents
|
| 121 |
-
|
| 122 |
-
#### 11 implementation limits <a id="defns.impl.limits">[[defns.impl.limits]]</a>
|
| 123 |
-
|
| 124 |
-
restrictions imposed upon programs by the implementation
|
| 125 |
-
|
| 126 |
-
#### 12 locale-specific behavior <a id="defns.locale.specific">[[defns.locale.specific]]</a>
|
| 127 |
-
|
| 128 |
-
behavior that depends on local conventions of nationality, culture, and
|
| 129 |
-
language that each implementation documents
|
| 130 |
-
|
| 131 |
-
#### 13 multibyte character <a id="defns.multibyte">[[defns.multibyte]]</a>
|
| 132 |
-
|
| 133 |
-
sequence of one or more bytes representing a member of the extended
|
| 134 |
-
character set of either the source or the execution environment
|
| 135 |
-
The extended character set is a superset of the basic character set (
|
| 136 |
-
[[lex.charset]]).
|
| 137 |
-
|
| 138 |
-
#### 14 parameter <a id="defns.parameter">[[defns.parameter]]</a>
|
| 139 |
-
|
| 140 |
-
\<function or catch clause\> object or reference declared as part of a
|
| 141 |
-
function declaration or definition or in the catch clause of an
|
| 142 |
-
exception handler that acquires a value on entry to the function or
|
| 143 |
-
handler
|
| 144 |
-
|
| 145 |
-
#### 15 parameter <a id="defns.parameter.macro">[[defns.parameter.macro]]</a>
|
| 146 |
-
|
| 147 |
-
\<function-like macro\> identifier from the comma-separated list bounded
|
| 148 |
-
by the parentheses immediately following the macro name
|
| 149 |
-
|
| 150 |
-
#### 16 parameter <a id="defns.parameter.templ">[[defns.parameter.templ]]</a>
|
| 151 |
-
|
| 152 |
-
\<template\> *template-parameter*
|
| 153 |
-
|
| 154 |
-
#### 17 signature <a id="defns.signature">[[defns.signature]]</a>
|
| 155 |
-
|
| 156 |
-
\<function\> name, parameter type list ([[dcl.fct]]), and enclosing
|
| 157 |
-
namespace (if any)
|
| 158 |
-
Signatures are used as a basis for name mangling and linking.
|
| 159 |
-
|
| 160 |
-
#### 18 signature <a id="defns.signature.templ">[[defns.signature.templ]]</a>
|
| 161 |
-
|
| 162 |
-
\<function template\> name, parameter type list ([[dcl.fct]]),
|
| 163 |
-
enclosing namespace (if any), return type, and template parameter list
|
| 164 |
-
|
| 165 |
-
#### 19 signature <a id="defns.signature.spec">[[defns.signature.spec]]</a>
|
| 166 |
-
|
| 167 |
-
\<function template specialization\> signature of the template of which
|
| 168 |
-
it is a specialization and its template arguments (whether explicitly
|
| 169 |
-
specified or deduced)
|
| 170 |
-
|
| 171 |
-
#### 20 signature <a id="defns.signature.member">[[defns.signature.member]]</a>
|
| 172 |
-
|
| 173 |
-
\<class member function\> name, parameter type list ([[dcl.fct]]),
|
| 174 |
-
class of which the function is a member, cv-qualifiers (if any), and
|
| 175 |
-
*ref-qualifier* (if any)
|
| 176 |
-
|
| 177 |
-
#### 21 signature <a id="defns.signature.member.templ">[[defns.signature.member.templ]]</a>
|
| 178 |
-
|
| 179 |
-
\<class member function template\> name, parameter type list (
|
| 180 |
-
[[dcl.fct]]), class of which the function is a member, cv-qualifiers (if
|
| 181 |
-
any), *ref-qualifier* (if any), return type, and template parameter list
|
| 182 |
-
|
| 183 |
-
#### 22 signature <a id="defns.signature.member.spec">[[defns.signature.member.spec]]</a>
|
| 184 |
-
|
| 185 |
-
\<class member function template specialization\> signature of the
|
| 186 |
-
member function template of which it is a specialization and its
|
| 187 |
-
template arguments (whether explicitly specified or deduced)
|
| 188 |
-
|
| 189 |
-
#### 23 static type <a id="defns.static.type">[[defns.static.type]]</a>
|
| 190 |
-
|
| 191 |
-
type of an expression ([[basic.types]]) resulting from analysis of the
|
| 192 |
-
program without considering execution semantics
|
| 193 |
-
The static type of an expression depends only on the form of the program
|
| 194 |
-
in which the expression appears, and does not change while the program
|
| 195 |
-
is executing.
|
| 196 |
-
|
| 197 |
-
#### 24 undefined behavior <a id="defns.undefined">[[defns.undefined]]</a>
|
| 198 |
-
|
| 199 |
-
behavior for which this International Standard imposes no requirements
|
| 200 |
-
Undefined behavior may be expected when this International Standard
|
| 201 |
-
omits any explicit definition of behavior or when a program uses an
|
| 202 |
-
erroneous construct or erroneous data. Permissible undefined behavior
|
| 203 |
-
ranges from ignoring the situation completely with unpredictable
|
| 204 |
-
results, to behaving during translation or program execution in a
|
| 205 |
-
documented manner characteristic of the environment (with or without the
|
| 206 |
-
issuance of a diagnostic message), to terminating a translation or
|
| 207 |
-
execution (with the issuance of a diagnostic message). Many erroneous
|
| 208 |
-
program constructs do not engender undefined behavior; they are required
|
| 209 |
-
to be diagnosed.
|
| 210 |
-
|
| 211 |
-
#### 25 unspecified behavior <a id="defns.unspecified">[[defns.unspecified]]</a>
|
| 212 |
-
|
| 213 |
-
behavior, for a well-formed program construct and correct data, that
|
| 214 |
-
depends on the implementation
|
| 215 |
-
The implementation is not required to document which behavior occurs.
|
| 216 |
-
The range of possible behaviors is usually delineated by this
|
| 217 |
-
International Standard.
|
| 218 |
-
|
| 219 |
-
#### 26 well-formed program <a id="defns.well.formed">[[defns.well.formed]]</a>
|
| 220 |
-
|
| 221 |
-
C++program constructed according to the syntax rules, diagnosable
|
| 222 |
-
semantic rules, and the One Definition Rule ([[basic.def.odr]]).
|
| 223 |
|
| 224 |
## Implementation compliance <a id="intro.compliance">[[intro.compliance]]</a>
|
| 225 |
|
| 226 |
The set of *diagnosable rules* consists of all syntactic and semantic
|
| 227 |
rules in this International Standard except for those rules containing
|
| 228 |
an explicit notation that “no diagnostic is required” or which are
|
| 229 |
-
described as resulting in “undefined behavior
|
| 230 |
|
| 231 |
Although this International Standard states only requirements on C++
|
| 232 |
implementations, those requirements are often easier to understand if
|
| 233 |
they are phrased as requirements on programs, parts of programs, or
|
| 234 |
execution of programs. Such requirements have the following meaning:
|
| 235 |
|
| 236 |
- If a program contains no violations of the rules in this International
|
| 237 |
Standard, a conforming implementation shall, within its resource
|
| 238 |
limits, accept and correctly execute[^2] that program.
|
| 239 |
- If a program contains a violation of any diagnosable rule or an
|
| 240 |
-
occurrence of a construct described in this Standard as
|
| 241 |
“conditionally-supported” when the implementation does not support
|
| 242 |
that construct, a conforming implementation shall issue at least one
|
| 243 |
diagnostic message.
|
| 244 |
- If a program contains a violation of a rule for which no diagnostic is
|
| 245 |
required, this International Standard places no requirement on
|
| 246 |
implementations with respect to that program.
|
| 247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
For classes and class templates, the library Clauses specify partial
|
| 249 |
definitions. Private members (Clause [[class.access]]) are not
|
| 250 |
specified, but each implementation shall supply them to complete the
|
| 251 |
definitions according to the description in the library Clauses.
|
| 252 |
|
|
@@ -281,45 +64,44 @@ execute such programs.
|
|
| 281 |
|
| 282 |
Each implementation shall include documentation that identifies all
|
| 283 |
conditionally-supported constructs that it does not support and defines
|
| 284 |
all locale-specific characteristics.[^3]
|
| 285 |
|
| 286 |
-
## Structure of this
|
| 287 |
|
| 288 |
Clauses [[lex]] through [[cpp]] describe the C++programming language.
|
| 289 |
That description includes detailed syntactic specifications in a form
|
| 290 |
described in [[syntax]]. For convenience, Annex [[gram]] repeats all
|
| 291 |
such syntactic specifications.
|
| 292 |
|
| 293 |
Clauses [[language.support]] through [[thread]] and Annex [[depr]]
|
| 294 |
-
(the *library clauses*) describe the
|
| 295 |
-
description includes detailed descriptions of the
|
| 296 |
-
|
| 297 |
-
described in Clause [[library]].
|
| 298 |
|
| 299 |
Annex [[implimits]] recommends lower bounds on the capacity of
|
| 300 |
conforming implementations.
|
| 301 |
|
| 302 |
Annex [[diff]] summarizes the evolution of C++since its first published
|
| 303 |
description, and explains in detail the differences between C++and C.
|
| 304 |
Certain features of C++exist solely for compatibility purposes; Annex
|
| 305 |
[[depr]] describes those features.
|
| 306 |
|
| 307 |
-
Throughout this
|
| 308 |
-
|
| 309 |
-
|
| 310 |
|
| 311 |
## Syntax notation <a id="syntax">[[syntax]]</a>
|
| 312 |
|
| 313 |
-
In the syntax notation used in this
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
|
| 322 |
``` bnf
|
| 323 |
'{' expressionₒₚₜ '}'
|
| 324 |
```
|
| 325 |
|
|
@@ -333,42 +115,49 @@ the following rules:
|
|
| 333 |
- *X-id* is an identifier with no context-dependent meaning (e.g.,
|
| 334 |
*qualified-id*).
|
| 335 |
- *X-seq* is one or more *X*’s without intervening delimiters (e.g.,
|
| 336 |
*declaration-seq* is a sequence of declarations).
|
| 337 |
- *X-list* is one or more *X*’s separated by intervening commas (e.g.,
|
| 338 |
-
*
|
| 339 |
|
| 340 |
## The C++memory model <a id="intro.memory">[[intro.memory]]</a>
|
| 341 |
|
| 342 |
The fundamental storage unit in the C++memory model is the *byte*. A
|
| 343 |
byte is at least large enough to contain any member of the basic
|
| 344 |
execution character set ([[lex.charset]]) and the eight-bit code units
|
| 345 |
of the Unicode UTF-8 encoding form and is composed of a contiguous
|
| 346 |
-
sequence of bits, the number of which is *implementation-defined*.
|
| 347 |
-
least significant bit is called the *low-order bit*; the most
|
| 348 |
significant bit is called the *high-order bit*. The memory available to
|
| 349 |
a C++program consists of one or more sequences of contiguous bytes.
|
| 350 |
Every byte has a unique address.
|
| 351 |
|
| 352 |
-
The representation of types is described in
|
|
|
|
| 353 |
|
| 354 |
A *memory location* is either an object of scalar type or a maximal
|
| 355 |
-
sequence of adjacent bit-fields all having
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
A structure declared as
|
| 372 |
|
| 373 |
``` cpp
|
| 374 |
struct {
|
|
@@ -386,84 +175,172 @@ contains four separate memory locations: The field `a` and bit-fields
|
|
| 386 |
concurrently without interfering with each other. The bit-fields `b` and
|
| 387 |
`c` together constitute the fourth memory location. The bit-fields `b`
|
| 388 |
and `c` cannot be concurrently modified, but `b` and `a`, for example,
|
| 389 |
can be.
|
| 390 |
|
|
|
|
|
|
|
| 391 |
## The C++object model <a id="intro.object">[[intro.object]]</a>
|
| 392 |
|
| 393 |
The constructs in a C++program create, destroy, refer to, access, and
|
| 394 |
-
manipulate objects. An *object* is
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
[[
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 405 |
information associated with each such object that makes it possible to
|
| 406 |
determine that object’s type during program execution. For other
|
| 407 |
objects, the interpretation of the values found therein is determined by
|
| 408 |
the type of the *expression*s (Clause [[expr]]) used to access them.
|
| 409 |
|
| 410 |
Objects can contain other objects, called *subobjects*. A subobject can
|
| 411 |
be a *member subobject* ([[class.mem]]), a *base class subobject*
|
| 412 |
(Clause [[class.derived]]), or an array element. An object that is not
|
| 413 |
-
a subobject of any other object is called a *complete object*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
|
| 415 |
For every object `x`, there is some object called the *complete object
|
| 416 |
of* `x`, determined as follows:
|
| 417 |
|
| 418 |
-
- If `x` is a complete object, then
|
|
|
|
| 419 |
- Otherwise, the complete object of `x` is the complete object of the
|
| 420 |
(unique) object that contains `x`.
|
| 421 |
|
| 422 |
If a complete object, a data member ([[class.mem]]), or an array
|
| 423 |
element is of class type, its type is considered the *most derived
|
| 424 |
class*, to distinguish it from the class type of any base class
|
| 425 |
subobject; an object of a most derived class type or of a non-class type
|
| 426 |
is called a *most derived object*.
|
| 427 |
|
| 428 |
Unless it is a bit-field ([[class.bit]]), a most derived object shall
|
| 429 |
-
have a
|
| 430 |
class subobjects may have zero size. An object of trivially copyable or
|
| 431 |
standard-layout type ([[basic.types]]) shall occupy contiguous bytes of
|
| 432 |
storage.
|
| 433 |
|
| 434 |
Unless an object is a bit-field or a base class subobject of zero size,
|
| 435 |
the address of that object is the address of the first byte it occupies.
|
| 436 |
-
Two objects
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
distinct addresses.[^
|
|
|
|
|
|
|
| 440 |
|
| 441 |
``` cpp
|
| 442 |
static const char test1 = 'x';
|
| 443 |
static const char test2 = 'x';
|
| 444 |
const bool b = &test1 != &test2; // always true
|
| 445 |
```
|
| 446 |
|
| 447 |
-
|
| 448 |
-
|
|
|
|
|
|
|
|
|
|
| 449 |
|
| 450 |
## Program execution <a id="intro.execution">[[intro.execution]]</a>
|
| 451 |
|
| 452 |
The semantic descriptions in this International Standard define a
|
| 453 |
parameterized nondeterministic abstract machine. This International
|
| 454 |
Standard places no requirement on the structure of conforming
|
| 455 |
implementations. In particular, they need not copy or emulate the
|
| 456 |
structure of the abstract machine. Rather, conforming implementations
|
| 457 |
are required to emulate (only) the observable behavior of the abstract
|
| 458 |
-
machine as explained below.[^
|
| 459 |
|
| 460 |
Certain aspects and operations of the abstract machine are described in
|
| 461 |
this International Standard as implementation-defined (for example,
|
| 462 |
`sizeof(int)`). These constitute the parameters of the abstract machine.
|
| 463 |
Each implementation shall include documentation describing its
|
| 464 |
-
characteristics and behavior in these respects.[^
|
| 465 |
shall define the instance of the abstract machine that corresponds to
|
| 466 |
that implementation (referred to as the “corresponding instance” below).
|
| 467 |
|
| 468 |
Certain other aspects and operations of the abstract machine are
|
| 469 |
described in this International Standard as unspecified (for example,
|
|
@@ -474,56 +351,56 @@ define the nondeterministic aspects of the abstract machine. An instance
|
|
| 474 |
of the abstract machine can thus have more than one possible execution
|
| 475 |
for a given program and a given input.
|
| 476 |
|
| 477 |
Certain other operations are described in this International Standard as
|
| 478 |
undefined (for example, the effect of attempting to modify a `const`
|
| 479 |
-
object).
|
| 480 |
-
|
|
|
|
|
|
|
| 481 |
|
| 482 |
A conforming implementation executing a well-formed program shall
|
| 483 |
produce the same observable behavior as one of the possible executions
|
| 484 |
of the corresponding instance of the abstract machine with the same
|
| 485 |
program and the same input. However, if any such execution contains an
|
| 486 |
undefined operation, this International Standard places no requirement
|
| 487 |
on the implementation executing that program with that input (not even
|
| 488 |
with regard to operations preceding the first undefined operation).
|
| 489 |
|
| 490 |
-
If a signal handler is executed as a result of a call to the `raise`
|
| 491 |
-
function, then the execution of the handler is sequenced after the
|
| 492 |
-
invocation of the `raise` function and before its return. When a signal
|
| 493 |
-
is received for another reason, the execution of the signal handler is
|
| 494 |
-
usually unsequenced with respect to the rest of the program.
|
| 495 |
-
|
| 496 |
An instance of each object with automatic storage duration (
|
| 497 |
[[basic.stc.auto]]) is associated with each entry into its block. Such
|
| 498 |
an object exists and retains its last-stored value during the execution
|
| 499 |
of the block and while the block is suspended (by a call of a function
|
| 500 |
or receipt of a signal).
|
| 501 |
|
| 502 |
The least requirements on a conforming implementation are:
|
| 503 |
|
| 504 |
-
-
|
| 505 |
-
rules of the abstract machine.
|
| 506 |
- At program termination, all data written into files shall be identical
|
| 507 |
to one of the possible results that execution of the program according
|
| 508 |
to the abstract semantics would have produced.
|
| 509 |
- The input and output dynamics of interactive devices shall take place
|
| 510 |
in such a fashion that prompting output is actually delivered before a
|
| 511 |
program waits for input. What constitutes an interactive device is
|
| 512 |
*implementation-defined*.
|
| 513 |
|
| 514 |
These collectively are referred to as the *observable behavior* of the
|
| 515 |
-
program.
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
Operators can be regrouped according to the usual mathematical rules
|
| 519 |
-
only where the operators really are associative or commutative.[^
|
| 520 |
example, in the following fragment
|
| 521 |
|
| 522 |
``` cpp
|
| 523 |
int a, b;
|
| 524 |
-
|
| 525 |
a = a + 32760 + b + 5;
|
| 526 |
```
|
| 527 |
|
| 528 |
the expression statement behaves exactly the same as
|
| 529 |
|
|
@@ -560,184 +437,269 @@ since the values for `a` and `b` might have been, respectively, 4 and -8
|
|
| 560 |
or -17 and 12. However on a machine in which overflows do not produce an
|
| 561 |
exception and in which the results of overflows are reversible, the
|
| 562 |
above expression statement can be rewritten by the implementation in any
|
| 563 |
of the above ways because the same result will occur.
|
| 564 |
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
|
| 577 |
``` cpp
|
| 578 |
struct S {
|
| 579 |
-
S(int i): I(i) { }
|
| 580 |
int& v() { return I; }
|
|
|
|
| 581 |
private:
|
| 582 |
int I;
|
| 583 |
};
|
| 584 |
|
| 585 |
S s1(1); // full-expression is call of S::S(int)
|
| 586 |
-
S s2 = 2; // full-expression is call of S::S(int)
|
| 587 |
-
|
| 588 |
void f() {
|
|
|
|
| 589 |
if (S(3).v()) // full-expression includes lvalue-to-rvalue and
|
| 590 |
// int to bool conversions, performed before
|
| 591 |
// temporary is deleted at end of full-expression
|
| 592 |
{ }
|
|
|
|
|
|
|
|
|
|
| 593 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
```
|
| 595 |
|
| 596 |
-
|
| 597 |
-
subexpressions that are not lexically part of the full-expression. For
|
| 598 |
-
example, subexpressions involved in evaluating default arguments (
|
| 599 |
-
[[dcl.fct.default]]) are considered to be created in the expression that
|
| 600 |
-
calls the function, not the expression that defines the default
|
| 601 |
-
argument.
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 615 |
|
| 616 |
*Sequenced before* is an asymmetric, transitive, pair-wise relation
|
| 617 |
between evaluations executed by a single thread (
|
| 618 |
[[intro.multithread]]), which induces a partial order among those
|
| 619 |
evaluations. Given any two evaluations *A* and *B*, if *A* is sequenced
|
| 620 |
-
before *B*,
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
|
| 628 |
Every value computation and side effect associated with a
|
| 629 |
full-expression is sequenced before every value computation and side
|
| 630 |
-
effect associated with the next full-expression to be evaluated.[^
|
| 631 |
|
| 632 |
Except where noted, evaluations of operands of individual operators and
|
| 633 |
-
of subexpressions of individual expressions are unsequenced.
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
evaluations
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
|
| 646 |
``` cpp
|
| 647 |
-
void
|
| 648 |
-
void g(int i, int* v) {
|
| 649 |
-
i = v[i++]; // the behavior is undefined
|
| 650 |
i = 7, i++, i++; // i becomes 9
|
| 651 |
|
| 652 |
-
i = i++ + 1; // the
|
|
|
|
| 653 |
i = i + 1; // the value of i is incremented
|
| 654 |
-
|
| 655 |
-
f(i = -1, i = -1); // the behavior is undefined
|
| 656 |
}
|
| 657 |
```
|
| 658 |
|
|
|
|
|
|
|
| 659 |
When calling a function (whether or not the function is inline), every
|
| 660 |
value computation and side effect associated with any argument
|
| 661 |
expression, or with the postfix expression designating the called
|
| 662 |
function, is sequenced before execution of every expression or statement
|
| 663 |
-
in the body of the called function.
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
|
| 679 |
## Multi-threaded executions and data races <a id="intro.multithread">[[intro.multithread]]</a>
|
| 680 |
|
| 681 |
A *thread of execution* (also known as a *thread*) is a single flow of
|
| 682 |
control within a program, including the initial invocation of a specific
|
| 683 |
top-level function, and recursively including every function invocation
|
| 684 |
-
subsequently executed by the thread.
|
| 685 |
-
the initial call to the top-level function of the new thread is executed
|
| 686 |
-
by the new thread, not by the creating thread. Every thread in a program
|
| 687 |
-
can potentially access every object and function in a program.[^10]
|
| 688 |
-
Under a hosted implementation, a C++program can have more than one
|
| 689 |
-
thread running concurrently. The execution of each thread proceeds as
|
| 690 |
-
defined by the remainder of this standard. The execution of the entire
|
| 691 |
-
program consists of an execution of all of its threads. Usually the
|
| 692 |
-
execution can be viewed as an interleaving of all its threads. However,
|
| 693 |
-
some kinds of atomic operations, for example, allow executions
|
| 694 |
-
inconsistent with a simple interleaving, as described below. Under a
|
| 695 |
-
freestanding implementation, it is *implementation-defined* whether a
|
| 696 |
-
program can have more than one thread of execution.
|
| 697 |
|
| 698 |
-
|
| 699 |
-
function
|
| 700 |
-
|
| 701 |
-
contains a signal handler invocation.
|
| 702 |
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
thread
|
| 707 |
-
|
|
|
|
| 708 |
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
|
|
|
| 712 |
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
inopportune interference from other threads may prevent forward
|
| 722 |
-
progress, e.g., by repeatedly stealing a cache line for unrelated
|
| 723 |
-
purposes between load-locked and store-conditional instructions.
|
| 724 |
-
Implementations should ensure that such effects cannot indefinitely
|
| 725 |
-
delay progress under expected operating conditions, and that such
|
| 726 |
-
anomalies can therefore safely be ignored by programmers. Outside this
|
| 727 |
-
International Standard, this property is sometimes termed lock-free.
|
| 728 |
|
| 729 |
The value of an object visible to a thread *T* at a particular point is
|
| 730 |
the initial value of the object, a value assigned to the object by *T*,
|
| 731 |
or a value assigned to the object by another thread, according to the
|
| 732 |
-
rules below.
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
|
|
|
|
|
|
|
|
|
| 736 |
|
| 737 |
Two expression evaluations *conflict* if one of them modifies a memory
|
| 738 |
-
location ([[intro.memory]]) and the other one
|
| 739 |
same memory location.
|
| 740 |
|
| 741 |
The library defines a number of atomic operations (Clause [[atomics]])
|
| 742 |
and operations on mutexes (Clause [[thread]]) that are specially
|
| 743 |
identified as synchronization operations. These operations play a
|
|
@@ -747,30 +709,30 @@ consume operation, an acquire operation, a release operation, or both an
|
|
| 747 |
acquire and release operation. A synchronization operation without an
|
| 748 |
associated memory location is a fence and can be either an acquire
|
| 749 |
fence, a release fence, or both an acquire and release fence. In
|
| 750 |
addition, there are relaxed atomic operations, which are not
|
| 751 |
synchronization operations, and atomic read-modify-write operations,
|
| 752 |
-
which have special characteristics.
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
|
|
|
|
|
|
| 756 |
release operation on *A* forces prior side effects on other memory
|
| 757 |
locations to become visible to other threads that later perform a
|
| 758 |
consume or an acquire operation on *A*. “Relaxed” atomic operations are
|
| 759 |
not synchronization operations even though, like synchronization
|
| 760 |
-
operations, they cannot contribute to data races.
|
| 761 |
|
| 762 |
All modifications to a particular atomic object *M* occur in some
|
| 763 |
-
particular total order, called the *modification order* of *M*.
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
general this will be impossible since different threads may observe
|
| 771 |
-
modifications to different objects in inconsistent orders.
|
| 772 |
|
| 773 |
A *release sequence* headed by a release operation *A* on an atomic
|
| 774 |
object *M* is a maximal contiguous sub-sequence of side effects in the
|
| 775 |
modification order of *M*, where the first operation is `A`, and every
|
| 776 |
subsequent operation
|
|
@@ -779,18 +741,21 @@ subsequent operation
|
|
| 779 |
- is an atomic read-modify-write operation.
|
| 780 |
|
| 781 |
Certain library calls *synchronize with* other library calls performed
|
| 782 |
by another thread. For example, an atomic store-release synchronizes
|
| 783 |
with a load-acquire that takes its value from the store (
|
| 784 |
-
[[atomics.order]]).
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
|
|
|
|
|
|
|
|
|
| 792 |
|
| 793 |
An evaluation *A* *carries a dependency* to an evaluation *B* if
|
| 794 |
|
| 795 |
- the value of *A* is used as an operand of *B*, unless:
|
| 796 |
- *B* is an invocation of any specialization of
|
|
@@ -807,25 +772,25 @@ An evaluation *A* *carries a dependency* to an evaluation *B* if
|
|
| 807 |
- *A* writes a scalar object or bit-field *M*, *B* reads the value
|
| 808 |
written by *A* from *M*, and *A* is sequenced before *B*, or
|
| 809 |
- for some evaluation *X*, *A* carries a dependency to *X*, and *X*
|
| 810 |
carries a dependency to *B*.
|
| 811 |
|
| 812 |
-
“Carries a dependency to” is a subset of “is sequenced
|
| 813 |
-
similarly strictly intra-thread.
|
| 814 |
|
| 815 |
An evaluation *A* is *dependency-ordered before* an evaluation *B* if
|
| 816 |
|
| 817 |
- *A* performs a release operation on an atomic object *M*, and, in
|
| 818 |
another thread, *B* performs a consume operation on *M* and reads a
|
| 819 |
value written by any side effect in the release sequence headed by
|
| 820 |
*A*, or
|
| 821 |
- for some evaluation *X*, *A* is dependency-ordered before *X* and *X*
|
| 822 |
carries a dependency to *B*.
|
| 823 |
|
| 824 |
-
The relation “is dependency-ordered before” is analogous to
|
| 825 |
“synchronizes with”, but uses release/consume in place of
|
| 826 |
-
release/acquire.
|
| 827 |
|
| 828 |
An evaluation *A* *inter-thread happens before* an evaluation *B* if
|
| 829 |
|
| 830 |
- *A* synchronizes with *B*, or
|
| 831 |
- *A* is dependency-ordered before *B*, or
|
|
@@ -834,12 +799,12 @@ An evaluation *A* *inter-thread happens before* an evaluation *B* if
|
|
| 834 |
- *A* is sequenced before *X* and *X* inter-thread happens before *B*,
|
| 835 |
or
|
| 836 |
- *A* inter-thread happens before *X* and *X* inter-thread happens
|
| 837 |
before *B*.
|
| 838 |
|
| 839 |
-
The “inter-thread happens before” relation describes
|
| 840 |
-
concatenations of “sequenced before”, “synchronizes with” and
|
| 841 |
“dependency-ordered before” relationships, with two exceptions. The
|
| 842 |
first exception is that a concatenation is not permitted to end with
|
| 843 |
“dependency-ordered before” followed by “sequenced before”. The reason
|
| 844 |
for this limitation is that a consume operation participating in a
|
| 845 |
“dependency-ordered before” relationship provides ordering only with
|
|
@@ -849,164 +814,340 @@ such a concatenation is that any subsequent release operation will
|
|
| 849 |
provide the required ordering for a prior consume operation. The second
|
| 850 |
exception is that a concatenation is not permitted to consist entirely
|
| 851 |
of “sequenced before”. The reasons for this limitation are (1) to permit
|
| 852 |
“inter-thread happens before” to be transitively closed and (2) the
|
| 853 |
“happens before” relation, defined below, provides for relationships
|
| 854 |
-
consisting entirely of “sequenced before”.
|
| 855 |
|
| 856 |
-
An evaluation *A* *happens before* an evaluation *B*
|
|
|
|
| 857 |
|
| 858 |
- *A* is sequenced before *B*, or
|
| 859 |
- *A* inter-thread happens before *B*.
|
| 860 |
|
| 861 |
The implementation shall ensure that no program execution demonstrates a
|
| 862 |
-
cycle in the “happens before” relation.
|
| 863 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
A *visible side effect* *A* on a scalar object or bit-field *M* with
|
| 866 |
respect to a value computation *B* of *M* satisfies the conditions:
|
| 867 |
|
| 868 |
- *A* happens before *B* and
|
| 869 |
- there is no other side effect *X* to *M* such that *A* happens before
|
| 870 |
*X* and *X* happens before *B*.
|
| 871 |
|
| 872 |
The value of a non-atomic scalar object or bit-field *M*, as determined
|
| 873 |
by evaluation *B*, shall be the value stored by the visible side effect
|
| 874 |
-
*A*.
|
| 875 |
-
|
| 876 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 877 |
visibly reordered. This is not actually detectable without data races,
|
| 878 |
but it is necessary to ensure that data races, as defined below, and
|
| 879 |
with suitable restrictions on the use of atomics, correspond to data
|
| 880 |
-
races in a simple interleaved (sequentially consistent)
|
|
|
|
| 881 |
|
| 882 |
The value of an atomic object *M*, as determined by evaluation *B*,
|
| 883 |
shall be the value stored by some side effect *A* that modifies *M*,
|
| 884 |
-
where *B* does not happen before *A*.
|
| 885 |
-
|
| 886 |
-
|
|
|
|
|
|
|
| 887 |
|
| 888 |
If an operation *A* that modifies an atomic object *M* happens before an
|
| 889 |
operation *B* that modifies *M*, then *A* shall be earlier than *B* in
|
| 890 |
-
the modification order of *M*.
|
| 891 |
-
|
|
|
|
|
|
|
| 892 |
|
| 893 |
If a value computation *A* of an atomic object *M* happens before a
|
| 894 |
value computation *B* of *M*, and *A* takes its value from a side effect
|
| 895 |
*X* on *M*, then the value computed by *B* shall either be the value
|
| 896 |
stored by *X* or the value stored by a side effect *Y* on *M*, where *Y*
|
| 897 |
-
follows *X* in the modification order of *M*.
|
| 898 |
-
|
|
|
|
|
|
|
| 899 |
|
| 900 |
If a value computation *A* of an atomic object *M* happens before an
|
| 901 |
operation *B* that modifies *M*, then *A* shall take its value from a
|
| 902 |
side effect *X* on *M*, where *X* precedes *B* in the modification order
|
| 903 |
-
of *M*.
|
|
|
|
|
|
|
|
|
|
| 904 |
|
| 905 |
If a side effect *X* on an atomic object *M* happens before a value
|
| 906 |
computation *B* of *M*, then the evaluation *B* shall take its value
|
| 907 |
from *X* or from a side effect *Y* that follows *X* in the modification
|
| 908 |
-
order of *M*.
|
| 909 |
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
operations are relaxed loads. This effectively makes the cache coherence
|
| 913 |
-
guarantee provided by most hardware available to C++atomic operations.
|
| 914 |
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 919 |
suitably chosen modification orders and the “happens before” relation
|
| 920 |
derived as described above, satisfy the resulting constraints as imposed
|
| 921 |
-
here.
|
| 922 |
|
| 923 |
Two actions are *potentially concurrent* if
|
| 924 |
|
| 925 |
- they are performed by different threads, or
|
| 926 |
-
- they are unsequenced,
|
| 927 |
-
handler.
|
| 928 |
|
| 929 |
The execution of a program contains a *data race* if it contains two
|
| 930 |
potentially concurrent conflicting actions, at least one of which is not
|
| 931 |
atomic, and neither happens before the other, except for the special
|
| 932 |
case for signal handlers described below. Any such data race results in
|
| 933 |
-
undefined behavior.
|
| 934 |
-
mutexes and `memory_order_seq_cst` operations to prevent all data races
|
| 935 |
-
and use no other synchronization operations behave as if the operations
|
| 936 |
-
executed by their constituent threads were simply interleaved, with each
|
| 937 |
-
value computation of an object being taken from the last side effect on
|
| 938 |
-
that object in that interleaving. This is normally referred to as
|
| 939 |
-
“sequential consistency”. However, this applies only to data-race-free
|
| 940 |
-
programs, and data-race-free programs cannot observe most program
|
| 941 |
-
transformations that do not change single-threaded program semantics. In
|
| 942 |
-
fact, most single-threaded program transformations continue to be
|
| 943 |
-
allowed, since any program that behaves differently as a result must
|
| 944 |
-
perform an undefined operation.
|
| 945 |
|
| 946 |
-
|
| 947 |
-
|
| 948 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 949 |
evaluations performed by the thread invoking a signal handler can be
|
| 950 |
divided into two groups *A* and *B*, such that no evaluations in *B*
|
| 951 |
happen before evaluations in *A*, and the evaluations of such
|
| 952 |
-
`volatile sig_atomic_t` objects take values as though all
|
| 953 |
-
*A* happened before the execution of the signal handler
|
| 954 |
-
execution of the signal handler happened before all evaluations
|
|
|
|
| 955 |
|
| 956 |
-
Compiler transformations that introduce assignments to a
|
| 957 |
-
shared memory location that would not be modified by the
|
| 958 |
-
machine are generally precluded by this
|
| 959 |
-
assignment might overwrite another assignment by a
|
| 960 |
-
cases in which an abstract machine execution would
|
| 961 |
-
a data race. This includes implementations of data
|
| 962 |
-
that overwrite adjacent members in separate memory
|
| 963 |
-
of atomic loads in cases in which the atomics in
|
| 964 |
-
also generally precluded, since this may violate
|
|
|
|
| 965 |
|
| 966 |
-
Transformations that introduce a speculative read of a
|
| 967 |
-
shared memory location may not preserve the semantics of the
|
| 968 |
-
as defined in this
|
| 969 |
-
race. However, they are typically valid in
|
| 970 |
-
compiler that targets a specific machine
|
| 971 |
-
data races. They would be invalid for a
|
| 972 |
-
tolerant of races or provides hardware
|
|
|
|
|
|
|
|
|
|
| 973 |
|
| 974 |
The implementation may assume that any thread will eventually do one of
|
| 975 |
the following:
|
| 976 |
|
| 977 |
- terminate,
|
| 978 |
- make a call to a library I/O function,
|
| 979 |
-
-
|
| 980 |
- perform a synchronization operation or an atomic operation.
|
| 981 |
|
| 982 |
-
This is intended to allow compiler transformations such as
|
| 983 |
-
empty loops, even when termination cannot be
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 984 |
|
| 985 |
An implementation should ensure that the last value (in modification
|
| 986 |
order) assigned by an atomic or synchronization operation will become
|
| 987 |
visible to all other threads in a finite period of time.
|
| 988 |
|
| 989 |
## Acknowledgments <a id="intro.ack">[[intro.ack]]</a>
|
| 990 |
|
| 991 |
-
The C++programming language as described in this
|
| 992 |
-
|
| 993 |
-
|
| 994 |
-
|
| 995 |
-
|
| 996 |
-
|
| 997 |
-
|
| 998 |
-
AT&T).
|
| 999 |
|
| 1000 |
-
Portions of the library Clauses of this
|
| 1001 |
-
|
| 1002 |
-
|
| 1003 |
-
Plauger).
|
| 1004 |
|
| 1005 |
POSIX® is a registered trademark of the Institute of Electrical and
|
| 1006 |
Electronic Engineers, Inc.
|
| 1007 |
|
|
|
|
|
|
|
| 1008 |
All rights in these originals are reserved.
|
| 1009 |
|
| 1010 |
<!-- Link reference definitions -->
|
| 1011 |
[atomics]: atomics.md#atomics
|
| 1012 |
[atomics.flag]: atomics.md#atomics.flag
|
|
@@ -1018,95 +1159,118 @@ All rights in these originals are reserved.
|
|
| 1018 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 1019 |
[basic.life]: basic.md#basic.life
|
| 1020 |
[basic.link]: basic.md#basic.link
|
| 1021 |
[basic.lval]: basic.md#basic.lval
|
| 1022 |
[basic.namespace]: dcl.md#basic.namespace
|
|
|
|
| 1023 |
[basic.stc]: basic.md#basic.stc
|
| 1024 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 1025 |
[basic.types]: basic.md#basic.types
|
| 1026 |
[class.access]: class.md#class.access
|
|
|
|
| 1027 |
[class.bit]: class.md#class.bit
|
|
|
|
| 1028 |
[class.conv.fct]: special.md#class.conv.fct
|
| 1029 |
[class.derived]: class.md#class.derived
|
| 1030 |
[class.mem]: class.md#class.mem
|
| 1031 |
[class.temporary]: special.md#class.temporary
|
|
|
|
| 1032 |
[class.virtual]: class.md#class.virtual
|
| 1033 |
[compliance]: library.md#compliance
|
|
|
|
| 1034 |
[cpp]: cpp.md#cpp
|
| 1035 |
[cpp.include]: cpp.md#cpp.include
|
|
|
|
|
|
|
|
|
|
| 1036 |
[dcl.fct]: dcl.md#dcl.fct
|
| 1037 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
|
|
|
| 1038 |
[dcl.ptr]: dcl.md#dcl.ptr
|
| 1039 |
[dcl.ref]: dcl.md#dcl.ref
|
| 1040 |
[definitions]: library.md#definitions
|
|
|
|
|
|
|
| 1041 |
[depr]: future.md#depr
|
| 1042 |
[diff]: compatibility.md#diff
|
| 1043 |
[diff.library]: compatibility.md#diff.library
|
| 1044 |
[expr]: expr.md#expr
|
|
|
|
| 1045 |
[expr.comma]: expr.md#expr.comma
|
| 1046 |
[expr.cond]: expr.md#expr.cond
|
|
|
|
| 1047 |
[expr.log.and]: expr.md#expr.log.and
|
| 1048 |
[expr.log.or]: expr.md#expr.log.or
|
| 1049 |
[expr.new]: expr.md#expr.new
|
|
|
|
|
|
|
| 1050 |
[gram]: grammar.md#gram
|
| 1051 |
[implimits]: limits.md#implimits
|
| 1052 |
[intro]: #intro
|
| 1053 |
[intro.ack]: #intro.ack
|
| 1054 |
[intro.compliance]: #intro.compliance
|
| 1055 |
[intro.defs]: #intro.defs
|
| 1056 |
[intro.execution]: #intro.execution
|
| 1057 |
[intro.memory]: #intro.memory
|
| 1058 |
[intro.multithread]: #intro.multithread
|
| 1059 |
[intro.object]: #intro.object
|
|
|
|
|
|
|
| 1060 |
[intro.refs]: #intro.refs
|
| 1061 |
[intro.scope]: #intro.scope
|
| 1062 |
[intro.structure]: #intro.structure
|
| 1063 |
[language.support]: language.md#language.support
|
| 1064 |
[lex]: lex.md#lex
|
| 1065 |
[lex.charset]: lex.md#lex.charset
|
| 1066 |
[lex.phases]: lex.md#lex.phases
|
| 1067 |
[library]: library.md#library
|
| 1068 |
[syntax]: #syntax
|
|
|
|
|
|
|
| 1069 |
[thread]: thread.md#thread
|
|
|
|
| 1070 |
|
| 1071 |
[^1]: With the qualifications noted in Clauses [[language.support]]
|
| 1072 |
through [[thread]] and in [[diff.library]], the C standard library
|
| 1073 |
is a subset of the C++standard library.
|
| 1074 |
|
| 1075 |
[^2]: “Correct execution” can include undefined behavior, depending on
|
| 1076 |
-
the data being processed; see [[intro.defs]] and
|
| 1077 |
[[intro.execution]].
|
| 1078 |
|
| 1079 |
[^3]: This documentation also defines implementation-defined behavior;
|
| 1080 |
see [[intro.execution]].
|
| 1081 |
|
| 1082 |
-
[^4]:
|
|
|
|
|
|
|
|
|
|
| 1083 |
objects at the same machine address or not store an object at all if
|
| 1084 |
the program cannot observe the difference ([[intro.execution]]).
|
| 1085 |
|
| 1086 |
-
[^
|
| 1087 |
implementation is free to disregard any requirement of this
|
| 1088 |
International Standard as long as the result is *as if* the
|
| 1089 |
requirement had been obeyed, as far as can be determined from the
|
| 1090 |
observable behavior of the program. For instance, an actual
|
| 1091 |
implementation need not evaluate part of an expression if it can
|
| 1092 |
deduce that its value is not used and that no side effects affecting
|
| 1093 |
the observable behavior of the program are produced.
|
| 1094 |
|
| 1095 |
-
[^
|
| 1096 |
constructs and locale-specific behavior. See [[intro.compliance]].
|
| 1097 |
|
| 1098 |
-
[^
|
| 1099 |
commutative.
|
| 1100 |
|
| 1101 |
-
[^
|
| 1102 |
evaluated, a sequence of zero or more invocations of destructor
|
| 1103 |
functions for temporary objects takes place, usually in reverse
|
| 1104 |
order of the construction of each temporary object.
|
| 1105 |
|
| 1106 |
-
[^
|
| 1107 |
other.
|
| 1108 |
|
| 1109 |
-
[^
|
| 1110 |
[[basic.stc]]) is associated with one specific thread, and can be
|
| 1111 |
accessed by a different thread only indirectly through a pointer or
|
| 1112 |
reference ([[basic.compound]]).
|
|
|
|
| 1 |
+
# General principles <a id="intro">[[intro]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
## Implementation compliance <a id="intro.compliance">[[intro.compliance]]</a>
|
| 4 |
|
| 5 |
The set of *diagnosable rules* consists of all syntactic and semantic
|
| 6 |
rules in this International Standard except for those rules containing
|
| 7 |
an explicit notation that “no diagnostic is required” or which are
|
| 8 |
+
described as resulting in “undefined behavior”.
|
| 9 |
|
| 10 |
Although this International Standard states only requirements on C++
|
| 11 |
implementations, those requirements are often easier to understand if
|
| 12 |
they are phrased as requirements on programs, parts of programs, or
|
| 13 |
execution of programs. Such requirements have the following meaning:
|
| 14 |
|
| 15 |
- If a program contains no violations of the rules in this International
|
| 16 |
Standard, a conforming implementation shall, within its resource
|
| 17 |
limits, accept and correctly execute[^2] that program.
|
| 18 |
- If a program contains a violation of any diagnosable rule or an
|
| 19 |
+
occurrence of a construct described in this International Standard as
|
| 20 |
“conditionally-supported” when the implementation does not support
|
| 21 |
that construct, a conforming implementation shall issue at least one
|
| 22 |
diagnostic message.
|
| 23 |
- If a program contains a violation of a rule for which no diagnostic is
|
| 24 |
required, this International Standard places no requirement on
|
| 25 |
implementations with respect to that program.
|
| 26 |
|
| 27 |
+
[*Note 1*: During template argument deduction and substitution, certain
|
| 28 |
+
constructs that in other contexts require a diagnostic are treated
|
| 29 |
+
differently; see [[temp.deduct]]. — *end note*]
|
| 30 |
+
|
| 31 |
For classes and class templates, the library Clauses specify partial
|
| 32 |
definitions. Private members (Clause [[class.access]]) are not
|
| 33 |
specified, but each implementation shall supply them to complete the
|
| 34 |
definitions according to the description in the library Clauses.
|
| 35 |
|
|
|
|
| 64 |
|
| 65 |
Each implementation shall include documentation that identifies all
|
| 66 |
conditionally-supported constructs that it does not support and defines
|
| 67 |
all locale-specific characteristics.[^3]
|
| 68 |
|
| 69 |
+
## Structure of this document <a id="intro.structure">[[intro.structure]]</a>
|
| 70 |
|
| 71 |
Clauses [[lex]] through [[cpp]] describe the C++programming language.
|
| 72 |
That description includes detailed syntactic specifications in a form
|
| 73 |
described in [[syntax]]. For convenience, Annex [[gram]] repeats all
|
| 74 |
such syntactic specifications.
|
| 75 |
|
| 76 |
Clauses [[language.support]] through [[thread]] and Annex [[depr]]
|
| 77 |
+
(the *library clauses*) describe the C++standard library. That
|
| 78 |
+
description includes detailed descriptions of the entities and macros
|
| 79 |
+
that constitute the library, in a form described in Clause [[library]].
|
|
|
|
| 80 |
|
| 81 |
Annex [[implimits]] recommends lower bounds on the capacity of
|
| 82 |
conforming implementations.
|
| 83 |
|
| 84 |
Annex [[diff]] summarizes the evolution of C++since its first published
|
| 85 |
description, and explains in detail the differences between C++and C.
|
| 86 |
Certain features of C++exist solely for compatibility purposes; Annex
|
| 87 |
[[depr]] describes those features.
|
| 88 |
|
| 89 |
+
Throughout this document, each example is introduced by “” and
|
| 90 |
+
terminated by “”. Each note is introduced by “” and terminated by “”.
|
| 91 |
+
Examples and notes may be nested.
|
| 92 |
|
| 93 |
## Syntax notation <a id="syntax">[[syntax]]</a>
|
| 94 |
|
| 95 |
+
In the syntax notation used in this document, syntactic categories are
|
| 96 |
+
indicated by *italic* type, and literal words and characters in
|
| 97 |
+
`constant` `width` type. Alternatives are listed on separate lines
|
| 98 |
+
except in a few cases where a long set of alternatives is marked by the
|
| 99 |
+
phrase “one of”. If the text of an alternative is too long to fit on a
|
| 100 |
+
line, the text is continued on subsequent lines indented from the first
|
| 101 |
+
one. An optional terminal or non-terminal symbol is indicated by the
|
| 102 |
+
subscript “”, so
|
| 103 |
|
| 104 |
``` bnf
|
| 105 |
'{' expressionₒₚₜ '}'
|
| 106 |
```
|
| 107 |
|
|
|
|
| 115 |
- *X-id* is an identifier with no context-dependent meaning (e.g.,
|
| 116 |
*qualified-id*).
|
| 117 |
- *X-seq* is one or more *X*’s without intervening delimiters (e.g.,
|
| 118 |
*declaration-seq* is a sequence of declarations).
|
| 119 |
- *X-list* is one or more *X*’s separated by intervening commas (e.g.,
|
| 120 |
+
*identifier-list* is a sequence of identifiers separated by commas).
|
| 121 |
|
| 122 |
## The C++memory model <a id="intro.memory">[[intro.memory]]</a>
|
| 123 |
|
| 124 |
The fundamental storage unit in the C++memory model is the *byte*. A
|
| 125 |
byte is at least large enough to contain any member of the basic
|
| 126 |
execution character set ([[lex.charset]]) and the eight-bit code units
|
| 127 |
of the Unicode UTF-8 encoding form and is composed of a contiguous
|
| 128 |
+
sequence of bits,[^4] the number of which is *implementation-defined*.
|
| 129 |
+
The least significant bit is called the *low-order bit*; the most
|
| 130 |
significant bit is called the *high-order bit*. The memory available to
|
| 131 |
a C++program consists of one or more sequences of contiguous bytes.
|
| 132 |
Every byte has a unique address.
|
| 133 |
|
| 134 |
+
[*Note 1*: The representation of types is described in
|
| 135 |
+
[[basic.types]]. — *end note*]
|
| 136 |
|
| 137 |
A *memory location* is either an object of scalar type or a maximal
|
| 138 |
+
sequence of adjacent bit-fields all having nonzero width.
|
| 139 |
+
|
| 140 |
+
[*Note 2*: Various features of the language, such as references and
|
| 141 |
+
virtual functions, might involve additional memory locations that are
|
| 142 |
+
not accessible to programs but are managed by the
|
| 143 |
+
implementation. — *end note*]
|
| 144 |
+
|
| 145 |
+
Two or more threads of execution ([[intro.multithread]]) can access
|
| 146 |
+
separate memory locations without interfering with each other.
|
| 147 |
+
|
| 148 |
+
[*Note 3*: Thus a bit-field and an adjacent non-bit-field are in
|
| 149 |
+
separate memory locations, and therefore can be concurrently updated by
|
| 150 |
+
two threads of execution without interference. The same applies to two
|
| 151 |
+
bit-fields, if one is declared inside a nested struct declaration and
|
| 152 |
+
the other is not, or if the two are separated by a zero-length bit-field
|
| 153 |
+
declaration, or if they are separated by a non-bit-field declaration. It
|
| 154 |
+
is not safe to concurrently update two bit-fields in the same struct if
|
| 155 |
+
all fields between them are also bit-fields of nonzero
|
| 156 |
+
width. — *end note*]
|
| 157 |
+
|
| 158 |
+
[*Example 1*:
|
| 159 |
|
| 160 |
A structure declared as
|
| 161 |
|
| 162 |
``` cpp
|
| 163 |
struct {
|
|
|
|
| 175 |
concurrently without interfering with each other. The bit-fields `b` and
|
| 176 |
`c` together constitute the fourth memory location. The bit-fields `b`
|
| 177 |
and `c` cannot be concurrently modified, but `b` and `a`, for example,
|
| 178 |
can be.
|
| 179 |
|
| 180 |
+
— *end example*]
|
| 181 |
+
|
| 182 |
## The C++object model <a id="intro.object">[[intro.object]]</a>
|
| 183 |
|
| 184 |
The constructs in a C++program create, destroy, refer to, access, and
|
| 185 |
+
manipulate objects. An *object* is created by a definition (
|
| 186 |
+
[[basic.def]]), by a *new-expression* ([[expr.new]]), when implicitly
|
| 187 |
+
changing the active member of a union ([[class.union]]), or when a
|
| 188 |
+
temporary object is created ([[conv.rval]], [[class.temporary]]). An
|
| 189 |
+
object occupies a region of storage in its period of construction (
|
| 190 |
+
[[class.cdtor]]), throughout its lifetime ([[basic.life]]), and in its
|
| 191 |
+
period of destruction ([[class.cdtor]]).
|
| 192 |
+
|
| 193 |
+
[*Note 1*: A function is not an object, regardless of whether or not it
|
| 194 |
+
occupies storage in the way that objects do. — *end note*]
|
| 195 |
+
|
| 196 |
+
The properties of an object are determined when the object is created.
|
| 197 |
+
An object can have a name (Clause [[basic]]). An object has a storage
|
| 198 |
+
duration ([[basic.stc]]) which influences its lifetime (
|
| 199 |
+
[[basic.life]]). An object has a type ([[basic.types]]). Some objects
|
| 200 |
+
are polymorphic ([[class.virtual]]); the implementation generates
|
| 201 |
information associated with each such object that makes it possible to
|
| 202 |
determine that object’s type during program execution. For other
|
| 203 |
objects, the interpretation of the values found therein is determined by
|
| 204 |
the type of the *expression*s (Clause [[expr]]) used to access them.
|
| 205 |
|
| 206 |
Objects can contain other objects, called *subobjects*. A subobject can
|
| 207 |
be a *member subobject* ([[class.mem]]), a *base class subobject*
|
| 208 |
(Clause [[class.derived]]), or an array element. An object that is not
|
| 209 |
+
a subobject of any other object is called a *complete object*. If an
|
| 210 |
+
object is created in storage associated with a member subobject or array
|
| 211 |
+
element *e* (which may or may not be within its lifetime), the created
|
| 212 |
+
object is a subobject of *e*’s containing object if:
|
| 213 |
+
|
| 214 |
+
- the lifetime of *e*’s containing object has begun and not ended, and
|
| 215 |
+
- the storage for the new object exactly overlays the storage location
|
| 216 |
+
associated with *e*, and
|
| 217 |
+
- the new object is of the same type as *e* (ignoring cv-qualification).
|
| 218 |
+
|
| 219 |
+
[*Note 2*: If the subobject contains a reference member or a `const`
|
| 220 |
+
subobject, the name of the original subobject cannot be used to access
|
| 221 |
+
the new object ([[basic.life]]). — *end note*]
|
| 222 |
+
|
| 223 |
+
[*Example 1*:
|
| 224 |
+
|
| 225 |
+
``` cpp
|
| 226 |
+
struct X { const int n; };
|
| 227 |
+
union U { X x; float f; };
|
| 228 |
+
void tong() {
|
| 229 |
+
U u = {{ 1 }};
|
| 230 |
+
u.f = 5.f; // OK, creates new subobject of u ([class.union])
|
| 231 |
+
X *p = new (&u.x) X {2}; // OK, creates new subobject of u
|
| 232 |
+
assert(p->n == 2); // OK
|
| 233 |
+
assert(*std::launder(&u.x.n) == 2); // OK
|
| 234 |
+
assert(u.x.n == 2); // undefined behavior, u.x does not name new subobject
|
| 235 |
+
}
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
— *end example*]
|
| 239 |
+
|
| 240 |
+
If a complete object is created ([[expr.new]]) in storage associated
|
| 241 |
+
with another object *e* of type “array of N `unsigned char`” or of type
|
| 242 |
+
“array of N `std::byte`” ([[cstddef.syn]]), that array *provides
|
| 243 |
+
storage* for the created object if:
|
| 244 |
+
|
| 245 |
+
- the lifetime of *e* has begun and not ended, and
|
| 246 |
+
- the storage for the new object fits entirely within *e*, and
|
| 247 |
+
- there is no smaller array object that satisfies these constraints.
|
| 248 |
+
|
| 249 |
+
[*Note 3*: If that portion of the array previously provided storage for
|
| 250 |
+
another object, the lifetime of that object ends because its storage was
|
| 251 |
+
reused ([[basic.life]]). — *end note*]
|
| 252 |
+
|
| 253 |
+
[*Example 2*:
|
| 254 |
+
|
| 255 |
+
``` cpp
|
| 256 |
+
template<typename ...T>
|
| 257 |
+
struct AlignedUnion {
|
| 258 |
+
alignas(T...) unsigned char data[max(sizeof(T)...)];
|
| 259 |
+
};
|
| 260 |
+
int f() {
|
| 261 |
+
AlignedUnion<int, char> au;
|
| 262 |
+
int *p = new (au.data) int; // OK, au.data provides storage
|
| 263 |
+
char *c = new (au.data) char(); // OK, ends lifetime of *p
|
| 264 |
+
char *d = new (au.data + 1) char();
|
| 265 |
+
return *c + *d; // OK
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
struct A { unsigned char a[32]; };
|
| 269 |
+
struct B { unsigned char b[16]; };
|
| 270 |
+
A a;
|
| 271 |
+
B *b = new (a.a + 8) B; // a.a provides storage for *b
|
| 272 |
+
int *p = new (b->b + 4) int; // b->b provides storage for *p
|
| 273 |
+
// a.a does not provide storage for *p (directly),
|
| 274 |
+
// but *p is nested within a (see below)
|
| 275 |
+
```
|
| 276 |
+
|
| 277 |
+
— *end example*]
|
| 278 |
+
|
| 279 |
+
An object *a* is *nested within* another object *b* if:
|
| 280 |
+
|
| 281 |
+
- *a* is a subobject of *b*, or
|
| 282 |
+
- *b* provides storage for *a*, or
|
| 283 |
+
- there exists an object *c* where *a* is nested within *c*, and *c* is
|
| 284 |
+
nested within *b*.
|
| 285 |
|
| 286 |
For every object `x`, there is some object called the *complete object
|
| 287 |
of* `x`, determined as follows:
|
| 288 |
|
| 289 |
+
- If `x` is a complete object, then the complete object of `x` is
|
| 290 |
+
itself.
|
| 291 |
- Otherwise, the complete object of `x` is the complete object of the
|
| 292 |
(unique) object that contains `x`.
|
| 293 |
|
| 294 |
If a complete object, a data member ([[class.mem]]), or an array
|
| 295 |
element is of class type, its type is considered the *most derived
|
| 296 |
class*, to distinguish it from the class type of any base class
|
| 297 |
subobject; an object of a most derived class type or of a non-class type
|
| 298 |
is called a *most derived object*.
|
| 299 |
|
| 300 |
Unless it is a bit-field ([[class.bit]]), a most derived object shall
|
| 301 |
+
have a nonzero size and shall occupy one or more bytes of storage. Base
|
| 302 |
class subobjects may have zero size. An object of trivially copyable or
|
| 303 |
standard-layout type ([[basic.types]]) shall occupy contiguous bytes of
|
| 304 |
storage.
|
| 305 |
|
| 306 |
Unless an object is a bit-field or a base class subobject of zero size,
|
| 307 |
the address of that object is the address of the first byte it occupies.
|
| 308 |
+
Two objects *a* and *b* with overlapping lifetimes that are not
|
| 309 |
+
bit-fields may have the same address if one is nested within the other,
|
| 310 |
+
or if at least one is a base class subobject of zero size and they are
|
| 311 |
+
of different types; otherwise, they have distinct addresses.[^5]
|
| 312 |
+
|
| 313 |
+
[*Example 3*:
|
| 314 |
|
| 315 |
``` cpp
|
| 316 |
static const char test1 = 'x';
|
| 317 |
static const char test2 = 'x';
|
| 318 |
const bool b = &test1 != &test2; // always true
|
| 319 |
```
|
| 320 |
|
| 321 |
+
— *end example*]
|
| 322 |
+
|
| 323 |
+
[*Note 4*: C++provides a variety of fundamental types and several ways
|
| 324 |
+
of composing new types from existing types (
|
| 325 |
+
[[basic.types]]). — *end note*]
|
| 326 |
|
| 327 |
## Program execution <a id="intro.execution">[[intro.execution]]</a>
|
| 328 |
|
| 329 |
The semantic descriptions in this International Standard define a
|
| 330 |
parameterized nondeterministic abstract machine. This International
|
| 331 |
Standard places no requirement on the structure of conforming
|
| 332 |
implementations. In particular, they need not copy or emulate the
|
| 333 |
structure of the abstract machine. Rather, conforming implementations
|
| 334 |
are required to emulate (only) the observable behavior of the abstract
|
| 335 |
+
machine as explained below.[^6]
|
| 336 |
|
| 337 |
Certain aspects and operations of the abstract machine are described in
|
| 338 |
this International Standard as implementation-defined (for example,
|
| 339 |
`sizeof(int)`). These constitute the parameters of the abstract machine.
|
| 340 |
Each implementation shall include documentation describing its
|
| 341 |
+
characteristics and behavior in these respects.[^7] Such documentation
|
| 342 |
shall define the instance of the abstract machine that corresponds to
|
| 343 |
that implementation (referred to as the “corresponding instance” below).
|
| 344 |
|
| 345 |
Certain other aspects and operations of the abstract machine are
|
| 346 |
described in this International Standard as unspecified (for example,
|
|
|
|
| 351 |
of the abstract machine can thus have more than one possible execution
|
| 352 |
for a given program and a given input.
|
| 353 |
|
| 354 |
Certain other operations are described in this International Standard as
|
| 355 |
undefined (for example, the effect of attempting to modify a `const`
|
| 356 |
+
object).
|
| 357 |
+
|
| 358 |
+
[*Note 1*: This International Standard imposes no requirements on the
|
| 359 |
+
behavior of programs that contain undefined behavior. — *end note*]
|
| 360 |
|
| 361 |
A conforming implementation executing a well-formed program shall
|
| 362 |
produce the same observable behavior as one of the possible executions
|
| 363 |
of the corresponding instance of the abstract machine with the same
|
| 364 |
program and the same input. However, if any such execution contains an
|
| 365 |
undefined operation, this International Standard places no requirement
|
| 366 |
on the implementation executing that program with that input (not even
|
| 367 |
with regard to operations preceding the first undefined operation).
|
| 368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
An instance of each object with automatic storage duration (
|
| 370 |
[[basic.stc.auto]]) is associated with each entry into its block. Such
|
| 371 |
an object exists and retains its last-stored value during the execution
|
| 372 |
of the block and while the block is suspended (by a call of a function
|
| 373 |
or receipt of a signal).
|
| 374 |
|
| 375 |
The least requirements on a conforming implementation are:
|
| 376 |
|
| 377 |
+
- Accesses through volatile glvalues are evaluated strictly according to
|
| 378 |
+
the rules of the abstract machine.
|
| 379 |
- At program termination, all data written into files shall be identical
|
| 380 |
to one of the possible results that execution of the program according
|
| 381 |
to the abstract semantics would have produced.
|
| 382 |
- The input and output dynamics of interactive devices shall take place
|
| 383 |
in such a fashion that prompting output is actually delivered before a
|
| 384 |
program waits for input. What constitutes an interactive device is
|
| 385 |
*implementation-defined*.
|
| 386 |
|
| 387 |
These collectively are referred to as the *observable behavior* of the
|
| 388 |
+
program.
|
| 389 |
+
|
| 390 |
+
[*Note 2*: More stringent correspondences between abstract and actual
|
| 391 |
+
semantics may be defined by each implementation. — *end note*]
|
| 392 |
+
|
| 393 |
+
[*Note 3*:
|
| 394 |
|
| 395 |
Operators can be regrouped according to the usual mathematical rules
|
| 396 |
+
only where the operators really are associative or commutative.[^8] For
|
| 397 |
example, in the following fragment
|
| 398 |
|
| 399 |
``` cpp
|
| 400 |
int a, b;
|
| 401 |
+
...
|
| 402 |
a = a + 32760 + b + 5;
|
| 403 |
```
|
| 404 |
|
| 405 |
the expression statement behaves exactly the same as
|
| 406 |
|
|
|
|
| 437 |
or -17 and 12. However on a machine in which overflows do not produce an
|
| 438 |
exception and in which the results of overflows are reversible, the
|
| 439 |
above expression statement can be rewritten by the implementation in any
|
| 440 |
of the above ways because the same result will occur.
|
| 441 |
|
| 442 |
+
— *end note*]
|
| 443 |
+
|
| 444 |
+
A *constituent expression* is defined as follows:
|
| 445 |
+
|
| 446 |
+
- The constituent expression of an expression is that expression.
|
| 447 |
+
- The constituent expressions of a *braced-init-list* or of a (possibly
|
| 448 |
+
parenthesized) *expression-list* are the constituent expressions of
|
| 449 |
+
the elements of the respective list.
|
| 450 |
+
- The constituent expressions of a *brace-or-equal-initializer* of the
|
| 451 |
+
form `=` *initializer-clause* are the constituent expressions of the
|
| 452 |
+
*initializer-clause*.
|
| 453 |
+
|
| 454 |
+
[*Example 1*:
|
| 455 |
+
|
| 456 |
+
``` cpp
|
| 457 |
+
struct A { int x; };
|
| 458 |
+
struct B { int y; struct A a; };
|
| 459 |
+
B b = { 5, { 1+1 } };
|
| 460 |
+
```
|
| 461 |
+
|
| 462 |
+
The constituent expressions of the *initializer* used for the
|
| 463 |
+
initialization of `b` are `5` and `1+1`.
|
| 464 |
+
|
| 465 |
+
— *end example*]
|
| 466 |
+
|
| 467 |
+
The *immediate subexpressions* of an expression `e` are
|
| 468 |
+
|
| 469 |
+
- the constituent expressions of `e`’s operands (Clause [[expr]]),
|
| 470 |
+
- any function call that `e` implicitly invokes,
|
| 471 |
+
- if `e` is a *lambda-expression* ([[expr.prim.lambda]]), the
|
| 472 |
+
initialization of the entities captured by copy and the constituent
|
| 473 |
+
expressions of the *initializer* of the *init-capture*s,
|
| 474 |
+
- if `e` is a function call ([[expr.call]]) or implicitly invokes a
|
| 475 |
+
function, the constituent expressions of each default argument (
|
| 476 |
+
[[dcl.fct.default]]) used in the call, or
|
| 477 |
+
- if `e` creates an aggregate object ([[dcl.init.aggr]]), the
|
| 478 |
+
constituent expressions of each default member initializer (
|
| 479 |
+
[[class.mem]]) used in the initialization.
|
| 480 |
+
|
| 481 |
+
A *subexpression* of an expression `e` is an immediate subexpression of
|
| 482 |
+
`e` or a subexpression of an immediate subexpression of `e`.
|
| 483 |
+
|
| 484 |
+
[*Note 4*: Expressions appearing in the *compound-statement* of a
|
| 485 |
+
*lambda-expression* are not subexpressions of the
|
| 486 |
+
*lambda-expression*. — *end note*]
|
| 487 |
+
|
| 488 |
+
A *full-expression* is
|
| 489 |
+
|
| 490 |
+
- an unevaluated operand (Clause [[expr]]),
|
| 491 |
+
- a *constant-expression* ([[expr.const]]),
|
| 492 |
+
- an *init-declarator* (Clause [[dcl.decl]]) or a *mem-initializer* (
|
| 493 |
+
[[class.base.init]]), including the constituent expressions of the
|
| 494 |
+
initializer,
|
| 495 |
+
- an invocation of a destructor generated at the end of the lifetime of
|
| 496 |
+
an object other than a temporary object ([[class.temporary]]), or
|
| 497 |
+
- an expression that is not a subexpression of another expression and
|
| 498 |
+
that is not otherwise part of a full-expression.
|
| 499 |
+
|
| 500 |
+
If a language construct is defined to produce an implicit call of a
|
| 501 |
+
function, a use of the language construct is considered to be an
|
| 502 |
+
expression for the purposes of this definition. Conversions applied to
|
| 503 |
+
the result of an expression in order to satisfy the requirements of the
|
| 504 |
+
language construct in which the expression appears are also considered
|
| 505 |
+
to be part of the full-expression. For an initializer, performing the
|
| 506 |
+
initialization of the entity (including evaluating default member
|
| 507 |
+
initializers of an aggregate) is also considered part of the
|
| 508 |
+
full-expression.
|
| 509 |
+
|
| 510 |
+
[*Example 2*:
|
| 511 |
|
| 512 |
``` cpp
|
| 513 |
struct S {
|
| 514 |
+
S(int i): I(i) { } // full-expression is initialization of I
|
| 515 |
int& v() { return I; }
|
| 516 |
+
~S() noexcept(false) { }
|
| 517 |
private:
|
| 518 |
int I;
|
| 519 |
};
|
| 520 |
|
| 521 |
S s1(1); // full-expression is call of S::S(int)
|
|
|
|
|
|
|
| 522 |
void f() {
|
| 523 |
+
S s2 = 2; // full-expression is call of S::S(int)
|
| 524 |
if (S(3).v()) // full-expression includes lvalue-to-rvalue and
|
| 525 |
// int to bool conversions, performed before
|
| 526 |
// temporary is deleted at end of full-expression
|
| 527 |
{ }
|
| 528 |
+
bool b = noexcept(S()); // exception specification of destructor of S
|
| 529 |
+
// considered for noexcept
|
| 530 |
+
// full-expression is destruction of s2 at end of block
|
| 531 |
}
|
| 532 |
+
struct B {
|
| 533 |
+
B(S = S(0));
|
| 534 |
+
};
|
| 535 |
+
B b[2] = { B(), B() }; // full-expression is the entire initialization
|
| 536 |
+
// including the destruction of temporaries
|
| 537 |
```
|
| 538 |
|
| 539 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
|
| 541 |
+
[*Note 5*: The evaluation of a full-expression can include the
|
| 542 |
+
evaluation of subexpressions that are not lexically part of the
|
| 543 |
+
full-expression. For example, subexpressions involved in evaluating
|
| 544 |
+
default arguments ([[dcl.fct.default]]) are considered to be created in
|
| 545 |
+
the expression that calls the function, not the expression that defines
|
| 546 |
+
the default argument. — *end note*]
|
| 547 |
+
|
| 548 |
+
Reading an object designated by a `volatile` glvalue ([[basic.lval]]),
|
| 549 |
+
modifying an object, calling a library I/O function, or calling a
|
| 550 |
+
function that does any of those operations are all *side effects*, which
|
| 551 |
+
are changes in the state of the execution environment. *Evaluation* of
|
| 552 |
+
an expression (or a subexpression) in general includes both value
|
| 553 |
+
computations (including determining the identity of an object for
|
| 554 |
+
glvalue evaluation and fetching a value previously assigned to an object
|
| 555 |
+
for prvalue evaluation) and initiation of side effects. When a call to a
|
| 556 |
+
library I/O function returns or an access through a volatile glvalue is
|
| 557 |
+
evaluated the side effect is considered complete, even though some
|
| 558 |
+
external actions implied by the call (such as the I/O itself) or by the
|
| 559 |
+
`volatile` access may not have completed yet.
|
| 560 |
|
| 561 |
*Sequenced before* is an asymmetric, transitive, pair-wise relation
|
| 562 |
between evaluations executed by a single thread (
|
| 563 |
[[intro.multithread]]), which induces a partial order among those
|
| 564 |
evaluations. Given any two evaluations *A* and *B*, if *A* is sequenced
|
| 565 |
+
before *B* (or, equivalently, *B* is *sequenced after* *A*), then the
|
| 566 |
+
execution of *A* shall precede the execution of *B*. If *A* is not
|
| 567 |
+
sequenced before *B* and *B* is not sequenced before *A*, then *A* and
|
| 568 |
+
*B* are *unsequenced*.
|
| 569 |
+
|
| 570 |
+
[*Note 6*: The execution of unsequenced evaluations can
|
| 571 |
+
overlap. — *end note*]
|
| 572 |
+
|
| 573 |
+
Evaluations *A* and *B* are *indeterminately sequenced* when either *A*
|
| 574 |
+
is sequenced before *B* or *B* is sequenced before *A*, but it is
|
| 575 |
+
unspecified which.
|
| 576 |
+
|
| 577 |
+
[*Note 7*: Indeterminately sequenced evaluations cannot overlap, but
|
| 578 |
+
either could be executed first. — *end note*]
|
| 579 |
+
|
| 580 |
+
An expression *X* is said to be sequenced before an expression *Y* if
|
| 581 |
+
every value computation and every side effect associated with the
|
| 582 |
+
expression *X* is sequenced before every value computation and every
|
| 583 |
+
side effect associated with the expression *Y*.
|
| 584 |
|
| 585 |
Every value computation and side effect associated with a
|
| 586 |
full-expression is sequenced before every value computation and side
|
| 587 |
+
effect associated with the next full-expression to be evaluated.[^9]
|
| 588 |
|
| 589 |
Except where noted, evaluations of operands of individual operators and
|
| 590 |
+
of subexpressions of individual expressions are unsequenced.
|
| 591 |
+
|
| 592 |
+
[*Note 8*: In an expression that is evaluated more than once during the
|
| 593 |
+
execution of a program, unsequenced and indeterminately sequenced
|
| 594 |
+
evaluations of its subexpressions need not be performed consistently in
|
| 595 |
+
different evaluations. — *end note*]
|
| 596 |
+
|
| 597 |
+
The value computations of the operands of an operator are sequenced
|
| 598 |
+
before the value computation of the result of the operator. If a side
|
| 599 |
+
effect on a memory location ([[intro.memory]]) is unsequenced relative
|
| 600 |
+
to either another side effect on the same memory location or a value
|
| 601 |
+
computation using the value of any object in the same memory location,
|
| 602 |
+
and they are not potentially concurrent ([[intro.multithread]]), the
|
| 603 |
+
behavior is undefined.
|
| 604 |
+
|
| 605 |
+
[*Note 9*: The next section imposes similar, but more complex
|
| 606 |
+
restrictions on potentially concurrent computations. — *end note*]
|
| 607 |
+
|
| 608 |
+
[*Example 3*:
|
| 609 |
|
| 610 |
``` cpp
|
| 611 |
+
void g(int i) {
|
|
|
|
|
|
|
| 612 |
i = 7, i++, i++; // i becomes 9
|
| 613 |
|
| 614 |
+
i = i++ + 1; // the value of i is incremented
|
| 615 |
+
i = i++ + i; // the behavior is undefined
|
| 616 |
i = i + 1; // the value of i is incremented
|
|
|
|
|
|
|
| 617 |
}
|
| 618 |
```
|
| 619 |
|
| 620 |
+
— *end example*]
|
| 621 |
+
|
| 622 |
When calling a function (whether or not the function is inline), every
|
| 623 |
value computation and side effect associated with any argument
|
| 624 |
expression, or with the postfix expression designating the called
|
| 625 |
function, is sequenced before execution of every expression or statement
|
| 626 |
+
in the body of the called function. For each function invocation *F*,
|
| 627 |
+
for every evaluation *A* that occurs within *F* and every evaluation *B*
|
| 628 |
+
that does not occur within *F* but is evaluated on the same thread and
|
| 629 |
+
as part of the same signal handler (if any), either *A* is sequenced
|
| 630 |
+
before *B* or *B* is sequenced before *A*.[^10]
|
| 631 |
+
|
| 632 |
+
[*Note 10*: If *A* and *B* would not otherwise be sequenced then they
|
| 633 |
+
are indeterminately sequenced. — *end note*]
|
| 634 |
+
|
| 635 |
+
Several contexts in C++cause evaluation of a function call, even though
|
| 636 |
+
no corresponding function call syntax appears in the translation unit.
|
| 637 |
+
|
| 638 |
+
[*Example 4*: Evaluation of a *new-expression* invokes one or more
|
| 639 |
+
allocation and constructor functions; see [[expr.new]]. For another
|
| 640 |
+
example, invocation of a conversion function ([[class.conv.fct]]) can
|
| 641 |
+
arise in contexts in which no function call syntax
|
| 642 |
+
appears. — *end example*]
|
| 643 |
+
|
| 644 |
+
The sequencing constraints on the execution of the called function (as
|
| 645 |
+
described above) are features of the function calls as evaluated,
|
| 646 |
+
whatever the syntax of the expression that calls the function might be.
|
| 647 |
+
|
| 648 |
+
If a signal handler is executed as a result of a call to the
|
| 649 |
+
`std::raise` function, then the execution of the handler is sequenced
|
| 650 |
+
after the invocation of the `std::raise` function and before its return.
|
| 651 |
+
|
| 652 |
+
[*Note 11*: When a signal is received for another reason, the execution
|
| 653 |
+
of the signal handler is usually unsequenced with respect to the rest of
|
| 654 |
+
the program. — *end note*]
|
| 655 |
|
| 656 |
## Multi-threaded executions and data races <a id="intro.multithread">[[intro.multithread]]</a>
|
| 657 |
|
| 658 |
A *thread of execution* (also known as a *thread*) is a single flow of
|
| 659 |
control within a program, including the initial invocation of a specific
|
| 660 |
top-level function, and recursively including every function invocation
|
| 661 |
+
subsequently executed by the thread.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 662 |
|
| 663 |
+
[*Note 1*: When one thread creates another, the initial call to the
|
| 664 |
+
top-level function of the new thread is executed by the new thread, not
|
| 665 |
+
by the creating thread. — *end note*]
|
|
|
|
| 666 |
|
| 667 |
+
Every thread in a program can potentially access every object and
|
| 668 |
+
function in a program.[^11] Under a hosted implementation, a C++program
|
| 669 |
+
can have more than one thread running concurrently. The execution of
|
| 670 |
+
each thread proceeds as defined by the remainder of this International
|
| 671 |
+
Standard. The execution of the entire program consists of an execution
|
| 672 |
+
of all of its threads.
|
| 673 |
|
| 674 |
+
[*Note 2*: Usually the execution can be viewed as an interleaving of
|
| 675 |
+
all its threads. However, some kinds of atomic operations, for example,
|
| 676 |
+
allow executions inconsistent with a simple interleaving, as described
|
| 677 |
+
below. — *end note*]
|
| 678 |
|
| 679 |
+
Under a freestanding implementation, it is *implementation-defined*
|
| 680 |
+
whether a program can have more than one thread of execution.
|
| 681 |
+
|
| 682 |
+
For a signal handler that is not executed as a result of a call to the
|
| 683 |
+
`std::raise` function, it is unspecified which thread of execution
|
| 684 |
+
contains the signal handler invocation.
|
| 685 |
+
|
| 686 |
+
### Data races <a id="intro.races">[[intro.races]]</a>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
|
| 688 |
The value of an object visible to a thread *T* at a particular point is
|
| 689 |
the initial value of the object, a value assigned to the object by *T*,
|
| 690 |
or a value assigned to the object by another thread, according to the
|
| 691 |
+
rules below.
|
| 692 |
+
|
| 693 |
+
[*Note 1*: In some cases, there may instead be undefined behavior. Much
|
| 694 |
+
of this section is motivated by the desire to support atomic operations
|
| 695 |
+
with explicit and detailed visibility constraints. However, it also
|
| 696 |
+
implicitly supports a simpler view for more restricted
|
| 697 |
+
programs. — *end note*]
|
| 698 |
|
| 699 |
Two expression evaluations *conflict* if one of them modifies a memory
|
| 700 |
+
location ([[intro.memory]]) and the other one reads or modifies the
|
| 701 |
same memory location.
|
| 702 |
|
| 703 |
The library defines a number of atomic operations (Clause [[atomics]])
|
| 704 |
and operations on mutexes (Clause [[thread]]) that are specially
|
| 705 |
identified as synchronization operations. These operations play a
|
|
|
|
| 709 |
acquire and release operation. A synchronization operation without an
|
| 710 |
associated memory location is a fence and can be either an acquire
|
| 711 |
fence, a release fence, or both an acquire and release fence. In
|
| 712 |
addition, there are relaxed atomic operations, which are not
|
| 713 |
synchronization operations, and atomic read-modify-write operations,
|
| 714 |
+
which have special characteristics.
|
| 715 |
+
|
| 716 |
+
[*Note 2*: For example, a call that acquires a mutex will perform an
|
| 717 |
+
acquire operation on the locations comprising the mutex.
|
| 718 |
+
Correspondingly, a call that releases the same mutex will perform a
|
| 719 |
+
release operation on those same locations. Informally, performing a
|
| 720 |
release operation on *A* forces prior side effects on other memory
|
| 721 |
locations to become visible to other threads that later perform a
|
| 722 |
consume or an acquire operation on *A*. “Relaxed” atomic operations are
|
| 723 |
not synchronization operations even though, like synchronization
|
| 724 |
+
operations, they cannot contribute to data races. — *end note*]
|
| 725 |
|
| 726 |
All modifications to a particular atomic object *M* occur in some
|
| 727 |
+
particular total order, called the *modification order* of *M*.
|
| 728 |
+
|
| 729 |
+
[*Note 3*: There is a separate order for each atomic object. There is
|
| 730 |
+
no requirement that these can be combined into a single total order for
|
| 731 |
+
all objects. In general this will be impossible since different threads
|
| 732 |
+
may observe modifications to different objects in inconsistent
|
| 733 |
+
orders. — *end note*]
|
|
|
|
|
|
|
| 734 |
|
| 735 |
A *release sequence* headed by a release operation *A* on an atomic
|
| 736 |
object *M* is a maximal contiguous sub-sequence of side effects in the
|
| 737 |
modification order of *M*, where the first operation is `A`, and every
|
| 738 |
subsequent operation
|
|
|
|
| 741 |
- is an atomic read-modify-write operation.
|
| 742 |
|
| 743 |
Certain library calls *synchronize with* other library calls performed
|
| 744 |
by another thread. For example, an atomic store-release synchronizes
|
| 745 |
with a load-acquire that takes its value from the store (
|
| 746 |
+
[[atomics.order]]).
|
| 747 |
+
|
| 748 |
+
[*Note 4*: Except in the specified cases, reading a later value does
|
| 749 |
+
not necessarily ensure visibility as described below. Such a requirement
|
| 750 |
+
would sometimes interfere with efficient implementation. — *end note*]
|
| 751 |
+
|
| 752 |
+
[*Note 5*: The specifications of the synchronization operations define
|
| 753 |
+
when one reads the value written by another. For atomic objects, the
|
| 754 |
+
definition is clear. All operations on a given mutex occur in a single
|
| 755 |
+
total order. Each mutex acquisition “reads the value written” by the
|
| 756 |
+
last mutex release. — *end note*]
|
| 757 |
|
| 758 |
An evaluation *A* *carries a dependency* to an evaluation *B* if
|
| 759 |
|
| 760 |
- the value of *A* is used as an operand of *B*, unless:
|
| 761 |
- *B* is an invocation of any specialization of
|
|
|
|
| 772 |
- *A* writes a scalar object or bit-field *M*, *B* reads the value
|
| 773 |
written by *A* from *M*, and *A* is sequenced before *B*, or
|
| 774 |
- for some evaluation *X*, *A* carries a dependency to *X*, and *X*
|
| 775 |
carries a dependency to *B*.
|
| 776 |
|
| 777 |
+
[*Note 6*: “Carries a dependency to” is a subset of “is sequenced
|
| 778 |
+
before”, and is similarly strictly intra-thread. — *end note*]
|
| 779 |
|
| 780 |
An evaluation *A* is *dependency-ordered before* an evaluation *B* if
|
| 781 |
|
| 782 |
- *A* performs a release operation on an atomic object *M*, and, in
|
| 783 |
another thread, *B* performs a consume operation on *M* and reads a
|
| 784 |
value written by any side effect in the release sequence headed by
|
| 785 |
*A*, or
|
| 786 |
- for some evaluation *X*, *A* is dependency-ordered before *X* and *X*
|
| 787 |
carries a dependency to *B*.
|
| 788 |
|
| 789 |
+
[*Note 7*: The relation “is dependency-ordered before” is analogous to
|
| 790 |
“synchronizes with”, but uses release/consume in place of
|
| 791 |
+
release/acquire. — *end note*]
|
| 792 |
|
| 793 |
An evaluation *A* *inter-thread happens before* an evaluation *B* if
|
| 794 |
|
| 795 |
- *A* synchronizes with *B*, or
|
| 796 |
- *A* is dependency-ordered before *B*, or
|
|
|
|
| 799 |
- *A* is sequenced before *X* and *X* inter-thread happens before *B*,
|
| 800 |
or
|
| 801 |
- *A* inter-thread happens before *X* and *X* inter-thread happens
|
| 802 |
before *B*.
|
| 803 |
|
| 804 |
+
[*Note 8*: The “inter-thread happens before” relation describes
|
| 805 |
+
arbitrary concatenations of “sequenced before”, “synchronizes with” and
|
| 806 |
“dependency-ordered before” relationships, with two exceptions. The
|
| 807 |
first exception is that a concatenation is not permitted to end with
|
| 808 |
“dependency-ordered before” followed by “sequenced before”. The reason
|
| 809 |
for this limitation is that a consume operation participating in a
|
| 810 |
“dependency-ordered before” relationship provides ordering only with
|
|
|
|
| 814 |
provide the required ordering for a prior consume operation. The second
|
| 815 |
exception is that a concatenation is not permitted to consist entirely
|
| 816 |
of “sequenced before”. The reasons for this limitation are (1) to permit
|
| 817 |
“inter-thread happens before” to be transitively closed and (2) the
|
| 818 |
“happens before” relation, defined below, provides for relationships
|
| 819 |
+
consisting entirely of “sequenced before”. — *end note*]
|
| 820 |
|
| 821 |
+
An evaluation *A* *happens before* an evaluation *B* (or, equivalently,
|
| 822 |
+
*B* *happens after* *A*) if:
|
| 823 |
|
| 824 |
- *A* is sequenced before *B*, or
|
| 825 |
- *A* inter-thread happens before *B*.
|
| 826 |
|
| 827 |
The implementation shall ensure that no program execution demonstrates a
|
| 828 |
+
cycle in the “happens before” relation.
|
| 829 |
+
|
| 830 |
+
[*Note 9*: This cycle would otherwise be possible only through the use
|
| 831 |
+
of consume operations. — *end note*]
|
| 832 |
+
|
| 833 |
+
An evaluation *A* *strongly happens before* an evaluation *B* if either
|
| 834 |
+
|
| 835 |
+
- *A* is sequenced before *B*, or
|
| 836 |
+
- *A* synchronizes with *B*, or
|
| 837 |
+
- *A* strongly happens before *X* and *X* strongly happens before *B*.
|
| 838 |
+
|
| 839 |
+
[*Note 10*: In the absence of consume operations, the happens before
|
| 840 |
+
and strongly happens before relations are identical. Strongly happens
|
| 841 |
+
before essentially excludes consume operations. — *end note*]
|
| 842 |
|
| 843 |
A *visible side effect* *A* on a scalar object or bit-field *M* with
|
| 844 |
respect to a value computation *B* of *M* satisfies the conditions:
|
| 845 |
|
| 846 |
- *A* happens before *B* and
|
| 847 |
- there is no other side effect *X* to *M* such that *A* happens before
|
| 848 |
*X* and *X* happens before *B*.
|
| 849 |
|
| 850 |
The value of a non-atomic scalar object or bit-field *M*, as determined
|
| 851 |
by evaluation *B*, shall be the value stored by the visible side effect
|
| 852 |
+
*A*.
|
| 853 |
+
|
| 854 |
+
[*Note 11*: If there is ambiguity about which side effect to a
|
| 855 |
+
non-atomic object or bit-field is visible, then the behavior is either
|
| 856 |
+
unspecified or undefined. — *end note*]
|
| 857 |
+
|
| 858 |
+
[*Note 12*: This states that operations on ordinary objects are not
|
| 859 |
visibly reordered. This is not actually detectable without data races,
|
| 860 |
but it is necessary to ensure that data races, as defined below, and
|
| 861 |
with suitable restrictions on the use of atomics, correspond to data
|
| 862 |
+
races in a simple interleaved (sequentially consistent)
|
| 863 |
+
execution. — *end note*]
|
| 864 |
|
| 865 |
The value of an atomic object *M*, as determined by evaluation *B*,
|
| 866 |
shall be the value stored by some side effect *A* that modifies *M*,
|
| 867 |
+
where *B* does not happen before *A*.
|
| 868 |
+
|
| 869 |
+
[*Note 13*: The set of such side effects is also restricted by the rest
|
| 870 |
+
of the rules described here, and in particular, by the coherence
|
| 871 |
+
requirements below. — *end note*]
|
| 872 |
|
| 873 |
If an operation *A* that modifies an atomic object *M* happens before an
|
| 874 |
operation *B* that modifies *M*, then *A* shall be earlier than *B* in
|
| 875 |
+
the modification order of *M*.
|
| 876 |
+
|
| 877 |
+
[*Note 14*: This requirement is known as write-write
|
| 878 |
+
coherence. — *end note*]
|
| 879 |
|
| 880 |
If a value computation *A* of an atomic object *M* happens before a
|
| 881 |
value computation *B* of *M*, and *A* takes its value from a side effect
|
| 882 |
*X* on *M*, then the value computed by *B* shall either be the value
|
| 883 |
stored by *X* or the value stored by a side effect *Y* on *M*, where *Y*
|
| 884 |
+
follows *X* in the modification order of *M*.
|
| 885 |
+
|
| 886 |
+
[*Note 15*: This requirement is known as read-read
|
| 887 |
+
coherence. — *end note*]
|
| 888 |
|
| 889 |
If a value computation *A* of an atomic object *M* happens before an
|
| 890 |
operation *B* that modifies *M*, then *A* shall take its value from a
|
| 891 |
side effect *X* on *M*, where *X* precedes *B* in the modification order
|
| 892 |
+
of *M*.
|
| 893 |
+
|
| 894 |
+
[*Note 16*: This requirement is known as read-write
|
| 895 |
+
coherence. — *end note*]
|
| 896 |
|
| 897 |
If a side effect *X* on an atomic object *M* happens before a value
|
| 898 |
computation *B* of *M*, then the evaluation *B* shall take its value
|
| 899 |
from *X* or from a side effect *Y* that follows *X* in the modification
|
| 900 |
+
order of *M*.
|
| 901 |
|
| 902 |
+
[*Note 17*: This requirement is known as write-read
|
| 903 |
+
coherence. — *end note*]
|
|
|
|
|
|
|
| 904 |
|
| 905 |
+
[*Note 18*: The four preceding coherence requirements effectively
|
| 906 |
+
disallow compiler reordering of atomic operations to a single object,
|
| 907 |
+
even if both operations are relaxed loads. This effectively makes the
|
| 908 |
+
cache coherence guarantee provided by most hardware available to
|
| 909 |
+
C++atomic operations. — *end note*]
|
| 910 |
+
|
| 911 |
+
[*Note 19*: The value observed by a load of an atomic depends on the
|
| 912 |
+
“happens before” relation, which depends on the values observed by loads
|
| 913 |
+
of atomics. The intended reading is that there must exist an association
|
| 914 |
+
of atomic loads with modifications they observe that, together with
|
| 915 |
suitably chosen modification orders and the “happens before” relation
|
| 916 |
derived as described above, satisfy the resulting constraints as imposed
|
| 917 |
+
here. — *end note*]
|
| 918 |
|
| 919 |
Two actions are *potentially concurrent* if
|
| 920 |
|
| 921 |
- they are performed by different threads, or
|
| 922 |
+
- they are unsequenced, at least one is performed by a signal handler,
|
| 923 |
+
and they are not both performed by the same signal handler invocation.
|
| 924 |
|
| 925 |
The execution of a program contains a *data race* if it contains two
|
| 926 |
potentially concurrent conflicting actions, at least one of which is not
|
| 927 |
atomic, and neither happens before the other, except for the special
|
| 928 |
case for signal handlers described below. Any such data race results in
|
| 929 |
+
undefined behavior.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 930 |
|
| 931 |
+
[*Note 20*: It can be shown that programs that correctly use mutexes
|
| 932 |
+
and `memory_order_seq_cst` operations to prevent all data races and use
|
| 933 |
+
no other synchronization operations behave as if the operations executed
|
| 934 |
+
by their constituent threads were simply interleaved, with each value
|
| 935 |
+
computation of an object being taken from the last side effect on that
|
| 936 |
+
object in that interleaving. This is normally referred to as “sequential
|
| 937 |
+
consistency”. However, this applies only to data-race-free programs, and
|
| 938 |
+
data-race-free programs cannot observe most program transformations that
|
| 939 |
+
do not change single-threaded program semantics. In fact, most
|
| 940 |
+
single-threaded program transformations continue to be allowed, since
|
| 941 |
+
any program that behaves differently as a result must perform an
|
| 942 |
+
undefined operation. — *end note*]
|
| 943 |
+
|
| 944 |
+
Two accesses to the same object of type `volatile std::sig_atomic_t` do
|
| 945 |
+
not result in a data race if both occur in the same thread, even if one
|
| 946 |
+
or more occurs in a signal handler. For each signal handler invocation,
|
| 947 |
evaluations performed by the thread invoking a signal handler can be
|
| 948 |
divided into two groups *A* and *B*, such that no evaluations in *B*
|
| 949 |
happen before evaluations in *A*, and the evaluations of such
|
| 950 |
+
`volatile std::sig_atomic_t` objects take values as though all
|
| 951 |
+
evaluations in *A* happened before the execution of the signal handler
|
| 952 |
+
and the execution of the signal handler happened before all evaluations
|
| 953 |
+
in *B*.
|
| 954 |
|
| 955 |
+
[*Note 21*: Compiler transformations that introduce assignments to a
|
| 956 |
+
potentially shared memory location that would not be modified by the
|
| 957 |
+
abstract machine are generally precluded by this International Standard,
|
| 958 |
+
since such an assignment might overwrite another assignment by a
|
| 959 |
+
different thread in cases in which an abstract machine execution would
|
| 960 |
+
not have encountered a data race. This includes implementations of data
|
| 961 |
+
member assignment that overwrite adjacent members in separate memory
|
| 962 |
+
locations. Reordering of atomic loads in cases in which the atomics in
|
| 963 |
+
question may alias is also generally precluded, since this may violate
|
| 964 |
+
the coherence rules. — *end note*]
|
| 965 |
|
| 966 |
+
[*Note 22*: Transformations that introduce a speculative read of a
|
| 967 |
+
potentially shared memory location may not preserve the semantics of the
|
| 968 |
+
C++program as defined in this International Standard, since they
|
| 969 |
+
potentially introduce a data race. However, they are typically valid in
|
| 970 |
+
the context of an optimizing compiler that targets a specific machine
|
| 971 |
+
with well-defined semantics for data races. They would be invalid for a
|
| 972 |
+
hypothetical machine that is not tolerant of races or provides hardware
|
| 973 |
+
race detection. — *end note*]
|
| 974 |
+
|
| 975 |
+
### Forward progress <a id="intro.progress">[[intro.progress]]</a>
|
| 976 |
|
| 977 |
The implementation may assume that any thread will eventually do one of
|
| 978 |
the following:
|
| 979 |
|
| 980 |
- terminate,
|
| 981 |
- make a call to a library I/O function,
|
| 982 |
+
- perform an access through a volatile glvalue, or
|
| 983 |
- perform a synchronization operation or an atomic operation.
|
| 984 |
|
| 985 |
+
[*Note 1*: This is intended to allow compiler transformations such as
|
| 986 |
+
removal of empty loops, even when termination cannot be
|
| 987 |
+
proven. — *end note*]
|
| 988 |
+
|
| 989 |
+
Executions of atomic functions that are either defined to be lock-free (
|
| 990 |
+
[[atomics.flag]]) or indicated as lock-free ([[atomics.lockfree]]) are
|
| 991 |
+
*lock-free executions*.
|
| 992 |
+
|
| 993 |
+
- If there is only one thread that is not blocked ([[defns.block]]) in
|
| 994 |
+
a standard library function, a lock-free execution in that thread
|
| 995 |
+
shall complete. \[*Note 2*: Concurrently executing threads may prevent
|
| 996 |
+
progress of a lock-free execution. For example, this situation can
|
| 997 |
+
occur with load-locked store-conditional implementations. This
|
| 998 |
+
property is sometimes termed obstruction-free. — *end note*]
|
| 999 |
+
- When one or more lock-free executions run concurrently, at least one
|
| 1000 |
+
should complete. \[*Note 3*: It is difficult for some implementations
|
| 1001 |
+
to provide absolute guarantees to this effect, since repeated and
|
| 1002 |
+
particularly inopportune interference from other threads may prevent
|
| 1003 |
+
forward progress, e.g., by repeatedly stealing a cache line for
|
| 1004 |
+
unrelated purposes between load-locked and store-conditional
|
| 1005 |
+
instructions. Implementations should ensure that such effects cannot
|
| 1006 |
+
indefinitely delay progress under expected operating conditions, and
|
| 1007 |
+
that such anomalies can therefore safely be ignored by programmers.
|
| 1008 |
+
Outside this document, this property is sometimes termed
|
| 1009 |
+
lock-free. — *end note*]
|
| 1010 |
+
|
| 1011 |
+
During the execution of a thread of execution, each of the following is
|
| 1012 |
+
termed an *execution step*:
|
| 1013 |
+
|
| 1014 |
+
- termination of the thread of execution,
|
| 1015 |
+
- performing an access through a volatile glvalue, or
|
| 1016 |
+
- completion of a call to a library I/O function, a synchronization
|
| 1017 |
+
operation, or an atomic operation.
|
| 1018 |
+
|
| 1019 |
+
An invocation of a standard library function that blocks (
|
| 1020 |
+
[[defns.block]]) is considered to continuously execute execution steps
|
| 1021 |
+
while waiting for the condition that it blocks on to be satisfied.
|
| 1022 |
+
|
| 1023 |
+
[*Example 1*: A library I/O function that blocks until the I/O
|
| 1024 |
+
operation is complete can be considered to continuously check whether
|
| 1025 |
+
the operation is complete. Each such check might consist of one or more
|
| 1026 |
+
execution steps, for example using observable behavior of the abstract
|
| 1027 |
+
machine. — *end example*]
|
| 1028 |
+
|
| 1029 |
+
[*Note 4*: Because of this and the preceding requirement regarding what
|
| 1030 |
+
threads of execution have to perform eventually, it follows that no
|
| 1031 |
+
thread of execution can execute forever without an execution step
|
| 1032 |
+
occurring. — *end note*]
|
| 1033 |
+
|
| 1034 |
+
A thread of execution *makes progress* when an execution step occurs or
|
| 1035 |
+
a lock-free execution does not complete because there are other
|
| 1036 |
+
concurrent threads that are not blocked in a standard library function
|
| 1037 |
+
(see above).
|
| 1038 |
+
|
| 1039 |
+
For a thread of execution providing *concurrent forward progress
|
| 1040 |
+
guarantees*, the implementation ensures that the thread will eventually
|
| 1041 |
+
make progress for as long as it has not terminated.
|
| 1042 |
+
|
| 1043 |
+
[*Note 5*: This is required regardless of whether or not other threads
|
| 1044 |
+
of executions (if any) have been or are making progress. To eventually
|
| 1045 |
+
fulfill this requirement means that this will happen in an unspecified
|
| 1046 |
+
but finite amount of time. — *end note*]
|
| 1047 |
+
|
| 1048 |
+
It is *implementation-defined* whether the implementation-created thread
|
| 1049 |
+
of execution that executes `main` ([[basic.start.main]]) and the
|
| 1050 |
+
threads of execution created by `std::thread` ([[thread.thread.class]])
|
| 1051 |
+
provide concurrent forward progress guarantees.
|
| 1052 |
+
|
| 1053 |
+
[*Note 6*: General-purpose implementations are encouraged to provide
|
| 1054 |
+
these guarantees. — *end note*]
|
| 1055 |
+
|
| 1056 |
+
For a thread of execution providing *parallel forward progress
|
| 1057 |
+
guarantees*, the implementation is not required to ensure that the
|
| 1058 |
+
thread will eventually make progress if it has not yet executed any
|
| 1059 |
+
execution step; once this thread has executed a step, it provides
|
| 1060 |
+
concurrent forward progress guarantees.
|
| 1061 |
+
|
| 1062 |
+
[*Note 7*: This does not specify a requirement for when to start this
|
| 1063 |
+
thread of execution, which will typically be specified by the entity
|
| 1064 |
+
that creates this thread of execution. For example, a thread of
|
| 1065 |
+
execution that provides concurrent forward progress guarantees and
|
| 1066 |
+
executes tasks from a set of tasks in an arbitrary order, one after the
|
| 1067 |
+
other, satisfies the requirements of parallel forward progress for these
|
| 1068 |
+
tasks. — *end note*]
|
| 1069 |
+
|
| 1070 |
+
For a thread of execution providing *weakly parallel forward progress
|
| 1071 |
+
guarantees*, the implementation does not ensure that the thread will
|
| 1072 |
+
eventually make progress.
|
| 1073 |
+
|
| 1074 |
+
[*Note 8*: Threads of execution providing weakly parallel forward
|
| 1075 |
+
progress guarantees cannot be expected to make progress regardless of
|
| 1076 |
+
whether other threads make progress or not; however, blocking with
|
| 1077 |
+
forward progress guarantee delegation, as defined below, can be used to
|
| 1078 |
+
ensure that such threads of execution make progress
|
| 1079 |
+
eventually. — *end note*]
|
| 1080 |
+
|
| 1081 |
+
Concurrent forward progress guarantees are stronger than parallel
|
| 1082 |
+
forward progress guarantees, which in turn are stronger than weakly
|
| 1083 |
+
parallel forward progress guarantees.
|
| 1084 |
+
|
| 1085 |
+
[*Note 9*: For example, some kinds of synchronization between threads
|
| 1086 |
+
of execution may only make progress if the respective threads of
|
| 1087 |
+
execution provide parallel forward progress guarantees, but will fail to
|
| 1088 |
+
make progress under weakly parallel guarantees. — *end note*]
|
| 1089 |
+
|
| 1090 |
+
When a thread of execution *P* is specified to *block with forward
|
| 1091 |
+
progress guarantee delegation* on the completion of a set *S* of threads
|
| 1092 |
+
of execution, then throughout the whole time of *P* being blocked on
|
| 1093 |
+
*S*, the implementation shall ensure that the forward progress
|
| 1094 |
+
guarantees provided by at least one thread of execution in *S* is at
|
| 1095 |
+
least as strong as *P*’s forward progress guarantees.
|
| 1096 |
+
|
| 1097 |
+
[*Note 10*: It is unspecified which thread or threads of execution in
|
| 1098 |
+
*S* are chosen and for which number of execution steps. The
|
| 1099 |
+
strengthening is not permanent and not necessarily in place for the rest
|
| 1100 |
+
of the lifetime of the affected thread of execution. As long as *P* is
|
| 1101 |
+
blocked, the implementation has to eventually select and potentially
|
| 1102 |
+
strengthen a thread of execution in *S*. — *end note*]
|
| 1103 |
+
|
| 1104 |
+
Once a thread of execution in *S* terminates, it is removed from *S*.
|
| 1105 |
+
Once *S* is empty, *P* is unblocked.
|
| 1106 |
+
|
| 1107 |
+
[*Note 11*: A thread of execution *B* thus can temporarily provide an
|
| 1108 |
+
effectively stronger forward progress guarantee for a certain amount of
|
| 1109 |
+
time, due to a second thread of execution *A* being blocked on it with
|
| 1110 |
+
forward progress guarantee delegation. In turn, if *B* then blocks with
|
| 1111 |
+
forward progress guarantee delegation on *C*, this may also temporarily
|
| 1112 |
+
provide a stronger forward progress guarantee to *C*. — *end note*]
|
| 1113 |
+
|
| 1114 |
+
[*Note 12*: If all threads of execution in *S* finish executing (e.g.,
|
| 1115 |
+
they terminate and do not use blocking synchronization incorrectly),
|
| 1116 |
+
then *P*’s execution of the operation that blocks with forward progress
|
| 1117 |
+
guarantee delegation will not result in *P*’s progress guarantee being
|
| 1118 |
+
effectively weakened. — *end note*]
|
| 1119 |
+
|
| 1120 |
+
[*Note 13*: This does not remove any constraints regarding blocking
|
| 1121 |
+
synchronization for threads of execution providing parallel or weakly
|
| 1122 |
+
parallel forward progress guarantees because the implementation is not
|
| 1123 |
+
required to strengthen a particular thread of execution whose too-weak
|
| 1124 |
+
progress guarantee is preventing overall progress. — *end note*]
|
| 1125 |
|
| 1126 |
An implementation should ensure that the last value (in modification
|
| 1127 |
order) assigned by an atomic or synchronization operation will become
|
| 1128 |
visible to all other threads in a finite period of time.
|
| 1129 |
|
| 1130 |
## Acknowledgments <a id="intro.ack">[[intro.ack]]</a>
|
| 1131 |
|
| 1132 |
+
The C++programming language as described in this document is based on
|
| 1133 |
+
the language as described in Chapter R (Reference Manual) of Stroustrup:
|
| 1134 |
+
*The C++Programming Language* (second edition, Addison-Wesley Publishing
|
| 1135 |
+
Company, ISBN 0-201-53992-6, copyright ©1991 AT&T). That, in turn, is
|
| 1136 |
+
based on the C programming language as described in Appendix A of
|
| 1137 |
+
Kernighan and Ritchie: *The C Programming Language* (Prentice-Hall,
|
| 1138 |
+
1978, ISBN 0-13-110163-3, copyright ©1978 AT&T).
|
|
|
|
| 1139 |
|
| 1140 |
+
Portions of the library Clauses of this document are based on work by
|
| 1141 |
+
P.J. Plauger, which was published as *The Draft Standard C++Library*
|
| 1142 |
+
(Prentice-Hall, ISBN 0-13-117003-1, copyright ©1995 P.J. Plauger).
|
|
|
|
| 1143 |
|
| 1144 |
POSIX® is a registered trademark of the Institute of Electrical and
|
| 1145 |
Electronic Engineers, Inc.
|
| 1146 |
|
| 1147 |
+
ECMAScript® is a registered trademark of Ecma International.
|
| 1148 |
+
|
| 1149 |
All rights in these originals are reserved.
|
| 1150 |
|
| 1151 |
<!-- Link reference definitions -->
|
| 1152 |
[atomics]: atomics.md#atomics
|
| 1153 |
[atomics.flag]: atomics.md#atomics.flag
|
|
|
|
| 1159 |
[basic.def.odr]: basic.md#basic.def.odr
|
| 1160 |
[basic.life]: basic.md#basic.life
|
| 1161 |
[basic.link]: basic.md#basic.link
|
| 1162 |
[basic.lval]: basic.md#basic.lval
|
| 1163 |
[basic.namespace]: dcl.md#basic.namespace
|
| 1164 |
+
[basic.start.main]: basic.md#basic.start.main
|
| 1165 |
[basic.stc]: basic.md#basic.stc
|
| 1166 |
[basic.stc.auto]: basic.md#basic.stc.auto
|
| 1167 |
[basic.types]: basic.md#basic.types
|
| 1168 |
[class.access]: class.md#class.access
|
| 1169 |
+
[class.base.init]: special.md#class.base.init
|
| 1170 |
[class.bit]: class.md#class.bit
|
| 1171 |
+
[class.cdtor]: special.md#class.cdtor
|
| 1172 |
[class.conv.fct]: special.md#class.conv.fct
|
| 1173 |
[class.derived]: class.md#class.derived
|
| 1174 |
[class.mem]: class.md#class.mem
|
| 1175 |
[class.temporary]: special.md#class.temporary
|
| 1176 |
+
[class.union]: class.md#class.union
|
| 1177 |
[class.virtual]: class.md#class.virtual
|
| 1178 |
[compliance]: library.md#compliance
|
| 1179 |
+
[conv.rval]: conv.md#conv.rval
|
| 1180 |
[cpp]: cpp.md#cpp
|
| 1181 |
[cpp.include]: cpp.md#cpp.include
|
| 1182 |
+
[cpp.replace]: cpp.md#cpp.replace
|
| 1183 |
+
[cstddef.syn]: language.md#cstddef.syn
|
| 1184 |
+
[dcl.decl]: dcl.md#dcl.decl
|
| 1185 |
[dcl.fct]: dcl.md#dcl.fct
|
| 1186 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 1187 |
+
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 1188 |
[dcl.ptr]: dcl.md#dcl.ptr
|
| 1189 |
[dcl.ref]: dcl.md#dcl.ref
|
| 1190 |
[definitions]: library.md#definitions
|
| 1191 |
+
[defns.block]: #defns.block
|
| 1192 |
+
[defns.well.formed]: #defns.well.formed
|
| 1193 |
[depr]: future.md#depr
|
| 1194 |
[diff]: compatibility.md#diff
|
| 1195 |
[diff.library]: compatibility.md#diff.library
|
| 1196 |
[expr]: expr.md#expr
|
| 1197 |
+
[expr.call]: expr.md#expr.call
|
| 1198 |
[expr.comma]: expr.md#expr.comma
|
| 1199 |
[expr.cond]: expr.md#expr.cond
|
| 1200 |
+
[expr.const]: expr.md#expr.const
|
| 1201 |
[expr.log.and]: expr.md#expr.log.and
|
| 1202 |
[expr.log.or]: expr.md#expr.log.or
|
| 1203 |
[expr.new]: expr.md#expr.new
|
| 1204 |
+
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
| 1205 |
+
[expr.throw]: expr.md#expr.throw
|
| 1206 |
[gram]: grammar.md#gram
|
| 1207 |
[implimits]: limits.md#implimits
|
| 1208 |
[intro]: #intro
|
| 1209 |
[intro.ack]: #intro.ack
|
| 1210 |
[intro.compliance]: #intro.compliance
|
| 1211 |
[intro.defs]: #intro.defs
|
| 1212 |
[intro.execution]: #intro.execution
|
| 1213 |
[intro.memory]: #intro.memory
|
| 1214 |
[intro.multithread]: #intro.multithread
|
| 1215 |
[intro.object]: #intro.object
|
| 1216 |
+
[intro.progress]: #intro.progress
|
| 1217 |
+
[intro.races]: #intro.races
|
| 1218 |
[intro.refs]: #intro.refs
|
| 1219 |
[intro.scope]: #intro.scope
|
| 1220 |
[intro.structure]: #intro.structure
|
| 1221 |
[language.support]: language.md#language.support
|
| 1222 |
[lex]: lex.md#lex
|
| 1223 |
[lex.charset]: lex.md#lex.charset
|
| 1224 |
[lex.phases]: lex.md#lex.phases
|
| 1225 |
[library]: library.md#library
|
| 1226 |
[syntax]: #syntax
|
| 1227 |
+
[temp.arg]: temp.md#temp.arg
|
| 1228 |
+
[temp.deduct]: temp.md#temp.deduct
|
| 1229 |
[thread]: thread.md#thread
|
| 1230 |
+
[thread.thread.class]: thread.md#thread.thread.class
|
| 1231 |
|
| 1232 |
[^1]: With the qualifications noted in Clauses [[language.support]]
|
| 1233 |
through [[thread]] and in [[diff.library]], the C standard library
|
| 1234 |
is a subset of the C++standard library.
|
| 1235 |
|
| 1236 |
[^2]: “Correct execution” can include undefined behavior, depending on
|
| 1237 |
+
the data being processed; see Clause [[intro.defs]] and
|
| 1238 |
[[intro.execution]].
|
| 1239 |
|
| 1240 |
[^3]: This documentation also defines implementation-defined behavior;
|
| 1241 |
see [[intro.execution]].
|
| 1242 |
|
| 1243 |
+
[^4]: The number of bits in a byte is reported by the macro `CHAR_BIT`
|
| 1244 |
+
in the header `<climits>`.
|
| 1245 |
+
|
| 1246 |
+
[^5]: Under the “as-if” rule an implementation is allowed to store two
|
| 1247 |
objects at the same machine address or not store an object at all if
|
| 1248 |
the program cannot observe the difference ([[intro.execution]]).
|
| 1249 |
|
| 1250 |
+
[^6]: This provision is sometimes called the “as-if” rule, because an
|
| 1251 |
implementation is free to disregard any requirement of this
|
| 1252 |
International Standard as long as the result is *as if* the
|
| 1253 |
requirement had been obeyed, as far as can be determined from the
|
| 1254 |
observable behavior of the program. For instance, an actual
|
| 1255 |
implementation need not evaluate part of an expression if it can
|
| 1256 |
deduce that its value is not used and that no side effects affecting
|
| 1257 |
the observable behavior of the program are produced.
|
| 1258 |
|
| 1259 |
+
[^7]: This documentation also includes conditionally-supported
|
| 1260 |
constructs and locale-specific behavior. See [[intro.compliance]].
|
| 1261 |
|
| 1262 |
+
[^8]: Overloaded operators are never assumed to be associative or
|
| 1263 |
commutative.
|
| 1264 |
|
| 1265 |
+
[^9]: As specified in [[class.temporary]], after a full-expression is
|
| 1266 |
evaluated, a sequence of zero or more invocations of destructor
|
| 1267 |
functions for temporary objects takes place, usually in reverse
|
| 1268 |
order of the construction of each temporary object.
|
| 1269 |
|
| 1270 |
+
[^10]: In other words, function executions do not interleave with each
|
| 1271 |
other.
|
| 1272 |
|
| 1273 |
+
[^11]: An object with automatic or thread storage duration (
|
| 1274 |
[[basic.stc]]) is associated with one specific thread, and can be
|
| 1275 |
accessed by a different thread only indirectly through a pointer or
|
| 1276 |
reference ([[basic.compound]]).
|