- tmp/tmpubp60ij6/{from.md → to.md} +1945 -1954
tmp/tmpubp60ij6/{from.md → to.md}
RENAMED
|
@@ -17,19 +17,38 @@ Clauses. — *end note*]
|
|
| 17 |
|
| 18 |
An *entity* is a value, object, reference, structured binding, function,
|
| 19 |
enumerator, type, class member, bit-field, template, template
|
| 20 |
specialization, namespace, or pack.
|
| 21 |
|
| 22 |
-
A *name* is
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
A *variable* is introduced by the declaration of a reference other than
|
| 33 |
a non-static data member or of an object. The variable’s name, if any,
|
| 34 |
denotes the reference or object.
|
| 35 |
|
|
@@ -46,27 +65,27 @@ it. The process that determines this is called *name lookup*
|
|
| 46 |
|
| 47 |
Two names are *the same* if
|
| 48 |
|
| 49 |
- they are *identifier*s composed of the same character sequence, or
|
| 50 |
- they are *operator-function-id*s formed with the same operator, or
|
| 51 |
-
- they are *conversion-function-id*s formed with
|
| 52 |
-
|
| 53 |
-
variable [[temp.type]], or
|
| 54 |
- they are *literal-operator-id*s [[over.literal]] formed with the same
|
| 55 |
literal suffix identifier.
|
| 56 |
|
| 57 |
A name used in more than one translation unit can potentially refer to
|
| 58 |
the same entity in these translation units depending on the linkage
|
| 59 |
[[basic.link]] of the name specified in each translation unit.
|
| 60 |
|
| 61 |
## Declarations and definitions <a id="basic.def">[[basic.def]]</a>
|
| 62 |
|
| 63 |
-
A declaration [[dcl.dcl]] may introduce one or more names
|
| 64 |
-
translation unit
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
- a static assertion [[dcl.pre]],
|
| 70 |
- controlling template instantiation [[temp.explicit]],
|
| 71 |
- guiding template argument deduction for constructors
|
| 72 |
[[temp.deduct.guide]],
|
|
@@ -79,16 +98,16 @@ declaration unless:
|
|
| 79 |
- it declares a function without specifying the function’s body
|
| 80 |
[[dcl.fct.def]],
|
| 81 |
- it contains the `extern` specifier [[dcl.stc]] or a
|
| 82 |
*linkage-specification*[^1] [[dcl.link]] and neither an *initializer*
|
| 83 |
nor a *function-body*,
|
| 84 |
-
- it declares a non-inline static data member in a class definition
|
| 85 |
-
[[class.mem]], [[class.static]]
|
| 86 |
- it declares a static data member outside a class definition and the
|
| 87 |
variable was defined within the class with the `constexpr` specifier
|
| 88 |
(this usage is deprecated; see [[depr.static.constexpr]]),
|
| 89 |
-
- it is
|
| 90 |
- it is an *opaque-enum-declaration* [[dcl.enum]],
|
| 91 |
- it is a *template-parameter* [[temp.param]],
|
| 92 |
- it is a *parameter-declaration* [[dcl.fct]] in a function declarator
|
| 93 |
that is not the *declarator* of a *function-definition*,
|
| 94 |
- it is a `typedef` declaration [[dcl.typedef]],
|
|
@@ -190,22 +209,31 @@ struct C {
|
|
| 190 |
|
| 191 |
[*Note 2*: A class name can also be implicitly declared by an
|
| 192 |
*elaborated-type-specifier* [[dcl.type.elab]]. — *end note*]
|
| 193 |
|
| 194 |
In the definition of an object, the type of that object shall not be an
|
| 195 |
-
incomplete type [[
|
| 196 |
-
[[class.abstract]], or a (possibly
|
| 197 |
|
| 198 |
## One-definition rule <a id="basic.def.odr">[[basic.def.odr]]</a>
|
| 199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
No translation unit shall contain more than one definition of any
|
| 201 |
-
|
| 202 |
-
argument for a parameter (for a function in a given scope), or default
|
| 203 |
-
template argument.
|
| 204 |
|
| 205 |
An expression or conversion is *potentially evaluated* unless it is an
|
| 206 |
-
unevaluated operand [[expr.
|
| 207 |
conversion in an initialization or conversion sequence in such a
|
| 208 |
context. The set of *potential results* of an expression E is defined as
|
| 209 |
follows:
|
| 210 |
|
| 211 |
- If E is an *id-expression* [[expr.prim.id]], the set contains only E.
|
|
@@ -250,52 +278,55 @@ int n = b ? (1, S::x) // S::x is not odr-used here
|
|
| 250 |
— *end note*]
|
| 251 |
|
| 252 |
A function is *named by* an expression or conversion as follows:
|
| 253 |
|
| 254 |
- A function is named by an expression or conversion if it is the
|
| 255 |
-
selected member of an overload set
|
| 256 |
-
[[over.over]]
|
| 257 |
-
|
| 258 |
-
and either the expression is not
|
| 259 |
-
|
| 260 |
-
pointer to member [[expr.unary.op]].
|
| 261 |
-
the address of functions
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
[[
|
| 266 |
-
|
| 267 |
-
|
|
|
|
| 268 |
[[class.copy.elision]]. — *end note*]
|
| 269 |
- A deallocation function for a class is named by a *new-expression* if
|
| 270 |
it is the single matching deallocation function for the allocation
|
| 271 |
function selected by overload resolution, as specified in
|
| 272 |
[[expr.new]].
|
| 273 |
- A deallocation function for a class is named by a *delete-expression*
|
| 274 |
if it is the selected usual deallocation function as specified in
|
| 275 |
[[expr.delete]] and [[class.free]].
|
| 276 |
|
| 277 |
-
A variable
|
| 278 |
-
|
|
|
|
| 279 |
|
| 280 |
- `x` is a reference that is usable in constant expressions
|
| 281 |
[[expr.const]], or
|
| 282 |
- `x` is a variable of non-reference type that is usable in constant
|
| 283 |
expressions and has no mutable subobjects, and E is an element of the
|
| 284 |
set of potential results of an expression of non-volatile-qualified
|
| 285 |
non-class type to which the lvalue-to-rvalue conversion [[conv.lval]]
|
| 286 |
is applied, or
|
| 287 |
- `x` is a variable of non-reference type, and E is an element of the
|
| 288 |
-
set of potential results of a discarded-value expression
|
| 289 |
-
to which the lvalue-to-rvalue conversion is not
|
|
|
|
| 290 |
|
| 291 |
A structured binding is odr-used if it appears as a
|
| 292 |
potentially-evaluated expression.
|
| 293 |
|
| 294 |
`*this` is odr-used if `this` appears as a potentially-evaluated
|
| 295 |
expression (including as the result of the implicit transformation in
|
| 296 |
-
the body of a non-static member function
|
| 297 |
|
| 298 |
A virtual member function is odr-used if it is not pure. A function is
|
| 299 |
odr-used if it is named by a potentially-evaluated expression or
|
| 300 |
conversion. A non-placement allocation or deallocation function for a
|
| 301 |
class is odr-used by the definition of a constructor of that class. A
|
|
@@ -308,29 +339,29 @@ An assignment operator function in a class is odr-used by an
|
|
| 308 |
implicitly-defined copy-assignment or move-assignment function for
|
| 309 |
another class as specified in [[class.copy.assign]]. A constructor for
|
| 310 |
a class is odr-used as specified in [[dcl.init]]. A destructor for a
|
| 311 |
class is odr-used if it is potentially invoked [[class.dtor]].
|
| 312 |
|
| 313 |
-
A local entity [[basic.pre]] is *odr-usable* in a
|
| 314 |
-
[[basic.scope.
|
| 315 |
|
| 316 |
- either the local entity is not `*this`, or an enclosing class or
|
| 317 |
non-lambda function parameter scope exists and, if the innermost such
|
| 318 |
scope is a function parameter scope, it corresponds to a non-static
|
| 319 |
member function, and
|
| 320 |
-
- for each intervening
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
- the intervening
|
| 325 |
-
- the intervening
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
*lambda-expression* is also an intervening
|
| 329 |
|
| 330 |
-
If a local entity is odr-used in a
|
| 331 |
-
|
| 332 |
|
| 333 |
[*Example 2*:
|
| 334 |
|
| 335 |
``` cpp
|
| 336 |
void f(int n) {
|
|
@@ -345,15 +376,15 @@ void f(int n) {
|
|
| 345 |
}
|
| 346 |
```
|
| 347 |
|
| 348 |
— *end example*]
|
| 349 |
|
| 350 |
-
Every program shall contain
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
(see [[class.default.ctor]], [[class.copy.ctor]], [[class.dtor]], and
|
| 356 |
[[class.copy.assign]]).
|
| 357 |
|
| 358 |
[*Example 3*:
|
| 359 |
|
|
@@ -377,13 +408,12 @@ A *definition domain* is a *private-module-fragment* or the portion of a
|
|
| 377 |
translation unit excluding its *private-module-fragment* (if any). A
|
| 378 |
definition of an inline function or variable shall be reachable from the
|
| 379 |
end of every definition domain in which it is odr-used outside of a
|
| 380 |
discarded statement.
|
| 381 |
|
| 382 |
-
A definition of a class
|
| 383 |
-
|
| 384 |
-
complete.
|
| 385 |
|
| 386 |
[*Example 4*:
|
| 387 |
|
| 388 |
The following complete translation unit is well-formed, even though it
|
| 389 |
never defines `X`:
|
|
@@ -407,12 +437,13 @@ complete class types are required. A class type `T` must be complete if:
|
|
| 407 |
- `T` is used as the allocated type or array element type in a
|
| 408 |
*new-expression* [[expr.new]], or
|
| 409 |
- an lvalue-to-rvalue conversion is applied to a glvalue referring to an
|
| 410 |
object of type `T` [[conv.lval]], or
|
| 411 |
- an expression is converted (either implicitly or explicitly) to type
|
| 412 |
-
`T`
|
| 413 |
-
[[expr.
|
|
|
|
| 414 |
- an expression that is not a null pointer constant, and has type other
|
| 415 |
than cv `void*`, is converted to the type pointer to `T` or reference
|
| 416 |
to `T` using a standard conversion [[conv]], a `dynamic_cast`
|
| 417 |
[[expr.dynamic.cast]] or a `static_cast` [[expr.static.cast]], or
|
| 418 |
- a class member access operator is applied to an expression of type `T`
|
|
@@ -428,26 +459,23 @@ complete class types are required. A class type `T` must be complete if:
|
|
| 428 |
- an *exception-declaration* has type `T`, reference to `T`, or pointer
|
| 429 |
to `T` [[except.handle]].
|
| 430 |
|
| 431 |
— *end note*]
|
| 432 |
|
| 433 |
-
|
|
|
|
| 434 |
|
| 435 |
-
-
|
| 436 |
-
-
|
| 437 |
-
|
| 438 |
-
- templated entity [[temp.pre]],
|
| 439 |
-
- default argument for a parameter (for a function in a given scope)
|
| 440 |
-
[[dcl.fct.default]], or
|
| 441 |
-
- default template argument [[temp.param]]
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
all definitions of `D`, or, if `D` is an unnamed enumeration,
|
| 447 |
-
definitions of `D` that are reachable at any given program
|
| 448 |
-
following requirements shall be satisfied.
|
| 449 |
|
| 450 |
- Each such definition shall not be attached to a named module
|
| 451 |
[[module.unit]].
|
| 452 |
- Each such definition shall consist of the same sequence of tokens,
|
| 453 |
where the definition of a closure type is considered to consist of the
|
|
@@ -470,22 +498,28 @@ following requirements shall be satisfied.
|
|
| 470 |
- In each such definition, except within the default arguments and
|
| 471 |
default template arguments of `D`, corresponding *lambda-expression*s
|
| 472 |
shall have the same closure type (see below).
|
| 473 |
- In each such definition, corresponding entities shall have the same
|
| 474 |
language linkage.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
- In each such definition, the overloaded operators referred to, the
|
| 476 |
implicit calls to conversion functions, constructors, operator new
|
| 477 |
functions and operator delete functions, shall refer to the same
|
| 478 |
function.
|
| 479 |
- In each such definition, a default argument used by an (implicit or
|
| 480 |
explicit) function call or a default template argument used by an
|
| 481 |
(implicit or explicit) *template-id* or *simple-template-id* is
|
| 482 |
treated as if its token sequence were present in the definition of
|
| 483 |
`D`; that is, the default argument or default template argument is
|
| 484 |
subject to the requirements described in this paragraph (recursively).
|
| 485 |
-
- If `D` is a class with an implicitly-declared constructor
|
| 486 |
-
[[class.default.ctor]], [[class.copy.ctor]]
|
| 487 |
constructor was implicitly defined in every translation unit where it
|
| 488 |
is odr-used, and the implicit definition in every translation unit
|
| 489 |
shall call the same constructor for a subobject of `D`.
|
| 490 |
\[*Example 5*:
|
| 491 |
``` cpp
|
|
@@ -520,33 +554,27 @@ following requirements shall be satisfied.
|
|
| 520 |
implicit definition in every translation unit shall call the same
|
| 521 |
comparison operators for each subobject of `D`.
|
| 522 |
|
| 523 |
If `D` is a template and is defined in more than one translation unit,
|
| 524 |
then the preceding requirements shall apply both to names from the
|
| 525 |
-
template’s enclosing scope used in the template definition
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
entities.
|
| 535 |
|
| 536 |
[*Note 4*: The entity is still declared in multiple translation units,
|
| 537 |
and [[basic.link]] still applies to these declarations. In particular,
|
| 538 |
*lambda-expression*s [[expr.prim.lambda]] appearing in the type of `D`
|
| 539 |
-
|
| 540 |
-
*lambda-expression*s appearing in a default argument of `D`
|
| 541 |
denote different types in different translation units. — *end note*]
|
| 542 |
|
| 543 |
-
If these definitions do not satisfy these requirements, then the program
|
| 544 |
-
is ill-formed; a diagnostic is required only if the entity is attached
|
| 545 |
-
to a named module and a prior definition is reachable at the point where
|
| 546 |
-
a later definition occurs.
|
| 547 |
-
|
| 548 |
[*Example 6*:
|
| 549 |
|
| 550 |
``` cpp
|
| 551 |
inline void f(bool cond, void (*p)()) {
|
| 552 |
if (cond) f(false, []{});
|
|
@@ -579,92 +607,209 @@ first enumerator name and do not have typedef names for linkage purposes
|
|
| 579 |
[[dcl.enum]], those unnamed enumeration types shall be the same; no
|
| 580 |
diagnostic required.
|
| 581 |
|
| 582 |
## Scope <a id="basic.scope">[[basic.scope]]</a>
|
| 583 |
|
| 584 |
-
###
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
scope*
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
|
| 599 |
[*Example 1*:
|
| 600 |
|
| 601 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
|
| 603 |
``` cpp
|
| 604 |
-
int
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 608 |
}
|
| 609 |
```
|
| 610 |
|
| 611 |
-
the
|
| 612 |
-
|
| 613 |
-
potential scope of the first `j` begins immediately after that `j` and
|
| 614 |
-
extends to the end of the program, but its (actual) scope excludes the
|
| 615 |
-
text between the `,` and the `}`. The declarative region of the second
|
| 616 |
-
declaration of `j` (the `j` immediately before the semicolon) includes
|
| 617 |
-
all the text between `{` and `}`, but its potential scope excludes the
|
| 618 |
-
declaration of `i`. The scope of the second declaration of `j` is the
|
| 619 |
-
same as its potential scope.
|
| 620 |
|
| 621 |
— *end example*]
|
| 622 |
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
[[namespace.udir]] alter this general behavior.
|
| 628 |
-
|
| 629 |
-
Given a set of declarations in a single declarative region, each of
|
| 630 |
-
which specifies the same unqualified name,
|
| 631 |
-
|
| 632 |
-
- they shall all refer to the same entity, or all refer to functions and
|
| 633 |
-
function templates; or
|
| 634 |
-
- exactly one declaration shall declare a class name or enumeration name
|
| 635 |
-
that is not a typedef name and the other declarations shall all refer
|
| 636 |
-
to the same variable, non-static data member, or enumerator, or all
|
| 637 |
-
refer to functions and function templates; in this case the class name
|
| 638 |
-
or enumeration name is hidden [[basic.scope.hiding]]. \[*Note 1*: A
|
| 639 |
-
structured binding [[dcl.struct.bind]], namespace name
|
| 640 |
-
[[basic.namespace]], or class template name [[temp.pre]] must be
|
| 641 |
-
unique in its declarative region. — *end note*]
|
| 642 |
-
|
| 643 |
-
[*Note 2*: These restrictions apply to the declarative region into
|
| 644 |
-
which a name is introduced, which is not necessarily the same as the
|
| 645 |
-
region in which the declaration occurs. In particular,
|
| 646 |
-
*elaborated-type-specifier*s [[dcl.type.elab]] and friend declarations
|
| 647 |
-
[[class.friend]] may introduce a (possibly not visible) name into an
|
| 648 |
-
enclosing namespace; these restrictions apply to that region. Local
|
| 649 |
-
extern declarations [[basic.link]] may introduce a name into the
|
| 650 |
-
declarative region where the declaration appears and also introduce a
|
| 651 |
-
(possibly not visible) name into an enclosing namespace; these
|
| 652 |
-
restrictions apply to both regions. — *end note*]
|
| 653 |
-
|
| 654 |
-
For a given declarative region *R* and a point *P* outside *R*, the set
|
| 655 |
-
of *intervening* declarative regions between *P* and *R* comprises all
|
| 656 |
-
declarative regions that are or enclose *R* and do not enclose *P*.
|
| 657 |
-
|
| 658 |
-
[*Note 3*: The name lookup rules are summarized in
|
| 659 |
-
[[basic.lookup]]. — *end note*]
|
| 660 |
|
| 661 |
### Point of declaration <a id="basic.scope.pdecl">[[basic.scope.pdecl]]</a>
|
| 662 |
|
| 663 |
-
The *
|
| 664 |
-
declarator [[dcl.decl]]
|
| 665 |
-
noted below.
|
| 666 |
|
| 667 |
[*Example 1*:
|
| 668 |
|
| 669 |
``` cpp
|
| 670 |
unsigned char x = 12;
|
|
@@ -677,12 +822,12 @@ because the initializer accesses the second `x` outside its lifetime
|
|
| 677 |
|
| 678 |
— *end example*]
|
| 679 |
|
| 680 |
[*Note 1*:
|
| 681 |
|
| 682 |
-
A name from an outer scope remains visible up to the
|
| 683 |
-
declaration
|
| 684 |
|
| 685 |
[*Example 2*:
|
| 686 |
|
| 687 |
``` cpp
|
| 688 |
const int i = 2;
|
|
@@ -693,25 +838,21 @@ declares a block-scope array of two integers.
|
|
| 693 |
|
| 694 |
— *end example*]
|
| 695 |
|
| 696 |
— *end note*]
|
| 697 |
|
| 698 |
-
The
|
| 699 |
-
|
| 700 |
-
*
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
point of declaration of an alias or alias template immediately follows
|
| 705 |
-
the *defining-type-id* to which the alias refers.
|
| 706 |
|
| 707 |
-
The
|
| 708 |
-
|
| 709 |
-
[[namespace.udecl]].
|
| 710 |
|
| 711 |
-
The
|
| 712 |
-
*enumerator-definition*.
|
| 713 |
|
| 714 |
[*Example 3*:
|
| 715 |
|
| 716 |
``` cpp
|
| 717 |
const int x = 12;
|
|
@@ -721,113 +862,125 @@ const int x = 12;
|
|
| 721 |
Here, the enumerator `x` is initialized with the value of the constant
|
| 722 |
`x`, namely 12.
|
| 723 |
|
| 724 |
— *end example*]
|
| 725 |
|
| 726 |
-
After the point of declaration of a class member, the member name can be
|
| 727 |
-
looked up in the scope of its class.
|
| 728 |
-
|
| 729 |
[*Note 2*:
|
| 730 |
|
| 731 |
-
|
|
|
|
|
|
|
|
|
|
| 732 |
|
| 733 |
``` cpp
|
| 734 |
struct X {
|
| 735 |
enum E { z = 16 };
|
| 736 |
int b[X::z]; // OK
|
| 737 |
};
|
| 738 |
```
|
| 739 |
|
|
|
|
|
|
|
| 740 |
— *end note*]
|
| 741 |
|
| 742 |
-
The
|
| 743 |
-
|
| 744 |
|
| 745 |
-
|
| 746 |
-
``` bnf
|
| 747 |
-
class-key attribute-specifier-seqₒₚₜ identifier ';'
|
| 748 |
-
```
|
| 749 |
-
|
| 750 |
-
the *identifier* is declared to be a *class-name* in the scope that
|
| 751 |
-
contains the declaration, otherwise
|
| 752 |
-
- for an *elaborated-type-specifier* of the form
|
| 753 |
-
``` bnf
|
| 754 |
-
class-key identifier
|
| 755 |
-
```
|
| 756 |
-
|
| 757 |
-
if the *elaborated-type-specifier* is used in the *decl-specifier-seq*
|
| 758 |
-
or *parameter-declaration-clause* of a function defined in namespace
|
| 759 |
-
scope, the *identifier* is declared as a *class-name* in the namespace
|
| 760 |
-
that contains the declaration; otherwise, except as a friend
|
| 761 |
-
declaration, the *identifier* is declared in the smallest namespace or
|
| 762 |
-
block scope that contains the declaration.
|
| 763 |
-
\[*Note 3*: These rules also apply within templates. — *end note*]
|
| 764 |
-
\[*Note 4*: Other forms of *elaborated-type-specifier* do not declare
|
| 765 |
-
a new name, and therefore must refer to an existing *type-name*. See
|
| 766 |
-
[[basic.lookup.elab]] and [[dcl.type.elab]]. — *end note*]
|
| 767 |
-
|
| 768 |
-
The point of declaration for an injected-class-name [[class.pre]] is
|
| 769 |
immediately following the opening brace of the class definition.
|
| 770 |
|
| 771 |
-
The
|
| 772 |
-
[[dcl.fct.def.general]] is immediately before the
|
| 773 |
-
function definition.
|
| 774 |
|
| 775 |
-
The
|
| 776 |
-
immediately after the *identifier-list* of the structured binding
|
| 777 |
declaration.
|
| 778 |
|
| 779 |
-
The
|
| 780 |
-
declared in the *for-range-declaration* of a range-based `for` statement
|
| 781 |
[[stmt.ranged]] is immediately after the *for-range-initializer*.
|
| 782 |
|
| 783 |
-
The
|
| 784 |
-
its complete *template-parameter*.
|
| 785 |
|
| 786 |
-
[*Example
|
| 787 |
|
| 788 |
``` cpp
|
| 789 |
typedef unsigned char T;
|
| 790 |
template<class T
|
| 791 |
-
= T // lookup finds the typedef
|
| 792 |
, T // lookup finds the template parameter
|
| 793 |
N = 0> struct A { };
|
| 794 |
```
|
| 795 |
|
| 796 |
— *end example*]
|
| 797 |
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 805 |
|
| 806 |
[*Note 6*: For point of instantiation of a template, see
|
| 807 |
[[temp.point]]. — *end note*]
|
| 808 |
|
| 809 |
### Block scope <a id="basic.scope.block">[[basic.scope.block]]</a>
|
| 810 |
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
*handler*
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
statement, any of the outermost blocks) of the controlled statement.
|
| 826 |
|
| 827 |
[*Example 1*:
|
| 828 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 829 |
``` cpp
|
| 830 |
if (int x = f()) {
|
| 831 |
int x; // error: redeclaration of x
|
| 832 |
}
|
| 833 |
else {
|
|
@@ -837,364 +990,574 @@ else {
|
|
| 837 |
|
| 838 |
— *end example*]
|
| 839 |
|
| 840 |
### Function parameter scope <a id="basic.scope.param">[[basic.scope.param]]</a>
|
| 841 |
|
| 842 |
-
A
|
| 843 |
-
|
| 844 |
-
parameter scope*. The potential scope of a parameter or function-local
|
| 845 |
-
predefined variable begins at its point of declaration. If the nearest
|
| 846 |
-
enclosing function declarator is not the declarator of a function
|
| 847 |
-
definition, the potential scope ends at the end of that function
|
| 848 |
-
declarator. Otherwise, if the function has a *function-try-block* the
|
| 849 |
-
potential scope ends at the end of the last associated handler.
|
| 850 |
-
Otherwise the potential scope ends at the end of the outermost block of
|
| 851 |
-
the function definition. A parameter name shall not be redeclared in the
|
| 852 |
-
outermost block of the function definition nor in the outermost block of
|
| 853 |
-
any handler associated with a *function-try-block*.
|
| 854 |
|
| 855 |
-
|
|
|
|
| 856 |
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
scope
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
|
| 861 |
### Namespace scope <a id="basic.scope.namespace">[[basic.scope.namespace]]</a>
|
| 862 |
|
| 863 |
-
|
| 864 |
-
*namespace
|
| 865 |
-
*
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
*using-directive* [[namespace.udir]] that nominates the member’s
|
| 871 |
-
namespace, the member’s potential scope includes that portion of the
|
| 872 |
-
potential scope of the *using-directive* that follows the member’s point
|
| 873 |
-
of declaration.
|
| 874 |
|
| 875 |
[*Example 1*:
|
| 876 |
|
| 877 |
``` cpp
|
| 878 |
-
namespace
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
void q();
|
| 883 |
}
|
| 884 |
-
namespace { int l=1; }
|
| 885 |
-
// the potential scope of l is from its point of declaration to the end of the translation unit
|
| 886 |
-
|
| 887 |
-
namespace N {
|
| 888 |
-
int g(char a) { // overloads N::g(int)
|
| 889 |
-
return l+a; // l is from unnamed namespace
|
| 890 |
-
}
|
| 891 |
-
|
| 892 |
-
int i; // error: duplicate definition
|
| 893 |
-
int j(); // OK: duplicate function declaration
|
| 894 |
-
|
| 895 |
-
int j() { // OK: definition of N::j()
|
| 896 |
-
return g(i); // calls N::g(int)
|
| 897 |
-
}
|
| 898 |
-
int q(); // error: different return type
|
| 899 |
}
|
| 900 |
```
|
| 901 |
|
| 902 |
— *end example*]
|
| 903 |
|
| 904 |
-
If a translation unit Q is imported into a translation unit R
|
| 905 |
-
[[module.import]], the potential scope of a name X declared with
|
| 906 |
-
namespace scope in Q is extended to include the portion of the
|
| 907 |
-
corresponding namespace scope in R following the first
|
| 908 |
-
*module-import-declaration* or *module-declaration* in R that imports Q
|
| 909 |
-
(directly or indirectly) if
|
| 910 |
-
|
| 911 |
-
- X does not have internal linkage, and
|
| 912 |
-
- X is declared after the *module-declaration* in Q (if any), and
|
| 913 |
-
- either X is exported or Q and R are part of the same module.
|
| 914 |
-
|
| 915 |
-
[*Note 1*:
|
| 916 |
-
|
| 917 |
-
A *module-import-declaration* imports both the named translation unit(s)
|
| 918 |
-
and any modules named by exported *module-import-declaration*s within
|
| 919 |
-
them, recursively.
|
| 920 |
-
|
| 921 |
-
[*Example 2*:
|
| 922 |
-
|
| 923 |
-
Translation unit #1
|
| 924 |
-
|
| 925 |
-
``` cpp
|
| 926 |
-
export module Q;
|
| 927 |
-
export int sq(int i) { return i*i; }
|
| 928 |
-
```
|
| 929 |
-
|
| 930 |
-
Translation unit #2
|
| 931 |
-
|
| 932 |
-
``` cpp
|
| 933 |
-
export module R;
|
| 934 |
-
export import Q;
|
| 935 |
-
```
|
| 936 |
-
|
| 937 |
-
Translation unit #3
|
| 938 |
-
|
| 939 |
-
``` cpp
|
| 940 |
-
import R;
|
| 941 |
-
int main() { return sq(9); } // OK: sq from module Q
|
| 942 |
-
```
|
| 943 |
-
|
| 944 |
-
— *end example*]
|
| 945 |
-
|
| 946 |
-
— *end note*]
|
| 947 |
-
|
| 948 |
-
A namespace member can also be referred to after the `::` scope
|
| 949 |
-
resolution operator [[expr.prim.id.qual]] applied to the name of its
|
| 950 |
-
namespace or the name of a namespace which nominates the member’s
|
| 951 |
-
namespace in a *using-directive*; see [[namespace.qual]].
|
| 952 |
-
|
| 953 |
-
The outermost declarative region of a translation unit is also a
|
| 954 |
-
namespace, called the *global namespace*. A name declared in the global
|
| 955 |
-
namespace has *global namespace scope* (also called *global scope*). The
|
| 956 |
-
potential scope of such a name begins at its point of declaration
|
| 957 |
-
[[basic.scope.pdecl]] and ends at the end of the translation unit that
|
| 958 |
-
is its declarative region. A name with global namespace scope is said to
|
| 959 |
-
be a *global name*.
|
| 960 |
-
|
| 961 |
### Class scope <a id="basic.scope.class">[[basic.scope.class]]</a>
|
| 962 |
|
| 963 |
-
|
| 964 |
-
|
| 965 |
-
|
|
|
|
|
|
|
|
|
|
| 966 |
|
| 967 |
-
|
| 968 |
-
its context and when re-evaluated in the completed scope of `S`. No
|
| 969 |
-
diagnostic is required for a violation of this rule.
|
| 970 |
|
| 971 |
-
|
| 972 |
-
|
| 973 |
-
class.
|
| 974 |
-
|
| 975 |
-
The potential scope of a declaration in a class that extends to or past
|
| 976 |
-
the end of a class definition also extends to the regions defined by its
|
| 977 |
-
member definitions, even if the members are defined lexically outside
|
| 978 |
-
the class (this includes static data member definitions, nested class
|
| 979 |
-
definitions, and member function definitions, including the member
|
| 980 |
-
function body and any portion of the declarator part of such definitions
|
| 981 |
-
which follows the *declarator-id*, including a
|
| 982 |
-
*parameter-declaration-clause* and any default arguments
|
| 983 |
-
[[dcl.fct.default]]).
|
| 984 |
|
| 985 |
[*Example 1*:
|
| 986 |
|
| 987 |
``` cpp
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
class X {
|
| 992 |
-
char v[i]; // error: i refers to ::i but when reevaluated is X::i
|
| 993 |
-
int f() { return sizeof(c); } // OK: X::c
|
| 994 |
-
char c;
|
| 995 |
-
enum { i = 2 };
|
| 996 |
-
};
|
| 997 |
-
|
| 998 |
-
typedef char* T;
|
| 999 |
-
struct Y {
|
| 1000 |
-
T a; // error: T refers to ::T but when reevaluated is Y::T
|
| 1001 |
-
typedef long T;
|
| 1002 |
-
T b;
|
| 1003 |
};
|
| 1004 |
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
typedef I I; // error, even though no reordering involved
|
| 1008 |
-
};
|
| 1009 |
```
|
| 1010 |
|
| 1011 |
— *end example*]
|
| 1012 |
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
- in the scope of its class (as described above) or a class derived
|
| 1016 |
-
[[class.derived]] from its class,
|
| 1017 |
-
- after the `.` operator applied to an expression of the type of its
|
| 1018 |
-
class [[expr.ref]] or a class derived from its class,
|
| 1019 |
-
- after the `->` operator applied to a pointer to an object of its class
|
| 1020 |
-
[[expr.ref]] or a class derived from its class,
|
| 1021 |
-
- after the `::` scope resolution operator [[expr.prim.id.qual]] applied
|
| 1022 |
-
to the name of its class or a class derived from its class.
|
| 1023 |
|
| 1024 |
### Enumeration scope <a id="basic.scope.enum">[[basic.scope.enum]]</a>
|
| 1025 |
|
| 1026 |
-
|
| 1027 |
-
|
| 1028 |
-
|
| 1029 |
|
| 1030 |
### Template parameter scope <a id="basic.scope.temp">[[basic.scope.temp]]</a>
|
| 1031 |
|
| 1032 |
-
|
| 1033 |
-
*template
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
|
| 1037 |
-
|
| 1038 |
-
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
| 1042 |
-
|
| 1043 |
-
|
| 1044 |
-
[*
|
| 1045 |
-
|
| 1046 |
-
|
| 1047 |
-
namespace N {
|
| 1048 |
-
template<class T> struct A { }; // #1
|
| 1049 |
-
template<class U> void f(U) { } // #2
|
| 1050 |
-
struct B {
|
| 1051 |
-
template<class V> friend int g(struct C*); // #3
|
| 1052 |
-
};
|
| 1053 |
-
}
|
| 1054 |
-
```
|
| 1055 |
-
|
| 1056 |
-
The declarative regions of `T`, `U` and `V` are the
|
| 1057 |
-
*template-declaration*s on lines \#1, \#2, and \#3, respectively. But
|
| 1058 |
-
the names `A`, `f`, `g` and `C` all belong to the same declarative
|
| 1059 |
-
region — namely, the *namespace-body* of `N`. (`g` is still considered
|
| 1060 |
-
to belong to this declarative region in spite of its being hidden during
|
| 1061 |
-
qualified and unqualified name lookup.)
|
| 1062 |
-
|
| 1063 |
-
— *end example*]
|
| 1064 |
-
|
| 1065 |
-
The potential scope of a template parameter name begins at its point of
|
| 1066 |
-
declaration [[basic.scope.pdecl]] and ends at the end of its declarative
|
| 1067 |
-
region.
|
| 1068 |
-
|
| 1069 |
-
[*Note 1*:
|
| 1070 |
-
|
| 1071 |
-
This implies that a *template-parameter* can be used in the declaration
|
| 1072 |
-
of subsequent *template-parameter*s and their default arguments but
|
| 1073 |
-
cannot be used in preceding *template-parameter*s or their default
|
| 1074 |
-
arguments. For example,
|
| 1075 |
-
|
| 1076 |
-
``` cpp
|
| 1077 |
-
template<class T, T* p, class U = T> class X { ... };
|
| 1078 |
-
template<class T> void f(T* p = new T);
|
| 1079 |
-
```
|
| 1080 |
-
|
| 1081 |
-
This also implies that a *template-parameter* can be used in the
|
| 1082 |
-
specification of base classes. For example,
|
| 1083 |
-
|
| 1084 |
-
``` cpp
|
| 1085 |
-
template<class T> class X : public Array<T> { ... };
|
| 1086 |
-
template<class T> class Y : public T { ... };
|
| 1087 |
-
```
|
| 1088 |
-
|
| 1089 |
-
The use of a template parameter as a base class implies that a class
|
| 1090 |
-
used as a template argument must be defined and not just declared when
|
| 1091 |
-
the class template is instantiated.
|
| 1092 |
-
|
| 1093 |
-
— *end note*]
|
| 1094 |
-
|
| 1095 |
-
The declarative region of the name of a template parameter is nested
|
| 1096 |
-
within the immediately-enclosing declarative region.
|
| 1097 |
-
|
| 1098 |
-
[*Note 2*:
|
| 1099 |
-
|
| 1100 |
-
As a result, a *template-parameter* hides any entity with the same name
|
| 1101 |
-
in an enclosing scope [[basic.scope.hiding]].
|
| 1102 |
-
|
| 1103 |
-
[*Example 2*:
|
| 1104 |
-
|
| 1105 |
-
``` cpp
|
| 1106 |
-
typedef int N;
|
| 1107 |
-
template<N X, typename N, template<N Y> class T> struct A;
|
| 1108 |
-
```
|
| 1109 |
-
|
| 1110 |
-
Here, `X` is a non-type template parameter of type `int` and `Y` is a
|
| 1111 |
-
non-type template parameter of the same type as the second template
|
| 1112 |
-
parameter of `A`.
|
| 1113 |
-
|
| 1114 |
-
— *end example*]
|
| 1115 |
-
|
| 1116 |
-
— *end note*]
|
| 1117 |
-
|
| 1118 |
-
[*Note 3*: Because the name of a template parameter cannot be
|
| 1119 |
-
redeclared within its potential scope [[temp.local]], a template
|
| 1120 |
-
parameter’s scope is often its potential scope. However, it is still
|
| 1121 |
-
possible for a template parameter name to be hidden; see
|
| 1122 |
-
[[temp.local]]. — *end note*]
|
| 1123 |
-
|
| 1124 |
-
### Name hiding <a id="basic.scope.hiding">[[basic.scope.hiding]]</a>
|
| 1125 |
-
|
| 1126 |
-
A declaration of a name in a nested declarative region hides a
|
| 1127 |
-
declaration of the same name in an enclosing declarative region; see
|
| 1128 |
-
[[basic.scope.declarative]] and [[basic.lookup.unqual]].
|
| 1129 |
-
|
| 1130 |
-
If a class name [[class.name]] or enumeration name [[dcl.enum]] and a
|
| 1131 |
-
variable, data member, function, or enumerator are declared in the same
|
| 1132 |
-
declarative region (in any order) with the same name (excluding
|
| 1133 |
-
declarations made visible via *using-directive*s
|
| 1134 |
-
[[basic.lookup.unqual]]), the class or enumeration name is hidden
|
| 1135 |
-
wherever the variable, data member, function, or enumerator name is
|
| 1136 |
-
visible.
|
| 1137 |
-
|
| 1138 |
-
In a member function definition, the declaration of a name at block
|
| 1139 |
-
scope hides the declaration of a member of the class with the same name;
|
| 1140 |
-
see [[basic.scope.class]]. The declaration of a member in a derived
|
| 1141 |
-
class [[class.derived]] hides the declaration of a member of a base
|
| 1142 |
-
class of the same name; see [[class.member.lookup]].
|
| 1143 |
-
|
| 1144 |
-
During the lookup of a name qualified by a namespace name, declarations
|
| 1145 |
-
that would otherwise be made visible by a *using-directive* can be
|
| 1146 |
-
hidden by declarations with the same name in the namespace containing
|
| 1147 |
-
the *using-directive*; see [[namespace.qual]].
|
| 1148 |
-
|
| 1149 |
-
If a name is in scope and is not hidden it is said to be *visible*.
|
| 1150 |
|
| 1151 |
## Name lookup <a id="basic.lookup">[[basic.lookup]]</a>
|
| 1152 |
|
|
|
|
|
|
|
| 1153 |
The name lookup rules apply uniformly to all names (including
|
| 1154 |
*typedef-name*s [[dcl.typedef]], *namespace-name*s [[basic.namespace]],
|
| 1155 |
and *class-name*s [[class.name]]) wherever the grammar allows such names
|
| 1156 |
in the context discussed by a particular rule. Name lookup associates
|
| 1157 |
the use of a name with a set of declarations [[basic.def]] of that name.
|
| 1158 |
-
|
| 1159 |
-
|
| 1160 |
-
|
| 1161 |
-
|
| 1162 |
-
|
| 1163 |
-
|
| 1164 |
-
|
| 1165 |
-
|
| 1166 |
-
have succeeded
|
| 1167 |
-
|
| 1168 |
-
|
| 1169 |
-
|
| 1170 |
-
|
| 1171 |
-
|
| 1172 |
-
|
| 1173 |
-
|
| 1174 |
-
|
| 1175 |
-
|
| 1176 |
-
|
| 1177 |
-
|
| 1178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1179 |
|
| 1180 |
### Unqualified name lookup <a id="basic.lookup.unqual">[[basic.lookup.unqual]]</a>
|
| 1181 |
|
| 1182 |
-
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
| 1186 |
-
|
| 1187 |
-
|
| 1188 |
-
|
| 1189 |
-
|
| 1190 |
-
|
| 1191 |
-
namespace nominated by
|
| 1192 |
-
|
| 1193 |
-
|
| 1194 |
-
|
| 1195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1196 |
|
| 1197 |
[*Note 1*:
|
| 1198 |
|
| 1199 |
For purposes of determining (during parsing) whether an expression is a
|
| 1200 |
*postfix-expression* for a function call, the usual name lookup rules
|
|
@@ -1210,17 +1573,17 @@ namespace N {
|
|
| 1210 |
template <class T> int f(T);
|
| 1211 |
template <class T> int g(T);
|
| 1212 |
template <class T> int h(T);
|
| 1213 |
}
|
| 1214 |
|
| 1215 |
-
int x = f<N::A>(N::A()); // OK
|
| 1216 |
-
int y = g<N::A>(N::A()); // OK
|
| 1217 |
int z = h<N::A>(N::A()); // error: h< does not begin a template-id
|
| 1218 |
```
|
| 1219 |
|
| 1220 |
-
The rules
|
| 1221 |
-
|
| 1222 |
|
| 1223 |
``` cpp
|
| 1224 |
typedef int f;
|
| 1225 |
namespace N {
|
| 1226 |
struct A {
|
|
@@ -1231,398 +1594,79 @@ namespace N {
|
|
| 1231 |
}
|
| 1232 |
};
|
| 1233 |
}
|
| 1234 |
```
|
| 1235 |
|
| 1236 |
-
Because the expression is not a function call,
|
| 1237 |
-
|
| 1238 |
-
function `f` is not found.
|
| 1239 |
|
| 1240 |
— *end note*]
|
| 1241 |
|
| 1242 |
-
A name used in global scope, outside of any function, class or
|
| 1243 |
-
user-declared namespace, shall be declared before its use in global
|
| 1244 |
-
scope.
|
| 1245 |
-
|
| 1246 |
-
A name used in a user-declared namespace outside of the definition of
|
| 1247 |
-
any function or class shall be declared before its use in that namespace
|
| 1248 |
-
or before its use in a namespace enclosing its namespace.
|
| 1249 |
-
|
| 1250 |
-
In the definition of a function that is a member of namespace `N`, a
|
| 1251 |
-
name used after the function’s *declarator-id*[^3] shall be declared
|
| 1252 |
-
before its use in the block in which it is used or in one of its
|
| 1253 |
-
enclosing blocks [[stmt.block]] or shall be declared before its use in
|
| 1254 |
-
namespace `N` or, if `N` is a nested namespace, shall be declared before
|
| 1255 |
-
its use in one of `N`’s enclosing namespaces.
|
| 1256 |
-
|
| 1257 |
-
[*Example 1*:
|
| 1258 |
-
|
| 1259 |
-
``` cpp
|
| 1260 |
-
namespace A {
|
| 1261 |
-
namespace N {
|
| 1262 |
-
void f();
|
| 1263 |
-
}
|
| 1264 |
-
}
|
| 1265 |
-
void A::N::f() {
|
| 1266 |
-
i = 5;
|
| 1267 |
-
// The following scopes are searched for a declaration of i:
|
| 1268 |
-
// 1) outermost block scope of A::N::f, before the use of i
|
| 1269 |
-
// 2) scope of namespace N
|
| 1270 |
-
// 3) scope of namespace A
|
| 1271 |
-
// 4) global scope, before the definition of A::N::f
|
| 1272 |
-
}
|
| 1273 |
-
```
|
| 1274 |
-
|
| 1275 |
-
— *end example*]
|
| 1276 |
-
|
| 1277 |
-
A name used in the definition of a class `X` [^4] outside of a
|
| 1278 |
-
complete-class context [[class.mem]] of `X` shall be declared in one of
|
| 1279 |
-
the following ways:
|
| 1280 |
-
|
| 1281 |
-
- before its use in class `X` or be a member of a base class of `X`
|
| 1282 |
-
[[class.member.lookup]], or
|
| 1283 |
-
- if `X` is a nested class of class `Y` [[class.nest]], before the
|
| 1284 |
-
definition of `X` in `Y`, or shall be a member of a base class of `Y`
|
| 1285 |
-
(this lookup applies in turn to `Y`’s enclosing classes, starting with
|
| 1286 |
-
the innermost enclosing class),[^5] or
|
| 1287 |
-
- if `X` is a local class [[class.local]] or is a nested class of a
|
| 1288 |
-
local class, before the definition of class `X` in a block enclosing
|
| 1289 |
-
the definition of class `X`, or
|
| 1290 |
-
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 1291 |
-
that is a member of `N`, or is a local class or a nested class within
|
| 1292 |
-
a local class of a function that is a member of `N`, before the
|
| 1293 |
-
definition of class `X` in namespace `N` or in one of `N`’s enclosing
|
| 1294 |
-
namespaces.
|
| 1295 |
-
|
| 1296 |
-
[*Example 2*:
|
| 1297 |
-
|
| 1298 |
-
``` cpp
|
| 1299 |
-
namespace M {
|
| 1300 |
-
class B { };
|
| 1301 |
-
}
|
| 1302 |
-
```
|
| 1303 |
-
|
| 1304 |
-
``` cpp
|
| 1305 |
-
namespace N {
|
| 1306 |
-
class Y : public M::B {
|
| 1307 |
-
class X {
|
| 1308 |
-
int a[i];
|
| 1309 |
-
};
|
| 1310 |
-
};
|
| 1311 |
-
}
|
| 1312 |
-
|
| 1313 |
-
// The following scopes are searched for a declaration of i:
|
| 1314 |
-
// 1) scope of class N::Y::X, before the use of i
|
| 1315 |
-
// 2) scope of class N::Y, before the definition of N::Y::X
|
| 1316 |
-
// 3) scope of N::Y's base class M::B
|
| 1317 |
-
// 4) scope of namespace N, before the definition of N::Y
|
| 1318 |
-
// 5) global scope, before the definition of N
|
| 1319 |
-
```
|
| 1320 |
-
|
| 1321 |
-
— *end example*]
|
| 1322 |
-
|
| 1323 |
-
[*Note 2*: When looking for a prior declaration of a class or function
|
| 1324 |
-
introduced by a friend declaration, scopes outside of the innermost
|
| 1325 |
-
enclosing namespace scope are not considered; see
|
| 1326 |
-
[[namespace.memdef]]. — *end note*]
|
| 1327 |
-
|
| 1328 |
-
[*Note 3*: [[basic.scope.class]] further describes the restrictions on
|
| 1329 |
-
the use of names in a class definition. [[class.nest]] further describes
|
| 1330 |
-
the restrictions on the use of names in nested class definitions.
|
| 1331 |
-
[[class.local]] further describes the restrictions on the use of names
|
| 1332 |
-
in local class definitions. — *end note*]
|
| 1333 |
-
|
| 1334 |
-
For the members of a class `X`, a name used in a complete-class context
|
| 1335 |
-
[[class.mem]] of `X` or in the definition of a class member outside of
|
| 1336 |
-
the definition of `X`, following the member’s *declarator-id*[^6], shall
|
| 1337 |
-
be declared in one of the following ways:
|
| 1338 |
-
|
| 1339 |
-
- before its use in the block in which it is used or in an enclosing
|
| 1340 |
-
block [[stmt.block]], or
|
| 1341 |
-
- shall be a member of class `X` or be a member of a base class of `X`
|
| 1342 |
-
[[class.member.lookup]], or
|
| 1343 |
-
- if `X` is a nested class of class `Y` [[class.nest]], shall be a
|
| 1344 |
-
member of `Y`, or shall be a member of a base class of `Y` (this
|
| 1345 |
-
lookup applies in turn to `Y`’s enclosing classes, starting with the
|
| 1346 |
-
innermost enclosing class),[^7] or
|
| 1347 |
-
- if `X` is a local class [[class.local]] or is a nested class of a
|
| 1348 |
-
local class, before the definition of class `X` in a block enclosing
|
| 1349 |
-
the definition of class `X`, or
|
| 1350 |
-
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 1351 |
-
that is a member of `N`, or is a local class or a nested class within
|
| 1352 |
-
a local class of a function that is a member of `N`, before the use of
|
| 1353 |
-
the name, in namespace `N` or in one of `N`’s enclosing namespaces.
|
| 1354 |
-
|
| 1355 |
-
[*Example 3*:
|
| 1356 |
-
|
| 1357 |
-
``` cpp
|
| 1358 |
-
class B { };
|
| 1359 |
-
namespace M {
|
| 1360 |
-
namespace N {
|
| 1361 |
-
class X : public B {
|
| 1362 |
-
void f();
|
| 1363 |
-
};
|
| 1364 |
-
}
|
| 1365 |
-
}
|
| 1366 |
-
void M::N::X::f() {
|
| 1367 |
-
i = 16;
|
| 1368 |
-
}
|
| 1369 |
-
|
| 1370 |
-
// The following scopes are searched for a declaration of i:
|
| 1371 |
-
// 1) outermost block scope of M::N::X::f, before the use of i
|
| 1372 |
-
// 2) scope of class M::N::X
|
| 1373 |
-
// 3) scope of M::N::X's base class B
|
| 1374 |
-
// 4) scope of namespace M::N
|
| 1375 |
-
// 5) scope of namespace M
|
| 1376 |
-
// 6) global scope, before the definition of M::N::X::f
|
| 1377 |
-
```
|
| 1378 |
-
|
| 1379 |
-
— *end example*]
|
| 1380 |
-
|
| 1381 |
-
[*Note 4*: [[class.mfct]] and [[class.static]] further describe the
|
| 1382 |
-
restrictions on the use of names in member function definitions.
|
| 1383 |
-
[[class.nest]] further describes the restrictions on the use of names in
|
| 1384 |
-
the scope of nested classes. [[class.local]] further describes the
|
| 1385 |
-
restrictions on the use of names in local class
|
| 1386 |
-
definitions. — *end note*]
|
| 1387 |
-
|
| 1388 |
-
Name lookup for a name used in the definition of a friend function
|
| 1389 |
-
[[class.friend]] defined inline in the class granting friendship shall
|
| 1390 |
-
proceed as described for lookup in member function definitions. If the
|
| 1391 |
-
friend function is not defined in the class granting friendship, name
|
| 1392 |
-
lookup in the friend function definition shall proceed as described for
|
| 1393 |
-
lookup in namespace member function definitions.
|
| 1394 |
-
|
| 1395 |
-
In a friend declaration naming a member function, a name used in the
|
| 1396 |
-
function declarator and not part of a *template-argument* in the
|
| 1397 |
-
*declarator-id* is first looked up in the scope of the member function’s
|
| 1398 |
-
class [[class.member.lookup]]. If it is not found, or if the name is
|
| 1399 |
-
part of a *template-argument* in the *declarator-id*, the look up is as
|
| 1400 |
-
described for unqualified names in the definition of the class granting
|
| 1401 |
-
friendship.
|
| 1402 |
-
|
| 1403 |
-
[*Example 4*:
|
| 1404 |
-
|
| 1405 |
-
``` cpp
|
| 1406 |
-
struct A {
|
| 1407 |
-
typedef int AT;
|
| 1408 |
-
void f1(AT);
|
| 1409 |
-
void f2(float);
|
| 1410 |
-
template <class T> void f3();
|
| 1411 |
-
};
|
| 1412 |
-
struct B {
|
| 1413 |
-
typedef char AT;
|
| 1414 |
-
typedef float BT;
|
| 1415 |
-
friend void A::f1(AT); // parameter type is A::AT
|
| 1416 |
-
friend void A::f2(BT); // parameter type is B::BT
|
| 1417 |
-
friend void A::f3<AT>(); // template argument is B::AT
|
| 1418 |
-
};
|
| 1419 |
-
```
|
| 1420 |
-
|
| 1421 |
-
— *end example*]
|
| 1422 |
-
|
| 1423 |
-
During the lookup for a name used as a default argument
|
| 1424 |
-
[[dcl.fct.default]] in a function *parameter-declaration-clause* or used
|
| 1425 |
-
in the *expression* of a *mem-initializer* for a constructor
|
| 1426 |
-
[[class.base.init]], the function parameter names are visible and hide
|
| 1427 |
-
the names of entities declared in the block, class or namespace scopes
|
| 1428 |
-
containing the function declaration.
|
| 1429 |
-
|
| 1430 |
-
[*Note 5*: [[dcl.fct.default]] further describes the restrictions on
|
| 1431 |
-
the use of names in default arguments. [[class.base.init]] further
|
| 1432 |
-
describes the restrictions on the use of names in a
|
| 1433 |
-
*ctor-initializer*. — *end note*]
|
| 1434 |
-
|
| 1435 |
-
During the lookup of a name used in the *constant-expression* of an
|
| 1436 |
-
*enumerator-definition*, previously declared *enumerator*s of the
|
| 1437 |
-
enumeration are visible and hide the names of entities declared in the
|
| 1438 |
-
block, class, or namespace scopes containing the *enum-specifier*.
|
| 1439 |
-
|
| 1440 |
-
A name used in the definition of a `static` data member of class `X`
|
| 1441 |
-
[[class.static.data]] (after the *qualified-id* of the static member) is
|
| 1442 |
-
looked up as if the name was used in a member function of `X`.
|
| 1443 |
-
|
| 1444 |
-
[*Note 6*: [[class.static.data]] further describes the restrictions on
|
| 1445 |
-
the use of names in the definition of a `static` data
|
| 1446 |
-
member. — *end note*]
|
| 1447 |
-
|
| 1448 |
-
If a variable member of a namespace is defined outside of the scope of
|
| 1449 |
-
its namespace then any name that appears in the definition of the member
|
| 1450 |
-
(after the *declarator-id*) is looked up as if the definition of the
|
| 1451 |
-
member occurred in its namespace.
|
| 1452 |
-
|
| 1453 |
-
[*Example 5*:
|
| 1454 |
-
|
| 1455 |
-
``` cpp
|
| 1456 |
-
namespace N {
|
| 1457 |
-
int i = 4;
|
| 1458 |
-
extern int j;
|
| 1459 |
-
}
|
| 1460 |
-
|
| 1461 |
-
int i = 2;
|
| 1462 |
-
|
| 1463 |
-
int N::j = i; // N::j == 4
|
| 1464 |
-
```
|
| 1465 |
-
|
| 1466 |
-
— *end example*]
|
| 1467 |
-
|
| 1468 |
-
A name used in the handler for a *function-try-block* [[except.pre]] is
|
| 1469 |
-
looked up as if the name was used in the outermost block of the function
|
| 1470 |
-
definition. In particular, the function parameter names shall not be
|
| 1471 |
-
redeclared in the *exception-declaration* nor in the outermost block of
|
| 1472 |
-
a handler for the *function-try-block*. Names declared in the outermost
|
| 1473 |
-
block of the function definition are not found when looked up in the
|
| 1474 |
-
scope of a handler for the *function-try-block*.
|
| 1475 |
-
|
| 1476 |
-
[*Note 7*: But function parameter names are found. — *end note*]
|
| 1477 |
-
|
| 1478 |
-
[*Note 8*: The rules for name lookup in template definitions are
|
| 1479 |
-
described in [[temp.res]]. — *end note*]
|
| 1480 |
-
|
| 1481 |
-
### Argument-dependent name lookup <a id="basic.lookup.argdep">[[basic.lookup.argdep]]</a>
|
| 1482 |
-
|
| 1483 |
-
When the *postfix-expression* in a function call [[expr.call]] is an
|
| 1484 |
-
*unqualified-id*, other namespaces not considered during the usual
|
| 1485 |
-
unqualified lookup [[basic.lookup.unqual]] may be searched, and in those
|
| 1486 |
-
namespaces, namespace-scope friend function or function template
|
| 1487 |
-
declarations [[class.friend]] not otherwise visible may be found. These
|
| 1488 |
-
modifications to the search depend on the types of the arguments (and
|
| 1489 |
-
for template template arguments, the namespace of the template
|
| 1490 |
-
argument).
|
| 1491 |
-
|
| 1492 |
-
[*Example 1*:
|
| 1493 |
-
|
| 1494 |
-
``` cpp
|
| 1495 |
-
namespace N {
|
| 1496 |
-
struct S { };
|
| 1497 |
-
void f(S);
|
| 1498 |
-
}
|
| 1499 |
-
|
| 1500 |
-
void g() {
|
| 1501 |
-
N::S s;
|
| 1502 |
-
f(s); // OK: calls N::f
|
| 1503 |
-
(f)(s); // error: N::f not considered; parentheses prevent argument-dependent lookup
|
| 1504 |
-
}
|
| 1505 |
-
```
|
| 1506 |
-
|
| 1507 |
-
— *end example*]
|
| 1508 |
-
|
| 1509 |
For each argument type `T` in the function call, there is a set of zero
|
| 1510 |
-
or more *associated
|
| 1511 |
-
|
| 1512 |
-
|
| 1513 |
-
|
| 1514 |
-
|
| 1515 |
-
types do not contribute to this set. The sets of namespaces and entities
|
| 1516 |
-
are determined in the following way:
|
| 1517 |
|
| 1518 |
-
- If `T` is a fundamental type, its associated
|
| 1519 |
-
entities are both empty.
|
| 1520 |
- If `T` is a class type (including unions), its associated entities
|
| 1521 |
are: the class itself; the class of which it is a member, if any; and
|
| 1522 |
-
its direct and indirect base classes.
|
| 1523 |
-
|
| 1524 |
-
|
| 1525 |
-
|
| 1526 |
-
|
| 1527 |
-
template
|
| 1528 |
-
|
| 1529 |
-
|
| 1530 |
-
|
| 1531 |
-
|
| 1532 |
-
|
| 1533 |
-
|
| 1534 |
-
|
| 1535 |
-
|
| 1536 |
-
|
| 1537 |
-
namespaces and entities are those associated with `U`.
|
| 1538 |
-
- If `T` is a function type, its associated namespaces and entities are
|
| 1539 |
-
those associated with the function parameter types and those
|
| 1540 |
-
associated with the return type.
|
| 1541 |
- If `T` is a pointer to a member function of a class `X`, its
|
| 1542 |
-
associated
|
| 1543 |
-
|
| 1544 |
-
associated with `X`.
|
| 1545 |
- If `T` is a pointer to a data member of class `X`, its associated
|
| 1546 |
-
|
| 1547 |
-
|
| 1548 |
|
| 1549 |
-
|
| 1550 |
-
|
| 1551 |
-
|
| 1552 |
-
|
| 1553 |
-
|
| 1554 |
-
|
| 1555 |
-
|
| 1556 |
-
and return type. Additionally, if the aforementioned overload set is
|
| 1557 |
-
named with a *template-id*, its associated entities and namespaces also
|
| 1558 |
-
include those of its type *template-argument*s and its template
|
| 1559 |
-
*template-argument*s.
|
| 1560 |
|
| 1561 |
-
|
| 1562 |
-
|
| 1563 |
-
|
|
|
|
|
|
|
| 1564 |
|
| 1565 |
-
- a
|
| 1566 |
-
- a
|
| 1567 |
-
or
|
| 1568 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1569 |
|
| 1570 |
-
|
| 1571 |
-
|
| 1572 |
-
|
| 1573 |
-
|
| 1574 |
-
|
| 1575 |
-
[
|
| 1576 |
-
types can include namespaces and entities already considered by the
|
| 1577 |
-
ordinary unqualified lookup. — *end note*]
|
| 1578 |
|
| 1579 |
[*Example 2*:
|
| 1580 |
|
| 1581 |
-
``` cpp
|
| 1582 |
-
namespace NS {
|
| 1583 |
-
class T { };
|
| 1584 |
-
void f(T);
|
| 1585 |
-
void g(T, int);
|
| 1586 |
-
}
|
| 1587 |
-
NS::T parm;
|
| 1588 |
-
void g(NS::T, float);
|
| 1589 |
-
int main() {
|
| 1590 |
-
f(parm); // OK: calls NS::f
|
| 1591 |
-
extern void g(NS::T, float);
|
| 1592 |
-
g(parm, 1); // OK: calls g(NS::T, float)
|
| 1593 |
-
}
|
| 1594 |
-
```
|
| 1595 |
-
|
| 1596 |
-
— *end example*]
|
| 1597 |
-
|
| 1598 |
-
When considering an associated namespace `N`, the lookup is the same as
|
| 1599 |
-
the lookup performed when `N` is used as a qualifier [[namespace.qual]]
|
| 1600 |
-
except that:
|
| 1601 |
-
|
| 1602 |
-
- Any *using-directive*s in `N` are ignored.
|
| 1603 |
-
- All names except those of (possibly overloaded) functions and function
|
| 1604 |
-
templates are ignored.
|
| 1605 |
-
- Any namespace-scope friend functions or friend function templates
|
| 1606 |
-
[[class.friend]] declared in classes with reachable definitions in the
|
| 1607 |
-
set of associated entities are visible within their respective
|
| 1608 |
-
namespaces even if they are not visible during an ordinary lookup
|
| 1609 |
-
[[namespace.memdef]].
|
| 1610 |
-
- Any exported declaration `D` in `N` declared within the purview of a
|
| 1611 |
-
named module `M` [[module.interface]] is visible if there is an
|
| 1612 |
-
associated entity attached to `M` with the same innermost enclosing
|
| 1613 |
-
non-inline namespace as `D`.
|
| 1614 |
-
- If the lookup is for a dependent name ([[temp.dep]],
|
| 1615 |
-
[[temp.dep.candidate]]), any declaration `D` in `N` is visible if `D`
|
| 1616 |
-
would be visible to qualified name lookup [[namespace.qual]] at any
|
| 1617 |
-
point in the instantiation context [[module.context]] of the lookup,
|
| 1618 |
-
unless `D` is declared in another translation unit, attached to the
|
| 1619 |
-
global module, and is either discarded [[module.global.frag]] or has
|
| 1620 |
-
internal linkage.
|
| 1621 |
-
|
| 1622 |
-
[*Example 3*:
|
| 1623 |
-
|
| 1624 |
Translation unit #1
|
| 1625 |
|
| 1626 |
``` cpp
|
| 1627 |
export module M;
|
| 1628 |
namespace R {
|
|
@@ -1666,21 +1710,42 @@ void test() {
|
|
| 1666 |
}
|
| 1667 |
```
|
| 1668 |
|
| 1669 |
— *end example*]
|
| 1670 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1671 |
### Qualified name lookup <a id="basic.lookup.qual">[[basic.lookup.qual]]</a>
|
| 1672 |
|
| 1673 |
-
|
| 1674 |
-
|
| 1675 |
-
|
| 1676 |
-
|
| 1677 |
-
|
| 1678 |
-
|
| 1679 |
-
|
| 1680 |
-
|
| 1681 |
-
the program is ill-formed.
|
| 1682 |
|
| 1683 |
[*Example 1*:
|
| 1684 |
|
| 1685 |
``` cpp
|
| 1686 |
class A {
|
|
@@ -1690,70 +1755,137 @@ public:
|
|
| 1690 |
int main() {
|
| 1691 |
int A;
|
| 1692 |
A::n = 42; // OK
|
| 1693 |
A b; // error: A does not name a type
|
| 1694 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1695 |
```
|
| 1696 |
|
| 1697 |
— *end example*]
|
| 1698 |
|
| 1699 |
-
|
| 1700 |
-
|
| 1701 |
-
|
| 1702 |
-
|
| 1703 |
-
|
| 1704 |
-
|
| 1705 |
-
|
| 1706 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1707 |
|
| 1708 |
[*Example 2*:
|
| 1709 |
|
|
|
|
|
|
|
| 1710 |
``` cpp
|
| 1711 |
-
|
| 1712 |
-
class C {
|
| 1713 |
-
class X { };
|
| 1714 |
-
static const int number = 50;
|
| 1715 |
-
static X arr[number];
|
| 1716 |
-
};
|
| 1717 |
-
X C::arr[number]; // error:
|
| 1718 |
-
// equivalent to ::X C::arr[C::number];
|
| 1719 |
-
// and not to C::X C::arr[C::number];
|
| 1720 |
```
|
| 1721 |
|
|
|
|
|
|
|
|
|
|
| 1722 |
— *end example*]
|
| 1723 |
|
| 1724 |
-
|
| 1725 |
-
|
| 1726 |
-
|
| 1727 |
-
|
| 1728 |
-
|
| 1729 |
-
|
| 1730 |
-
|
| 1731 |
-
|
| 1732 |
-
|
| 1733 |
-
|
| 1734 |
-
|
| 1735 |
-
|
| 1736 |
-
|
| 1737 |
-
``` bnf
|
| 1738 |
-
nested-name-specifierₒₚₜ type-name '::' '~' type-name
|
| 1739 |
-
```
|
| 1740 |
-
|
| 1741 |
-
the second *type-name* is looked up in the same scope as the first.
|
| 1742 |
|
| 1743 |
[*Example 3*:
|
| 1744 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1745 |
``` cpp
|
| 1746 |
struct C {
|
| 1747 |
typedef int I;
|
| 1748 |
};
|
| 1749 |
typedef int I1, I2;
|
| 1750 |
extern int* p;
|
| 1751 |
extern int* q;
|
|
|
|
| 1752 |
p->C::I::~I(); // I is looked up in the scope of C
|
| 1753 |
-
q->I1::~I2();
|
| 1754 |
-
|
| 1755 |
struct A {
|
| 1756 |
~A();
|
| 1757 |
};
|
| 1758 |
typedef A AB;
|
| 1759 |
int main() {
|
|
@@ -1762,55 +1894,22 @@ int main() {
|
|
| 1762 |
}
|
| 1763 |
```
|
| 1764 |
|
| 1765 |
— *end example*]
|
| 1766 |
|
| 1767 |
-
[*Note 2*: [[basic.lookup.classref]] describes how name lookup
|
| 1768 |
-
proceeds after the `.` and `->` operators. — *end note*]
|
| 1769 |
-
|
| 1770 |
#### Class members <a id="class.qual">[[class.qual]]</a>
|
| 1771 |
|
| 1772 |
-
|
| 1773 |
-
|
| 1774 |
-
scope of the class [[class.member.lookup]], except for the cases listed
|
| 1775 |
-
below. The name shall represent one or more members of that class or of
|
| 1776 |
-
one of its base classes [[class.derived]].
|
| 1777 |
|
| 1778 |
-
|
| 1779 |
-
|
|
|
|
| 1780 |
|
| 1781 |
-
|
| 1782 |
-
|
| 1783 |
-
|
| 1784 |
-
- a *conversion-type-id* of a *conversion-function-id* is looked up in
|
| 1785 |
-
the same manner as a *conversion-type-id* in a class member access
|
| 1786 |
-
(see [[basic.lookup.classref]]);
|
| 1787 |
-
- the names in a *template-argument* of a *template-id* are looked up in
|
| 1788 |
-
the context in which the entire *postfix-expression* occurs;
|
| 1789 |
-
- the lookup for a name specified in a *using-declaration*
|
| 1790 |
-
[[namespace.udecl]] also finds class or enumeration names hidden
|
| 1791 |
-
within the same scope [[basic.scope.hiding]].
|
| 1792 |
-
|
| 1793 |
-
In a lookup in which function names are not ignored[^8] and the
|
| 1794 |
-
*nested-name-specifier* nominates a class `C`:
|
| 1795 |
-
|
| 1796 |
-
- if the name specified after the *nested-name-specifier*, when looked
|
| 1797 |
-
up in `C`, is the injected-class-name of `C` [[class.pre]], or
|
| 1798 |
-
- in a *using-declarator* of a *using-declaration* [[namespace.udecl]]
|
| 1799 |
-
that is a *member-declaration*, if the name specified after the
|
| 1800 |
-
*nested-name-specifier* is the same as the *identifier* or the
|
| 1801 |
-
*simple-template-id*’s *template-name* in the last component of the
|
| 1802 |
-
*nested-name-specifier*,
|
| 1803 |
-
|
| 1804 |
-
the name is instead considered to name the constructor of class `C`.
|
| 1805 |
-
|
| 1806 |
-
[*Note 2*: For example, the constructor is not an acceptable lookup
|
| 1807 |
-
result in an *elaborated-type-specifier* so the constructor would not be
|
| 1808 |
-
used in place of the injected-class-name. — *end note*]
|
| 1809 |
-
|
| 1810 |
-
Such a constructor name shall be used only in the *declarator-id* of a
|
| 1811 |
-
declaration that names a constructor or in a *using-declaration*.
|
| 1812 |
|
| 1813 |
[*Example 1*:
|
| 1814 |
|
| 1815 |
``` cpp
|
| 1816 |
struct A { A(); };
|
|
@@ -1824,39 +1923,21 @@ A::A a; // error: A::A is not a type name
|
|
| 1824 |
struct A::A a2; // object of type A
|
| 1825 |
```
|
| 1826 |
|
| 1827 |
— *end example*]
|
| 1828 |
|
| 1829 |
-
A class member name hidden by a name in a nested declarative region or
|
| 1830 |
-
by the name of a derived class member can still be found if qualified by
|
| 1831 |
-
the name of its class followed by the `::` operator.
|
| 1832 |
-
|
| 1833 |
#### Namespace members <a id="namespace.qual">[[namespace.qual]]</a>
|
| 1834 |
|
| 1835 |
-
|
| 1836 |
-
|
| 1837 |
-
|
| 1838 |
-
|
| 1839 |
-
|
| 1840 |
-
|
| 1841 |
|
| 1842 |
-
|
| 1843 |
-
|
| 1844 |
-
declarations of `m` in `X` and the inline namespace set of `X`
|
| 1845 |
-
[[namespace.def]] whose potential scope [[basic.scope.namespace]] would
|
| 1846 |
-
include the namespace in which `m` is declared at the location of the
|
| 1847 |
-
*nested-name-specifier*. If S'(X, m) is not empty, S(X, m) is S'(X, m);
|
| 1848 |
-
otherwise, S(X, m) is the union of S(Nᵢ, m) for all namespaces Nᵢ
|
| 1849 |
-
nominated by *using-directive*s in `X` and its inline namespace set.
|
| 1850 |
-
|
| 1851 |
-
Given `X::m` (where `X` is a user-declared namespace), or given `::m`
|
| 1852 |
-
(where X is the global namespace), if S(X, m) is the empty set, the
|
| 1853 |
-
program is ill-formed. Otherwise, if S(X, m) has exactly one member, or
|
| 1854 |
-
if the context of the reference is a *using-declaration*
|
| 1855 |
-
[[namespace.udecl]], S(X, m) is the required set of declarations of `m`.
|
| 1856 |
-
Otherwise if the use of `m` is not one that allows a unique declaration
|
| 1857 |
-
to be chosen from S(X, m), the program is ill-formed.
|
| 1858 |
|
| 1859 |
[*Example 1*:
|
| 1860 |
|
| 1861 |
``` cpp
|
| 1862 |
int x;
|
|
@@ -1910,11 +1991,11 @@ void h()
|
|
| 1910 |
}
|
| 1911 |
```
|
| 1912 |
|
| 1913 |
— *end example*]
|
| 1914 |
|
| 1915 |
-
[*Note
|
| 1916 |
|
| 1917 |
The same declaration found more than once is not an ambiguity (because
|
| 1918 |
it is still a unique declaration).
|
| 1919 |
|
| 1920 |
[*Example 2*:
|
|
@@ -1937,11 +2018,11 @@ namespace BC {
|
|
| 1937 |
using namespace C;
|
| 1938 |
}
|
| 1939 |
|
| 1940 |
void f()
|
| 1941 |
{
|
| 1942 |
-
BC::a++; // OK
|
| 1943 |
}
|
| 1944 |
|
| 1945 |
namespace D {
|
| 1946 |
using A::a;
|
| 1947 |
}
|
|
@@ -1951,11 +2032,11 @@ namespace BD {
|
|
| 1951 |
using namespace D;
|
| 1952 |
}
|
| 1953 |
|
| 1954 |
void g()
|
| 1955 |
{
|
| 1956 |
-
BD::a++; // OK
|
| 1957 |
}
|
| 1958 |
```
|
| 1959 |
|
| 1960 |
— *end example*]
|
| 1961 |
|
|
@@ -1980,26 +2061,21 @@ namespace B {
|
|
| 1980 |
using namespace A;
|
| 1981 |
}
|
| 1982 |
|
| 1983 |
void f()
|
| 1984 |
{
|
| 1985 |
-
A::a++; // OK
|
| 1986 |
-
B::a++; // OK
|
| 1987 |
-
A::b++; // OK
|
| 1988 |
-
B::b++; // OK
|
| 1989 |
}
|
| 1990 |
```
|
| 1991 |
|
| 1992 |
— *end example*]
|
| 1993 |
|
| 1994 |
-
|
| 1995 |
-
|
| 1996 |
-
introduces a class name or enumeration name and the other declarations
|
| 1997 |
-
introduce either the same variable, the same enumerator, or a set of
|
| 1998 |
-
functions, the non-type name hides the class or enumeration name if and
|
| 1999 |
-
only if the declarations are from the same namespace; otherwise (the
|
| 2000 |
-
declarations are from different namespaces), the program is ill-formed.
|
| 2001 |
|
| 2002 |
[*Example 4*:
|
| 2003 |
|
| 2004 |
``` cpp
|
| 2005 |
namespace A {
|
|
@@ -2020,221 +2096,55 @@ namespace C {
|
|
| 2020 |
}
|
| 2021 |
```
|
| 2022 |
|
| 2023 |
— *end example*]
|
| 2024 |
|
| 2025 |
-
In a declaration for a namespace member in which the *declarator-id* is
|
| 2026 |
-
a *qualified-id*, given that the *qualified-id* for the namespace member
|
| 2027 |
-
has the form
|
| 2028 |
-
|
| 2029 |
-
``` bnf
|
| 2030 |
-
nested-name-specifier unqualified-id
|
| 2031 |
-
```
|
| 2032 |
-
|
| 2033 |
-
the *unqualified-id* shall name a member of the namespace designated by
|
| 2034 |
-
the *nested-name-specifier* or of an element of the inline namespace set
|
| 2035 |
-
[[namespace.def]] of that namespace.
|
| 2036 |
-
|
| 2037 |
-
[*Example 5*:
|
| 2038 |
-
|
| 2039 |
-
``` cpp
|
| 2040 |
-
namespace A {
|
| 2041 |
-
namespace B {
|
| 2042 |
-
void f1(int);
|
| 2043 |
-
}
|
| 2044 |
-
using namespace B;
|
| 2045 |
-
}
|
| 2046 |
-
void A::f1(int){ } // error: f1 is not a member of A
|
| 2047 |
-
```
|
| 2048 |
-
|
| 2049 |
-
— *end example*]
|
| 2050 |
-
|
| 2051 |
-
However, in such namespace member declarations, the
|
| 2052 |
-
*nested-name-specifier* may rely on *using-directive*s to implicitly
|
| 2053 |
-
provide the initial part of the *nested-name-specifier*.
|
| 2054 |
-
|
| 2055 |
-
[*Example 6*:
|
| 2056 |
-
|
| 2057 |
-
``` cpp
|
| 2058 |
-
namespace A {
|
| 2059 |
-
namespace B {
|
| 2060 |
-
void f1(int);
|
| 2061 |
-
}
|
| 2062 |
-
}
|
| 2063 |
-
|
| 2064 |
-
namespace C {
|
| 2065 |
-
namespace D {
|
| 2066 |
-
void f1(int);
|
| 2067 |
-
}
|
| 2068 |
-
}
|
| 2069 |
-
|
| 2070 |
-
using namespace A;
|
| 2071 |
-
using namespace C::D;
|
| 2072 |
-
void B::f1(int){ } // OK, defines A::B::f1(int)
|
| 2073 |
-
```
|
| 2074 |
-
|
| 2075 |
-
— *end example*]
|
| 2076 |
-
|
| 2077 |
### Elaborated type specifiers <a id="basic.lookup.elab">[[basic.lookup.elab]]</a>
|
| 2078 |
|
| 2079 |
-
|
| 2080 |
-
|
| 2081 |
-
|
| 2082 |
-
|
| 2083 |
-
|
| 2084 |
-
|
| 2085 |
-
|
| 2086 |
-
|
| 2087 |
-
|
| 2088 |
-
|
| 2089 |
-
|
| 2090 |
-
|
| 2091 |
-
|
| 2092 |
-
ignoring any non-type names that have been declared. If the
|
| 2093 |
-
*elaborated-type-specifier* is introduced by the `enum` keyword and this
|
| 2094 |
-
lookup does not find a previously declared *type-name*, the
|
| 2095 |
-
*elaborated-type-specifier* is ill-formed. If the
|
| 2096 |
-
*elaborated-type-specifier* is introduced by the *class-key* and this
|
| 2097 |
-
lookup does not find a previously declared *type-name*, or if the
|
| 2098 |
-
*elaborated-type-specifier* appears in a declaration with the form:
|
| 2099 |
-
|
| 2100 |
-
``` bnf
|
| 2101 |
-
class-key attribute-specifier-seqₒₚₜ identifier ';'
|
| 2102 |
-
```
|
| 2103 |
-
|
| 2104 |
-
the *elaborated-type-specifier* is a declaration that introduces the
|
| 2105 |
-
*class-name* as described in [[basic.scope.pdecl]].
|
| 2106 |
-
|
| 2107 |
-
If the *elaborated-type-specifier* has a *nested-name-specifier*,
|
| 2108 |
-
qualified name lookup is performed, as described in
|
| 2109 |
-
[[basic.lookup.qual]], but ignoring any non-type names that have been
|
| 2110 |
-
declared. If the name lookup does not find a previously declared
|
| 2111 |
-
*type-name*, the *elaborated-type-specifier* is ill-formed.
|
| 2112 |
|
| 2113 |
[*Example 1*:
|
| 2114 |
|
| 2115 |
``` cpp
|
| 2116 |
struct Node {
|
| 2117 |
-
struct Node* Next; // OK
|
| 2118 |
-
struct Data* Data; // OK
|
| 2119 |
};
|
| 2120 |
|
| 2121 |
struct Data {
|
| 2122 |
-
struct Node* Node; // OK
|
| 2123 |
friend struct ::Glob; // error: Glob is not declared, cannot introduce a qualified type[dcl.type.elab]
|
| 2124 |
-
friend struct Glob; // OK
|
| 2125 |
...
|
| 2126 |
};
|
| 2127 |
|
| 2128 |
struct Base {
|
| 2129 |
-
struct Data; // OK
|
| 2130 |
-
struct ::Data* thatData; // OK
|
| 2131 |
-
struct Base::Data* thisData; // OK
|
| 2132 |
-
friend class ::Data; // OK
|
| 2133 |
-
friend class Data; // OK
|
| 2134 |
struct Data { ... }; // Defines nested Data
|
| 2135 |
};
|
| 2136 |
|
| 2137 |
-
struct Data; // OK
|
| 2138 |
struct ::Data; // error: cannot introduce a qualified type[dcl.type.elab]
|
| 2139 |
struct Base::Data; // error: cannot introduce a qualified type[dcl.type.elab]
|
| 2140 |
struct Base::Datum; // error: Datum undefined
|
| 2141 |
-
struct Base::Data* pBase; // OK
|
| 2142 |
-
```
|
| 2143 |
-
|
| 2144 |
-
— *end example*]
|
| 2145 |
-
|
| 2146 |
-
### Class member access <a id="basic.lookup.classref">[[basic.lookup.classref]]</a>
|
| 2147 |
-
|
| 2148 |
-
In a class member access expression [[expr.ref]], if the `.` or `->`
|
| 2149 |
-
token is immediately followed by an *identifier* followed by a `<`, the
|
| 2150 |
-
identifier must be looked up to determine whether the `<` is the
|
| 2151 |
-
beginning of a template argument list [[temp.names]] or a less-than
|
| 2152 |
-
operator. The identifier is first looked up in the class of the object
|
| 2153 |
-
expression [[class.member.lookup]]. If the identifier is not found, it
|
| 2154 |
-
is then looked up in the context of the entire *postfix-expression* and
|
| 2155 |
-
shall name a template whose specializations are types.
|
| 2156 |
-
|
| 2157 |
-
If the *id-expression* in a class member access [[expr.ref]] is an
|
| 2158 |
-
*unqualified-id*, and the type of the object expression is of a class
|
| 2159 |
-
type `C`, the *unqualified-id* is looked up in the scope of class `C`
|
| 2160 |
-
[[class.member.lookup]].
|
| 2161 |
-
|
| 2162 |
-
If the *unqualified-id* is `~`*type-name*, the *type-name* is looked up
|
| 2163 |
-
in the context of the entire *postfix-expression*. If the type `T` of
|
| 2164 |
-
the object expression is of a class type `C`, the *type-name* is also
|
| 2165 |
-
looked up in the scope of class `C`. At least one of the lookups shall
|
| 2166 |
-
find a name that refers to cv `T`.
|
| 2167 |
-
|
| 2168 |
-
[*Example 1*:
|
| 2169 |
-
|
| 2170 |
-
``` cpp
|
| 2171 |
-
struct A { };
|
| 2172 |
-
|
| 2173 |
-
struct B {
|
| 2174 |
-
struct A { };
|
| 2175 |
-
void f(::A* a);
|
| 2176 |
-
};
|
| 2177 |
-
|
| 2178 |
-
void B::f(::A* a) {
|
| 2179 |
-
a->~A(); // OK: lookup in *a finds the injected-class-name
|
| 2180 |
-
}
|
| 2181 |
-
```
|
| 2182 |
-
|
| 2183 |
-
— *end example*]
|
| 2184 |
-
|
| 2185 |
-
If the *id-expression* in a class member access is a *qualified-id* of
|
| 2186 |
-
the form
|
| 2187 |
-
|
| 2188 |
-
``` cpp
|
| 2189 |
-
class-name-or-namespace-name::...
|
| 2190 |
-
```
|
| 2191 |
-
|
| 2192 |
-
the `class-name-or-namespace-name` following the `.` or `->` operator is
|
| 2193 |
-
first looked up in the class of the object expression
|
| 2194 |
-
[[class.member.lookup]] and the name, if found, is used. Otherwise it is
|
| 2195 |
-
looked up in the context of the entire *postfix-expression*.
|
| 2196 |
-
|
| 2197 |
-
[*Note 1*: See [[basic.lookup.qual]], which describes the lookup of a
|
| 2198 |
-
name before `::`, which will only find a type or namespace
|
| 2199 |
-
name. — *end note*]
|
| 2200 |
-
|
| 2201 |
-
If the *qualified-id* has the form
|
| 2202 |
-
|
| 2203 |
-
``` cpp
|
| 2204 |
-
::class-name-or-namespace-name::...
|
| 2205 |
-
```
|
| 2206 |
-
|
| 2207 |
-
the `class-name-or-namespace-name` is looked up in global scope as a
|
| 2208 |
-
*class-name* or *namespace-name*.
|
| 2209 |
-
|
| 2210 |
-
If the *nested-name-specifier* contains a *simple-template-id*
|
| 2211 |
-
[[temp.names]], the names in its *template-argument*s are looked up in
|
| 2212 |
-
the context in which the entire *postfix-expression* occurs.
|
| 2213 |
-
|
| 2214 |
-
If the *id-expression* is a *conversion-function-id*, its
|
| 2215 |
-
*conversion-type-id* is first looked up in the class of the object
|
| 2216 |
-
expression [[class.member.lookup]] and the name, if found, is used.
|
| 2217 |
-
Otherwise it is looked up in the context of the entire
|
| 2218 |
-
*postfix-expression*. In each of these lookups, only names that denote
|
| 2219 |
-
types or templates whose specializations are types are considered.
|
| 2220 |
-
|
| 2221 |
-
[*Example 2*:
|
| 2222 |
-
|
| 2223 |
-
``` cpp
|
| 2224 |
-
struct A { };
|
| 2225 |
-
namespace N {
|
| 2226 |
-
struct A {
|
| 2227 |
-
void g() { }
|
| 2228 |
-
template <class T> operator T();
|
| 2229 |
-
};
|
| 2230 |
-
}
|
| 2231 |
-
|
| 2232 |
-
int main() {
|
| 2233 |
-
N::A a;
|
| 2234 |
-
a.operator A(); // calls N::A::operator N::A
|
| 2235 |
-
}
|
| 2236 |
```
|
| 2237 |
|
| 2238 |
— *end example*]
|
| 2239 |
|
| 2240 |
### Using-directives and namespace aliases <a id="basic.lookup.udir">[[basic.lookup.udir]]</a>
|
|
@@ -2253,11 +2163,11 @@ declarations.
|
|
| 2253 |
translation-unit:
|
| 2254 |
declaration-seqₒₚₜ
|
| 2255 |
global-module-fragmentₒₚₜ module-declaration declaration-seqₒₚₜ private-module-fragmentₒₚₜ
|
| 2256 |
```
|
| 2257 |
|
| 2258 |
-
A name is said to have *linkage* when it
|
| 2259 |
reference, function, type, template, namespace or value as a name
|
| 2260 |
introduced by a declaration in another scope:
|
| 2261 |
|
| 2262 |
- When a name has *external linkage*, the entity it denotes can be
|
| 2263 |
referred to by names from scopes of other translation units or from
|
|
@@ -2269,30 +2179,33 @@ introduced by a declaration in another scope:
|
|
| 2269 |
- When a name has *internal linkage*, the entity it denotes can be
|
| 2270 |
referred to by names from other scopes in the same translation unit.
|
| 2271 |
- When a name has *no linkage*, the entity it denotes cannot be referred
|
| 2272 |
to by names from other scopes.
|
| 2273 |
|
| 2274 |
-
|
| 2275 |
-
linkage if it is the name of
|
| 2276 |
|
| 2277 |
- a variable, variable template, function, or function template that is
|
| 2278 |
explicitly declared `static`; or
|
| 2279 |
- a non-template variable of non-volatile const-qualified type, unless
|
|
|
|
|
|
|
| 2280 |
- it is explicitly declared `extern`, or
|
| 2281 |
-
- it is inline
|
| 2282 |
- it was previously declared and the prior declaration did not have
|
| 2283 |
internal linkage; or
|
| 2284 |
- a data member of an anonymous union.
|
| 2285 |
|
| 2286 |
[*Note 1*: An instantiated variable template that has const-qualified
|
| 2287 |
type can have external or module linkage, even if not declared
|
| 2288 |
`extern`. — *end note*]
|
| 2289 |
|
| 2290 |
An unnamed namespace or a namespace declared directly or indirectly
|
| 2291 |
within an unnamed namespace has internal linkage. All other namespaces
|
| 2292 |
-
have external linkage.
|
| 2293 |
-
given internal linkage above and that is the
|
|
|
|
| 2294 |
|
| 2295 |
- a variable; or
|
| 2296 |
- a function; or
|
| 2297 |
- a named class [[class.pre]], or an unnamed class defined in a typedef
|
| 2298 |
declaration in which the class has the typedef name for linkage
|
|
@@ -2311,112 +2224,71 @@ has its linkage determined as follows:
|
|
| 2311 |
- otherwise, if the declaration of the name is attached to a named
|
| 2312 |
module [[module.unit]] and is not exported [[module.interface]], the
|
| 2313 |
name has module linkage;
|
| 2314 |
- otherwise, the name has external linkage.
|
| 2315 |
|
| 2316 |
-
In addition, a member function, static data member, a named class or
|
| 2317 |
-
enumeration
|
| 2318 |
-
in a
|
| 2319 |
-
|
| 2320 |
-
linkage, if any, as the name of
|
| 2321 |
-
|
| 2322 |
-
The name of a function declared in block scope and the name of a
|
| 2323 |
-
variable declared by a block scope `extern` declaration have linkage. If
|
| 2324 |
-
such a declaration is attached to a named module, the program is
|
| 2325 |
-
ill-formed. If there is a visible declaration of an entity with linkage,
|
| 2326 |
-
ignoring entities declared outside the innermost enclosing namespace
|
| 2327 |
-
scope, such that the block scope declaration would be a (possibly
|
| 2328 |
-
ill-formed) redeclaration if the two declarations appeared in the same
|
| 2329 |
-
declarative region, the block scope declaration declares that same
|
| 2330 |
-
entity and receives the linkage of the previous declaration. If there is
|
| 2331 |
-
more than one such matching entity, the program is ill-formed.
|
| 2332 |
-
Otherwise, if no matching entity is found, the block scope entity
|
| 2333 |
-
receives external linkage. If, within a translation unit, the same
|
| 2334 |
-
entity is declared with both internal and external linkage, the program
|
| 2335 |
-
is ill-formed.
|
| 2336 |
|
| 2337 |
[*Example 1*:
|
| 2338 |
|
| 2339 |
``` cpp
|
| 2340 |
static void f();
|
| 2341 |
extern "C" void h();
|
| 2342 |
static int i = 0; // #1
|
| 2343 |
-
void
|
| 2344 |
extern void f(); // internal linkage
|
|
|
|
| 2345 |
extern void h(); // C language linkage
|
| 2346 |
int i; // #2: i has no linkage
|
| 2347 |
{
|
| 2348 |
extern void f(); // internal linkage
|
| 2349 |
-
extern int i; // #3:
|
| 2350 |
}
|
| 2351 |
}
|
| 2352 |
```
|
| 2353 |
|
| 2354 |
-
|
| 2355 |
-
|
| 2356 |
-
internal linkage
|
| 2357 |
-
making the program ill-formed.
|
| 2358 |
-
|
| 2359 |
-
— *end example*]
|
| 2360 |
-
|
| 2361 |
-
When a block scope declaration of an entity with linkage is not found to
|
| 2362 |
-
refer to some other declaration, then that entity is a member of the
|
| 2363 |
-
innermost enclosing namespace. However such a declaration does not
|
| 2364 |
-
introduce the member name in its namespace scope.
|
| 2365 |
-
|
| 2366 |
-
[*Example 2*:
|
| 2367 |
-
|
| 2368 |
-
``` cpp
|
| 2369 |
-
namespace X {
|
| 2370 |
-
void p() {
|
| 2371 |
-
q(); // error: q not yet declared
|
| 2372 |
-
extern void q(); // q is a member of namespace X
|
| 2373 |
-
}
|
| 2374 |
-
|
| 2375 |
-
void middle() {
|
| 2376 |
-
q(); // error: q not yet declared
|
| 2377 |
-
}
|
| 2378 |
-
|
| 2379 |
-
void q() { ... } // definition of X::q
|
| 2380 |
-
}
|
| 2381 |
-
|
| 2382 |
-
void q() { ... } // some other, unrelated q
|
| 2383 |
-
```
|
| 2384 |
|
| 2385 |
— *end example*]
|
| 2386 |
|
| 2387 |
Names not covered by these rules have no linkage. Moreover, except as
|
| 2388 |
noted, a name declared at block scope [[basic.scope.block]] has no
|
| 2389 |
linkage.
|
| 2390 |
|
| 2391 |
-
Two
|
| 2392 |
-
|
| 2393 |
-
|
|
|
|
|
|
|
| 2394 |
|
| 2395 |
-
-
|
| 2396 |
-
|
| 2397 |
-
|
| 2398 |
-
- both names
|
| 2399 |
-
by inheritance, of the same class; and
|
| 2400 |
-
- when both names denote functions or function templates, the
|
| 2401 |
-
signatures ([[defns.signature]], [[defns.signature.templ]]) are the
|
| 2402 |
-
same.
|
| 2403 |
|
| 2404 |
-
|
| 2405 |
-
|
| 2406 |
-
|
| 2407 |
|
| 2408 |
-
|
| 2409 |
-
|
| 2410 |
-
|
| 2411 |
-
Enumerators do not have linkage, but may serve as the name of an
|
| 2412 |
-
enumeration with linkage [[dcl.enum]]. — *end note*]
|
| 2413 |
|
| 2414 |
-
|
| 2415 |
-
different module, the program is ill-formed.
|
| 2416 |
|
| 2417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2418 |
|
| 2419 |
\`"decls.h"\`
|
| 2420 |
|
| 2421 |
``` cpp
|
| 2422 |
int f(); // #1, attached to the global module
|
|
@@ -2427,11 +2299,11 @@ Module interface of \`M\`
|
|
| 2427 |
|
| 2428 |
``` cpp
|
| 2429 |
module;
|
| 2430 |
#include "decls.h"
|
| 2431 |
export module M;
|
| 2432 |
-
export using ::f; // OK
|
| 2433 |
int g(); // error: matches #2, but attached to M
|
| 2434 |
export int h(); // #3
|
| 2435 |
export int k(); // #4
|
| 2436 |
```
|
| 2437 |
|
|
@@ -2447,32 +2319,62 @@ int k(); // error: matches #4
|
|
| 2447 |
|
| 2448 |
As a consequence of these rules, all declarations of an entity are
|
| 2449 |
attached to the same module; the entity is said to be *attached* to that
|
| 2450 |
module.
|
| 2451 |
|
| 2452 |
-
|
| 2453 |
-
|
| 2454 |
-
|
| 2455 |
-
|
| 2456 |
-
|
| 2457 |
-
|
| 2458 |
-
|
| 2459 |
-
|
| 2460 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2461 |
*linkage-specification* [[dcl.link]]. — *end note*]
|
| 2462 |
|
| 2463 |
A declaration D *names* an entity E if
|
| 2464 |
|
| 2465 |
- D contains a *lambda-expression* whose closure type is E,
|
| 2466 |
- E is not a function or function template and D contains an
|
| 2467 |
*id-expression*, *type-specifier*, *nested-name-specifier*,
|
| 2468 |
*template-name*, or *concept-name* denoting E, or
|
| 2469 |
- E is a function or function template and D contains an expression that
|
| 2470 |
names E [[basic.def.odr]] or an *id-expression* that refers to a set
|
| 2471 |
-
of overloads that contains E. \[*Note
|
| 2472 |
instantiated declaration do not refer to a set of overloads
|
| 2473 |
-
[[temp.
|
| 2474 |
|
| 2475 |
A declaration is an *exposure* if it either names a TU-local entity
|
| 2476 |
(defined below), ignoring
|
| 2477 |
|
| 2478 |
- the *function-body* for a non-inline function or function template
|
|
@@ -2482,18 +2384,18 @@ A declaration is an *exposure* if it either names a TU-local entity
|
|
| 2482 |
- the *initializer* for a variable or variable template (but not the
|
| 2483 |
variable’s type),
|
| 2484 |
- friend declarations in a class definition, and
|
| 2485 |
- any reference to a non-volatile const object or reference with
|
| 2486 |
internal or no linkage initialized with a constant expression that is
|
| 2487 |
-
not an odr-use [[
|
| 2488 |
|
| 2489 |
or defines a constexpr variable initialized to a TU-local value (defined
|
| 2490 |
below).
|
| 2491 |
|
| 2492 |
-
[*Note
|
| 2493 |
-
explicit specializations of it
|
| 2494 |
-
units. — *end note*]
|
| 2495 |
|
| 2496 |
An entity is *TU-local* if it is
|
| 2497 |
|
| 2498 |
- a type, function, variable, or template that
|
| 2499 |
- has a name with internal linkage, or
|
|
@@ -2504,17 +2406,17 @@ An entity is *TU-local* if it is
|
|
| 2504 |
*defining-type-specifier* that is used to declare only TU-local
|
| 2505 |
entities,
|
| 2506 |
- a specialization of a TU-local template,
|
| 2507 |
- a specialization of a template with any TU-local template argument, or
|
| 2508 |
- a specialization of a template whose (possibly instantiated)
|
| 2509 |
-
declaration is an exposure. \[*Note
|
| 2510 |
-
|
| 2511 |
|
| 2512 |
A value or object is *TU-local* if either
|
| 2513 |
|
| 2514 |
- it is, or is a pointer to, a TU-local function or the object
|
| 2515 |
-
associated with a TU-local variable,
|
| 2516 |
- it is an object of class or array type and any of its subobjects or
|
| 2517 |
any of the objects or functions to which its non-static data members
|
| 2518 |
of reference type refer is TU-local and is usable in constant
|
| 2519 |
expressions.
|
| 2520 |
|
|
@@ -2560,11 +2462,11 @@ namespace N {
|
|
| 2560 |
void adl(A);
|
| 2561 |
static void adl(int);
|
| 2562 |
}
|
| 2563 |
void adl(double);
|
| 2564 |
|
| 2565 |
-
inline void h(auto x) { adl(x); } // OK, but
|
| 2566 |
```
|
| 2567 |
|
| 2568 |
Translation unit #2
|
| 2569 |
|
| 2570 |
``` cpp
|
|
@@ -2585,24 +2487,29 @@ void other() {
|
|
| 2585 |
## Memory and objects <a id="basic.memobj">[[basic.memobj]]</a>
|
| 2586 |
|
| 2587 |
### Memory model <a id="intro.memory">[[intro.memory]]</a>
|
| 2588 |
|
| 2589 |
The fundamental storage unit in the C++ memory model is the *byte*. A
|
| 2590 |
-
byte is at least large enough to contain
|
| 2591 |
-
|
| 2592 |
-
the
|
| 2593 |
-
|
| 2594 |
-
|
| 2595 |
-
|
| 2596 |
-
|
| 2597 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2598 |
|
| 2599 |
[*Note 1*: The representation of types is described in
|
| 2600 |
-
[[basic.types]]. — *end note*]
|
| 2601 |
|
| 2602 |
-
A *memory location* is either an object of scalar type
|
| 2603 |
-
sequence of adjacent bit-fields all having
|
|
|
|
| 2604 |
|
| 2605 |
[*Note 2*: Various features of the language, such as references and
|
| 2606 |
virtual functions, might involve additional memory locations that are
|
| 2607 |
not accessible to programs but are managed by the
|
| 2608 |
implementation. — *end note*]
|
|
@@ -2630,11 +2537,11 @@ struct {
|
|
| 2630 |
int b:5,
|
| 2631 |
c:11,
|
| 2632 |
:0,
|
| 2633 |
d:8;
|
| 2634 |
struct {int ee:8;} e;
|
| 2635 |
-
}
|
| 2636 |
```
|
| 2637 |
|
| 2638 |
contains four separate memory locations: The member `a` and bit-fields
|
| 2639 |
`d` and `e.ee` are each separate memory locations, and can be modified
|
| 2640 |
concurrently without interfering with each other. The bit-fields `b` and
|
|
@@ -2649,27 +2556,27 @@ can be.
|
|
| 2649 |
The constructs in a C++ program create, destroy, refer to, access, and
|
| 2650 |
manipulate objects. An *object* is created by a definition
|
| 2651 |
[[basic.def]], by a *new-expression* [[expr.new]], by an operation that
|
| 2652 |
implicitly creates objects (see below), when implicitly changing the
|
| 2653 |
active member of a union [[class.union]], or when a temporary object is
|
| 2654 |
-
created
|
| 2655 |
-
|
| 2656 |
-
|
| 2657 |
[[class.cdtor]].
|
| 2658 |
|
| 2659 |
[*Note 1*: A function is not an object, regardless of whether or not it
|
| 2660 |
occupies storage in the way that objects do. — *end note*]
|
| 2661 |
|
| 2662 |
The properties of an object are determined when the object is created.
|
| 2663 |
An object can have a name [[basic.pre]]. An object has a storage
|
| 2664 |
duration [[basic.stc]] which influences its lifetime [[basic.life]]. An
|
| 2665 |
-
object has a type [[basic.types]].
|
| 2666 |
-
|
| 2667 |
-
|
| 2668 |
-
|
| 2669 |
-
|
| 2670 |
-
|
| 2671 |
|
| 2672 |
Objects can contain other objects, called *subobjects*. A subobject can
|
| 2673 |
be a *member subobject* [[class.mem]], a *base class subobject*
|
| 2674 |
[[class.derived]], or an array element. An object that is not a
|
| 2675 |
subobject of any other object is called a *complete object*. If an
|
|
@@ -2687,13 +2594,14 @@ another object *e* of type “array of N `unsigned char`” or of type
|
|
| 2687 |
“array of N `std::byte`” [[cstddef.syn]], that array *provides storage*
|
| 2688 |
for the created object if:
|
| 2689 |
|
| 2690 |
- the lifetime of *e* has begun and not ended, and
|
| 2691 |
- the storage for the new object fits entirely within *e*, and
|
| 2692 |
-
- there is no
|
|
|
|
| 2693 |
|
| 2694 |
-
[*Note
|
| 2695 |
another object, the lifetime of that object ends because its storage was
|
| 2696 |
reused [[basic.life]]. — *end note*]
|
| 2697 |
|
| 2698 |
[*Example 1*:
|
| 2699 |
|
|
@@ -2734,12 +2642,12 @@ of* `x`, determined as follows:
|
|
| 2734 |
- If `x` is a complete object, then the complete object of `x` is
|
| 2735 |
itself.
|
| 2736 |
- Otherwise, the complete object of `x` is the complete object of the
|
| 2737 |
(unique) object that contains `x`.
|
| 2738 |
|
| 2739 |
-
If a complete object, a
|
| 2740 |
-
|
| 2741 |
distinguish it from the class type of any base class subobject; an
|
| 2742 |
object of a most derived class type or of a non-class type is called a
|
| 2743 |
*most derived object*.
|
| 2744 |
|
| 2745 |
A *potentially-overlapping subobject* is either:
|
|
@@ -2752,27 +2660,28 @@ An object has nonzero size if it
|
|
| 2752 |
|
| 2753 |
- is not a potentially-overlapping subobject, or
|
| 2754 |
- is not of class type, or
|
| 2755 |
- is of a class type with virtual member functions or virtual base
|
| 2756 |
classes, or
|
| 2757 |
-
- has subobjects of nonzero size or bit-fields of nonzero
|
|
|
|
| 2758 |
|
| 2759 |
Otherwise, if the object is a base class subobject of a standard-layout
|
| 2760 |
class type with no non-static data members, it has zero size. Otherwise,
|
| 2761 |
the circumstances under which the object has zero size are
|
| 2762 |
*implementation-defined*. Unless it is a bit-field [[class.bit]], an
|
| 2763 |
object with nonzero size shall occupy one or more bytes of storage,
|
| 2764 |
including every byte that is occupied in full or in part by any of its
|
| 2765 |
subobjects. An object of trivially copyable or standard-layout type
|
| 2766 |
-
[[basic.types]] shall occupy contiguous bytes of storage.
|
| 2767 |
|
| 2768 |
Unless an object is a bit-field or a subobject of zero size, the address
|
| 2769 |
of that object is the address of the first byte it occupies. Two objects
|
| 2770 |
with overlapping lifetimes that are not bit-fields may have the same
|
| 2771 |
address if one is nested within the other, or if at least one is a
|
| 2772 |
subobject of zero size and they are of different types; otherwise, they
|
| 2773 |
-
have distinct addresses and occupy disjoint bytes of storage.[^
|
| 2774 |
|
| 2775 |
[*Example 2*:
|
| 2776 |
|
| 2777 |
``` cpp
|
| 2778 |
static const char test1 = 'x';
|
|
@@ -2788,18 +2697,18 @@ subobject.
|
|
| 2788 |
|
| 2789 |
Some operations are described as *implicitly creating objects* within a
|
| 2790 |
specified region of storage. For each operation that is specified as
|
| 2791 |
implicitly creating objects, that operation implicitly creates and
|
| 2792 |
starts the lifetime of zero or more objects of implicit-lifetime types
|
| 2793 |
-
[[basic.types]] in its specified region of storage if doing so
|
| 2794 |
-
result in the program having defined behavior. If no such set of
|
| 2795 |
-
would give the program defined behavior, the behavior of the
|
| 2796 |
-
undefined. If multiple such sets of objects would give the
|
| 2797 |
-
defined behavior, it is unspecified which such set of objects is
|
| 2798 |
created.
|
| 2799 |
|
| 2800 |
-
[*Note
|
| 2801 |
such objects that are not themselves of implicit-lifetime
|
| 2802 |
types. — *end note*]
|
| 2803 |
|
| 2804 |
Further, after implicitly creating objects within a specified region of
|
| 2805 |
storage, some operations are described as producing a pointer to a
|
|
@@ -2830,44 +2739,44 @@ X *make_x() {
|
|
| 2830 |
}
|
| 2831 |
```
|
| 2832 |
|
| 2833 |
— *end example*]
|
| 2834 |
|
| 2835 |
-
An operation that begins the lifetime of an array of `char`
|
| 2836 |
-
`
|
| 2837 |
-
|
| 2838 |
|
| 2839 |
-
[*Note
|
| 2840 |
objects. — *end note*]
|
| 2841 |
|
| 2842 |
Any implicit or explicit invocation of a function named `operator new`
|
| 2843 |
or `operator new[]` implicitly creates objects in the returned region of
|
| 2844 |
storage and returns a pointer to a suitable created object.
|
| 2845 |
|
| 2846 |
-
[*Note
|
| 2847 |
-
objects
|
| 2848 |
-
[[bit.cast]]
|
| 2849 |
|
| 2850 |
### Lifetime <a id="basic.life">[[basic.life]]</a>
|
| 2851 |
|
| 2852 |
The *lifetime* of an object or reference is a runtime property of the
|
| 2853 |
object or reference. A variable is said to have *vacuous initialization*
|
| 2854 |
if it is default-initialized and, if it is of class type or a (possibly
|
| 2855 |
-
|
| 2856 |
constructor. The lifetime of an object of type `T` begins when:
|
| 2857 |
|
| 2858 |
- storage with the proper alignment and size for type `T` is obtained,
|
| 2859 |
and
|
| 2860 |
- its initialization (if any) is complete (including vacuous
|
| 2861 |
initialization) [[dcl.init]],
|
| 2862 |
|
| 2863 |
except that if the object is a union member or subobject thereof, its
|
| 2864 |
lifetime only begins if that union member is the initialized member in
|
| 2865 |
-
the union
|
| 2866 |
-
[[class.union]]
|
| 2867 |
-
[[allocator.members]]. The lifetime of an object
|
| 2868 |
-
when:
|
| 2869 |
|
| 2870 |
- if `T` is a non-class type, the object is destroyed, or
|
| 2871 |
- if `T` is a class type, the destructor call starts, or
|
| 2872 |
- the storage which the object occupies is released, or is reused by an
|
| 2873 |
object that is not nested within *o* [[intro.object]].
|
|
@@ -2882,43 +2791,44 @@ member subobjects. — *end note*]
|
|
| 2882 |
The properties ascribed to objects and references throughout this
|
| 2883 |
document apply for a given object or reference only during its lifetime.
|
| 2884 |
|
| 2885 |
[*Note 2*: In particular, before the lifetime of an object starts and
|
| 2886 |
after its lifetime ends there are significant restrictions on the use of
|
| 2887 |
-
the object, as described below, in [[class.base.init]] and in
|
| 2888 |
[[class.cdtor]]. Also, the behavior of an object under construction and
|
| 2889 |
-
destruction
|
| 2890 |
-
|
| 2891 |
-
|
| 2892 |
-
|
| 2893 |
-
|
| 2894 |
-
A program may end the lifetime of
|
| 2895 |
-
|
| 2896 |
-
|
| 2897 |
-
|
| 2898 |
-
|
| 2899 |
-
|
| 2900 |
-
|
| 2901 |
-
|
| 2902 |
-
|
| 2903 |
-
behavior
|
|
|
|
| 2904 |
|
| 2905 |
Before the lifetime of an object has started but after the storage which
|
| 2906 |
-
the object will occupy has been allocated[^
|
| 2907 |
-
an object has ended and before the storage which the object occupied is
|
| 2908 |
-
reused or released, any pointer that represents the address of the
|
| 2909 |
-
storage location where the object will be or was located may be used but
|
| 2910 |
-
only in limited ways. For an object under construction or destruction,
|
| 2911 |
-
see [[class.cdtor]]. Otherwise, such a pointer refers to allocated
|
| 2912 |
-
storage [[basic.stc.dynamic.allocation]], and using the pointer as if
|
| 2913 |
-
the pointer were of type `void*` is well-defined. Indirection through
|
| 2914 |
-
such a pointer is permitted but the resulting lvalue may only be used in
|
| 2915 |
-
limited ways, as described below. The program has undefined behavior if:
|
| 2916 |
|
| 2917 |
-
|
| 2918 |
-
|
| 2919 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2920 |
- the pointer is used to access a non-static data member or call a
|
| 2921 |
non-static member function of the object, or
|
| 2922 |
- the pointer is implicitly converted [[conv.ptr]] to a pointer to a
|
| 2923 |
virtual base class, or
|
| 2924 |
- the pointer is used as the operand of a `static_cast`
|
|
@@ -2950,12 +2860,12 @@ void B::mutate() {
|
|
| 2950 |
|
| 2951 |
void g() {
|
| 2952 |
void* p = std::malloc(sizeof(D1) + sizeof(D2));
|
| 2953 |
B* pb = new (p) D1;
|
| 2954 |
pb->mutate();
|
| 2955 |
-
*pb; // OK
|
| 2956 |
-
void* q = pb; // OK
|
| 2957 |
pb->f(); // undefined behavior: lifetime of *pb has ended
|
| 2958 |
}
|
| 2959 |
```
|
| 2960 |
|
| 2961 |
— *end example*]
|
|
@@ -2990,11 +2900,11 @@ object o₁ is *transparently replaceable* by an object o₂ if:
|
|
| 2990 |
|
| 2991 |
- the storage that o₂ occupies exactly overlays the storage that o₁
|
| 2992 |
occupied, and
|
| 2993 |
- o₁ and o₂ are of the same type (ignoring the top-level cv-qualifiers),
|
| 2994 |
and
|
| 2995 |
-
- o₁ is not a
|
| 2996 |
- neither o₁ nor o₂ is a potentially-overlapping subobject
|
| 2997 |
[[intro.object]], and
|
| 2998 |
- either o₁ and o₂ are both complete objects, or o₁ and o₂ are direct
|
| 2999 |
subobjects of objects p₁ and p₂, respectively, and p₁ is transparently
|
| 3000 |
replaceable by p₂.
|
|
@@ -3023,21 +2933,23 @@ c1 = c2; // well-defined
|
|
| 3023 |
c1.f(); // well-defined; c1 refers to a new object of type C
|
| 3024 |
```
|
| 3025 |
|
| 3026 |
— *end example*]
|
| 3027 |
|
| 3028 |
-
[*Note
|
| 3029 |
can be obtained from a pointer that represents the address of its
|
| 3030 |
storage by calling `std::launder` [[ptr.launder]]. — *end note*]
|
| 3031 |
|
| 3032 |
If a program ends the lifetime of an object of type `T` with static
|
| 3033 |
[[basic.stc.static]], thread [[basic.stc.thread]], or automatic
|
| 3034 |
[[basic.stc.auto]] storage duration and if `T` has a non-trivial
|
| 3035 |
-
destructor,[^
|
| 3036 |
-
|
| 3037 |
-
|
| 3038 |
-
|
|
|
|
|
|
|
| 3039 |
|
| 3040 |
[*Example 3*:
|
| 3041 |
|
| 3042 |
``` cpp
|
| 3043 |
class T { };
|
|
@@ -3051,11 +2963,11 @@ void h() {
|
|
| 3051 |
} // undefined behavior at block exit
|
| 3052 |
```
|
| 3053 |
|
| 3054 |
— *end example*]
|
| 3055 |
|
| 3056 |
-
Creating a new object within the storage that a const complete object
|
| 3057 |
with static, thread, or automatic storage duration occupies, or within
|
| 3058 |
the storage that such a const object used to occupy before its lifetime
|
| 3059 |
ended, results in undefined behavior.
|
| 3060 |
|
| 3061 |
[*Example 4*:
|
|
@@ -3077,11 +2989,11 @@ void h() {
|
|
| 3077 |
— *end example*]
|
| 3078 |
|
| 3079 |
In this subclause, “before” and “after” refer to the “happens before”
|
| 3080 |
relation [[intro.multithread]].
|
| 3081 |
|
| 3082 |
-
[*Note
|
| 3083 |
being constructed in one thread is referenced from another thread
|
| 3084 |
without adequate synchronization. — *end note*]
|
| 3085 |
|
| 3086 |
### Indeterminate values <a id="basic.indet">[[basic.indet]]</a>
|
| 3087 |
|
|
@@ -3100,13 +3012,13 @@ undefined except in the following cases:
|
|
| 3100 |
[[basic.fundamental]] or `std::byte` type [[cstddef.syn]] is produced
|
| 3101 |
by the evaluation of:
|
| 3102 |
- the second or third operand of a conditional expression
|
| 3103 |
[[expr.cond]],
|
| 3104 |
- the right operand of a comma expression [[expr.comma]],
|
| 3105 |
-
- the operand of a cast or conversion
|
| 3106 |
-
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]
|
| 3107 |
-
unsigned ordinary character type or `std::byte` type
|
| 3108 |
[[cstddef.syn]], or
|
| 3109 |
- a discarded-value expression [[expr.context]],
|
| 3110 |
|
| 3111 |
then the result of the operation is an indeterminate value.
|
| 3112 |
- If an indeterminate value of unsigned ordinary character type or
|
|
@@ -3137,10 +3049,12 @@ int f(bool b) {
|
|
| 3137 |
|
| 3138 |
— *end example*]
|
| 3139 |
|
| 3140 |
### Storage duration <a id="basic.stc">[[basic.stc]]</a>
|
| 3141 |
|
|
|
|
|
|
|
| 3142 |
The *storage duration* is the property of an object that defines the
|
| 3143 |
minimum potential lifetime of the storage containing the object. The
|
| 3144 |
storage duration is determined by the construct used to create the
|
| 3145 |
object and is one of the following:
|
| 3146 |
|
|
@@ -3160,33 +3074,35 @@ When the end of the duration of a region of storage is reached, the
|
|
| 3160 |
values of all pointers representing the address of any part of that
|
| 3161 |
region of storage become invalid pointer values [[basic.compound]].
|
| 3162 |
Indirection through an invalid pointer value and passing an invalid
|
| 3163 |
pointer value to a deallocation function have undefined behavior. Any
|
| 3164 |
other use of an invalid pointer value has *implementation-defined*
|
| 3165 |
-
behavior.[^
|
| 3166 |
|
| 3167 |
#### Static storage duration <a id="basic.stc.static">[[basic.stc.static]]</a>
|
| 3168 |
|
| 3169 |
-
All variables which
|
| 3170 |
-
|
| 3171 |
-
|
| 3172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3173 |
|
| 3174 |
If a variable with static storage duration has initialization or a
|
| 3175 |
destructor with side effects, it shall not be eliminated even if it
|
| 3176 |
appears to be unused, except that a class object or its copy/move may be
|
| 3177 |
eliminated as specified in [[class.copy.elision]].
|
| 3178 |
|
| 3179 |
-
The keyword `static` can be used to declare a
|
| 3180 |
-
storage duration
|
| 3181 |
-
|
| 3182 |
-
|
| 3183 |
-
|
| 3184 |
-
|
| 3185 |
-
|
| 3186 |
-
The keyword `static` applied to a class data member in a class
|
| 3187 |
-
definition gives the data member static storage duration.
|
| 3188 |
|
| 3189 |
#### Thread storage duration <a id="basic.stc.thread">[[basic.stc.thread]]</a>
|
| 3190 |
|
| 3191 |
All variables declared with the `thread_local` keyword have
|
| 3192 |
*thread storage duration*. The storage for these entities lasts for the
|
|
@@ -3199,13 +3115,14 @@ specified in [[basic.start.static]], [[basic.start.dynamic]], and
|
|
| 3199 |
[[stmt.dcl]] and, if constructed, is destroyed on thread exit
|
| 3200 |
[[basic.start.term]]. — *end note*]
|
| 3201 |
|
| 3202 |
#### Automatic storage duration <a id="basic.stc.auto">[[basic.stc.auto]]</a>
|
| 3203 |
|
| 3204 |
-
|
| 3205 |
-
|
| 3206 |
-
|
|
|
|
| 3207 |
|
| 3208 |
[*Note 1*: These variables are initialized and destroyed as described
|
| 3209 |
in [[stmt.dcl]]. — *end note*]
|
| 3210 |
|
| 3211 |
If a variable with automatic storage duration has initialization or a
|
|
@@ -3214,30 +3131,33 @@ before the end of its block nor eliminate it as an optimization, even if
|
|
| 3214 |
it appears to be unused, except that a class object or its copy/move may
|
| 3215 |
be eliminated as specified in [[class.copy.elision]].
|
| 3216 |
|
| 3217 |
#### Dynamic storage duration <a id="basic.stc.dynamic">[[basic.stc.dynamic]]</a>
|
| 3218 |
|
|
|
|
|
|
|
| 3219 |
Objects can be created dynamically during program execution
|
| 3220 |
[[intro.execution]], using *new-expression*s [[expr.new]], and destroyed
|
| 3221 |
using *delete-expression*s [[expr.delete]]. A C++ implementation
|
| 3222 |
provides access to, and management of, dynamic storage via the global
|
| 3223 |
-
*allocation functions* `operator new` and `operator
|
| 3224 |
-
|
| 3225 |
-
|
| 3226 |
|
| 3227 |
[*Note 1*: The non-allocating forms described in
|
| 3228 |
[[new.delete.placement]] do not perform allocation or
|
| 3229 |
deallocation. — *end note*]
|
| 3230 |
|
| 3231 |
The library provides default definitions for the global allocation and
|
| 3232 |
deallocation functions. Some global allocation and deallocation
|
| 3233 |
-
functions are replaceable [[new.delete]]
|
| 3234 |
-
|
| 3235 |
-
|
| 3236 |
-
|
| 3237 |
-
|
| 3238 |
-
|
|
|
|
| 3239 |
|
| 3240 |
``` cpp
|
| 3241 |
[[nodiscard]] void* operator new(std::size_t);
|
| 3242 |
[[nodiscard]] void* operator new(std::size_t, std::align_val_t);
|
| 3243 |
|
|
@@ -3253,22 +3173,24 @@ void operator delete[](void*) noexcept;
|
|
| 3253 |
void operator delete[](void*, std::size_t) noexcept;
|
| 3254 |
void operator delete[](void*, std::align_val_t) noexcept;
|
| 3255 |
void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
|
| 3256 |
```
|
| 3257 |
|
| 3258 |
-
These implicit declarations introduce only the function names
|
| 3259 |
-
`new`, `operator
|
| 3260 |
-
`delete[]`.
|
| 3261 |
|
| 3262 |
[*Note 2*: The implicit declarations do not introduce the names `std`,
|
| 3263 |
`std::size_t`, `std::align_val_t`, or any other names that the library
|
| 3264 |
uses to declare these names. Thus, a *new-expression*,
|
| 3265 |
*delete-expression*, or function call that refers to one of these
|
| 3266 |
-
functions without importing or including the header `<new>`
|
| 3267 |
-
well-formed. However, referring
|
| 3268 |
-
`std::align_val_t` is ill-formed unless
|
| 3269 |
-
|
|
|
|
|
|
|
| 3270 |
|
| 3271 |
Allocation and/or deallocation functions may also be declared and
|
| 3272 |
defined for any class [[class.free]].
|
| 3273 |
|
| 3274 |
If the behavior of an allocation or deallocation function does not
|
|
@@ -3276,22 +3198,21 @@ satisfy the semantic constraints specified in
|
|
| 3276 |
[[basic.stc.dynamic.allocation]] and
|
| 3277 |
[[basic.stc.dynamic.deallocation]], the behavior is undefined.
|
| 3278 |
|
| 3279 |
##### Allocation functions <a id="basic.stc.dynamic.allocation">[[basic.stc.dynamic.allocation]]</a>
|
| 3280 |
|
| 3281 |
-
An allocation function
|
| 3282 |
-
|
| 3283 |
-
|
| 3284 |
-
|
| 3285 |
-
|
| 3286 |
-
|
| 3287 |
-
|
| 3288 |
-
|
| 3289 |
-
template shall
|
| 3290 |
-
|
| 3291 |
-
|
| 3292 |
-
two or more parameters.
|
| 3293 |
|
| 3294 |
An allocation function attempts to allocate the requested amount of
|
| 3295 |
storage. If it is successful, it returns the address of the start of a
|
| 3296 |
block of storage whose length in bytes is at least as large as the
|
| 3297 |
requested size. The order, contiguity, and initial value of storage
|
|
@@ -3303,11 +3224,11 @@ from any previously returned value `p1`, unless that value `p1` was
|
|
| 3303 |
subsequently passed to a replaceable deallocation function. Furthermore,
|
| 3304 |
for the library allocation functions in [[new.delete.single]] and
|
| 3305 |
[[new.delete.array]], `p0` represents the address of a block of storage
|
| 3306 |
disjoint from the storage for any other object accessible to the caller.
|
| 3307 |
The effect of indirecting through a pointer returned from a request for
|
| 3308 |
-
zero size is undefined.[^
|
| 3309 |
|
| 3310 |
For an allocation function other than a reserved placement allocation
|
| 3311 |
function [[new.delete.placement]], the pointer returned on a successful
|
| 3312 |
call shall represent the address of storage that is aligned as follows:
|
| 3313 |
|
|
@@ -3347,14 +3268,12 @@ duration [[basic.stc.thread]], for objects of type `std::type_info`
|
|
| 3347 |
[[expr.typeid]], or for an exception object
|
| 3348 |
[[except.throw]]. — *end note*]
|
| 3349 |
|
| 3350 |
##### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
|
| 3351 |
|
| 3352 |
-
|
| 3353 |
-
|
| 3354 |
-
declared in a namespace scope other than global scope or declared static
|
| 3355 |
-
in global scope.
|
| 3356 |
|
| 3357 |
A deallocation function is a *destroying operator delete* if it has at
|
| 3358 |
least two parameters and its second parameter is of type
|
| 3359 |
`std::destroying_delete_t`. A destroying operator delete shall be a
|
| 3360 |
class member function named `operator delete`.
|
|
@@ -3368,11 +3287,11 @@ first parameter shall be `C*`; otherwise, the type of its first
|
|
| 3368 |
parameter shall be `void*`. A deallocation function may have more than
|
| 3369 |
one parameter. A *usual deallocation function* is a deallocation
|
| 3370 |
function whose parameters after the first are
|
| 3371 |
|
| 3372 |
- optionally, a parameter of type `std::destroying_delete_t`, then
|
| 3373 |
-
- optionally, a parameter of type `std::size_t`
|
| 3374 |
- optionally, a parameter of type `std::align_val_t`.
|
| 3375 |
|
| 3376 |
A destroying operator delete shall be a usual deallocation function. A
|
| 3377 |
deallocation function may be an instance of a function template. Neither
|
| 3378 |
the first parameter nor the return type shall depend on a template
|
|
@@ -3389,95 +3308,30 @@ has no effect.
|
|
| 3389 |
If the argument given to a deallocation function in the standard library
|
| 3390 |
is a pointer that is not the null pointer value [[basic.compound]], the
|
| 3391 |
deallocation function shall deallocate the storage referenced by the
|
| 3392 |
pointer, ending the duration of the region of storage.
|
| 3393 |
|
| 3394 |
-
##### Safely-derived pointers <a id="basic.stc.dynamic.safety">[[basic.stc.dynamic.safety]]</a>
|
| 3395 |
-
|
| 3396 |
-
A *traceable pointer object* is
|
| 3397 |
-
|
| 3398 |
-
- an object of an object pointer type [[basic.compound]], or
|
| 3399 |
-
- an object of an integral type that is at least as large as
|
| 3400 |
-
`std::intptr_t`, or
|
| 3401 |
-
- a sequence of elements in an array of narrow character type
|
| 3402 |
-
[[basic.fundamental]], where the size and alignment of the sequence
|
| 3403 |
-
match those of some object pointer type.
|
| 3404 |
-
|
| 3405 |
-
A pointer value is a *safely-derived pointer* to an object with dynamic
|
| 3406 |
-
storage duration only if the pointer value has an object pointer type
|
| 3407 |
-
and is one of the following:
|
| 3408 |
-
|
| 3409 |
-
- the value returned by a call to the C++ standard library
|
| 3410 |
-
implementation of `::operator new(std::{}size_t)` or
|
| 3411 |
-
`::operator new(std::size_t, std::align_val_t)` ;[^16]
|
| 3412 |
-
- the result of taking the address of an object (or one of its
|
| 3413 |
-
subobjects) designated by an lvalue resulting from indirection through
|
| 3414 |
-
a safely-derived pointer value;
|
| 3415 |
-
- the result of well-defined pointer arithmetic [[expr.add]] using a
|
| 3416 |
-
safely-derived pointer value;
|
| 3417 |
-
- the result of a well-defined pointer conversion ([[conv.ptr]],
|
| 3418 |
-
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]) of a
|
| 3419 |
-
safely-derived pointer value;
|
| 3420 |
-
- the result of a `reinterpret_cast` of a safely-derived pointer value;
|
| 3421 |
-
- the result of a `reinterpret_cast` of an integer representation of a
|
| 3422 |
-
safely-derived pointer value;
|
| 3423 |
-
- the value of an object whose value was copied from a traceable pointer
|
| 3424 |
-
object, where at the time of the copy the source object contained a
|
| 3425 |
-
copy of a safely-derived pointer value.
|
| 3426 |
-
|
| 3427 |
-
An integer value is an *integer representation of a safely-derived
|
| 3428 |
-
pointer* only if its type is at least as large as `std::intptr_t` and it
|
| 3429 |
-
is one of the following:
|
| 3430 |
-
|
| 3431 |
-
- the result of a `reinterpret_cast` of a safely-derived pointer value;
|
| 3432 |
-
- the result of a valid conversion of an integer representation of a
|
| 3433 |
-
safely-derived pointer value;
|
| 3434 |
-
- the value of an object whose value was copied from a traceable pointer
|
| 3435 |
-
object, where at the time of the copy the source object contained an
|
| 3436 |
-
integer representation of a safely-derived pointer value;
|
| 3437 |
-
- the result of an additive or bitwise operation, one of whose operands
|
| 3438 |
-
is an integer representation of a safely-derived pointer value `P`, if
|
| 3439 |
-
that result converted by `reinterpret_cast<void*>` would compare equal
|
| 3440 |
-
to a safely-derived pointer computable from
|
| 3441 |
-
`reinterpret_cast<void*>(P)`.
|
| 3442 |
-
|
| 3443 |
-
An implementation may have *relaxed pointer safety*, in which case the
|
| 3444 |
-
validity of a pointer value does not depend on whether it is a
|
| 3445 |
-
safely-derived pointer value. Alternatively, an implementation may have
|
| 3446 |
-
*strict pointer safety*, in which case a pointer value referring to an
|
| 3447 |
-
object with dynamic storage duration that is not a safely-derived
|
| 3448 |
-
pointer value is an invalid pointer value unless the referenced complete
|
| 3449 |
-
object has previously been declared reachable [[util.dynamic.safety]].
|
| 3450 |
-
|
| 3451 |
-
[*Note 6*: The effect of using an invalid pointer value (including
|
| 3452 |
-
passing it to a deallocation function) is undefined, see [[basic.stc]].
|
| 3453 |
-
This is true even if the unsafely-derived pointer value might compare
|
| 3454 |
-
equal to some safely-derived pointer value. — *end note*]
|
| 3455 |
-
|
| 3456 |
-
It is *implementation-defined* whether an implementation has relaxed or
|
| 3457 |
-
strict pointer safety.
|
| 3458 |
-
|
| 3459 |
#### Duration of subobjects <a id="basic.stc.inherit">[[basic.stc.inherit]]</a>
|
| 3460 |
|
| 3461 |
The storage duration of subobjects and reference members is that of
|
| 3462 |
their complete object [[intro.object]].
|
| 3463 |
|
| 3464 |
### Alignment <a id="basic.align">[[basic.align]]</a>
|
| 3465 |
|
| 3466 |
-
Object types have *alignment requirements*
|
| 3467 |
-
[[basic.compound]]
|
| 3468 |
-
an object of that type may be allocated. An
|
| 3469 |
-
*implementation-defined* integer value representing
|
| 3470 |
-
between successive addresses at which a given object
|
| 3471 |
-
An object type imposes an alignment requirement on
|
| 3472 |
-
type; stricter alignment can be requested using the
|
| 3473 |
-
[[dcl.align]].
|
| 3474 |
|
| 3475 |
A *fundamental alignment* is represented by an alignment less than or
|
| 3476 |
equal to the greatest alignment supported by the implementation in all
|
| 3477 |
contexts, which is equal to `alignof(std::max_align_t)`
|
| 3478 |
-
[[support.types]]. The alignment required for a type
|
| 3479 |
when it is used as the type of a complete object and when it is used as
|
| 3480 |
the type of a subobject.
|
| 3481 |
|
| 3482 |
[*Example 1*:
|
| 3483 |
|
|
@@ -3535,57 +3389,55 @@ Comparing alignments is meaningful and provides the obvious results:
|
|
| 3535 |
- Two alignments are different when their numeric values are not equal.
|
| 3536 |
- When an alignment is larger than another it represents a stricter
|
| 3537 |
alignment.
|
| 3538 |
|
| 3539 |
[*Note 3*: The runtime pointer alignment function [[ptr.align]] can be
|
| 3540 |
-
used to obtain an aligned pointer within a buffer;
|
| 3541 |
-
|
| 3542 |
-
|
| 3543 |
|
| 3544 |
If a request for a specific extended alignment in a specific context is
|
| 3545 |
not supported by an implementation, the program is ill-formed.
|
| 3546 |
|
| 3547 |
### Temporary objects <a id="class.temporary">[[class.temporary]]</a>
|
| 3548 |
|
| 3549 |
Temporary objects are created
|
| 3550 |
|
| 3551 |
- when a prvalue is converted to an xvalue [[conv.rval]],
|
| 3552 |
- when needed by the implementation to pass or return an object of
|
| 3553 |
-
trivially
|
| 3554 |
- when throwing an exception [[except.throw]]. \[*Note 1*: The lifetime
|
| 3555 |
of exception objects is described in [[except.throw]]. — *end note*]
|
| 3556 |
|
| 3557 |
Even when the creation of the temporary object is unevaluated
|
| 3558 |
-
[[expr.
|
| 3559 |
the temporary object had been created and later destroyed.
|
| 3560 |
|
| 3561 |
[*Note 2*: This includes accessibility [[class.access]] and whether it
|
| 3562 |
is deleted, for the constructor selected and for the destructor.
|
| 3563 |
However, in the special case of the operand of a *decltype-specifier*
|
| 3564 |
-
[[
|
| 3565 |
-
apply to such a prvalue. — *end note*]
|
| 3566 |
|
| 3567 |
The materialization of a temporary object is generally delayed as long
|
| 3568 |
as possible in order to avoid creating unnecessary temporary objects.
|
| 3569 |
|
| 3570 |
[*Note 3*:
|
| 3571 |
|
| 3572 |
Temporary objects are materialized:
|
| 3573 |
|
| 3574 |
-
- when binding a reference to a prvalue
|
| 3575 |
-
[[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]],
|
| 3576 |
-
|
| 3577 |
-
|
| 3578 |
-
[[expr.mptr.oper]]),
|
| 3579 |
- when performing an array-to-pointer conversion or subscripting on an
|
| 3580 |
-
array prvalue
|
| 3581 |
- when initializing an object of type `std::initializer_list<T>` from a
|
| 3582 |
*braced-init-list* [[dcl.init.list]],
|
| 3583 |
-
- for certain unevaluated operands
|
| 3584 |
-
and
|
| 3585 |
- when a prvalue that has type other than cv `void` appears as a
|
| 3586 |
-
discarded-value expression [[expr.
|
| 3587 |
|
| 3588 |
— *end note*]
|
| 3589 |
|
| 3590 |
[*Example 1*:
|
| 3591 |
|
|
@@ -3642,38 +3494,38 @@ object).
|
|
| 3642 |
|
| 3643 |
[*Note 4*: This latitude is granted to allow objects of class type to
|
| 3644 |
be passed to or returned from functions in registers. — *end note*]
|
| 3645 |
|
| 3646 |
When an implementation introduces a temporary object of a class that has
|
| 3647 |
-
a non-trivial constructor
|
| 3648 |
-
|
| 3649 |
-
|
| 3650 |
-
|
| 3651 |
-
|
| 3652 |
-
|
| 3653 |
-
|
| 3654 |
-
|
| 3655 |
-
|
| 3656 |
-
|
| 3657 |
|
| 3658 |
-
There are
|
| 3659 |
different point than the end of the full-expression. The first context
|
| 3660 |
is when a default constructor is called to initialize an element of an
|
| 3661 |
array with no corresponding initializer [[dcl.init]]. The second context
|
| 3662 |
is when a copy constructor is called to copy an element of an array
|
| 3663 |
-
while the entire array is copied
|
| 3664 |
-
[[class.copy.ctor]]
|
| 3665 |
-
default arguments, the destruction of
|
| 3666 |
-
default argument is sequenced before the
|
| 3667 |
-
element, if any.
|
| 3668 |
|
| 3669 |
-
The third context is when a reference
|
| 3670 |
-
|
| 3671 |
-
temporary object
|
| 3672 |
-
|
| 3673 |
-
|
| 3674 |
-
following:
|
| 3675 |
|
| 3676 |
- a temporary materialization conversion [[conv.rval]],
|
| 3677 |
- `(` *expression* `)`, where *expression* is one of these expressions,
|
| 3678 |
- subscripting [[expr.sub]] of an array operand, where that operand is
|
| 3679 |
one of these expressions,
|
|
@@ -3712,11 +3564,11 @@ int&& c = cond ? id<int[3]>{1, 2, 3}[i] : static_cast<int&&>(0);
|
|
| 3712 |
|
| 3713 |
— *end example*]
|
| 3714 |
|
| 3715 |
[*Note 5*:
|
| 3716 |
|
| 3717 |
-
An explicit type conversion
|
| 3718 |
interpreted as a sequence of elementary casts, covered above.
|
| 3719 |
|
| 3720 |
[*Example 3*:
|
| 3721 |
|
| 3722 |
``` cpp
|
|
@@ -3759,35 +3611,43 @@ The exceptions to this lifetime rule are:
|
|
| 3759 |
`return` statement [[stmt.return]] is not extended; the temporary is
|
| 3760 |
destroyed at the end of the full-expression in the `return` statement.
|
| 3761 |
- A temporary bound to a reference in a *new-initializer* [[expr.new]]
|
| 3762 |
persists until the completion of the full-expression containing the
|
| 3763 |
*new-initializer*.
|
| 3764 |
-
\[*Note 7*: This
|
| 3765 |
\[*Example 5*:
|
| 3766 |
``` cpp
|
| 3767 |
struct S { int mi; const std::pair<int,int>& mp; };
|
| 3768 |
S a { 1, {2,3} };
|
| 3769 |
S* p = new S{ 1, {2,3} }; // creates dangling reference
|
| 3770 |
```
|
| 3771 |
|
| 3772 |
— *end example*]
|
| 3773 |
|
| 3774 |
-
The
|
| 3775 |
-
|
| 3776 |
-
|
| 3777 |
-
|
| 3778 |
-
|
| 3779 |
-
|
| 3780 |
-
|
| 3781 |
-
|
| 3782 |
-
|
| 3783 |
-
|
| 3784 |
-
|
| 3785 |
-
|
| 3786 |
-
|
| 3787 |
-
|
| 3788 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3789 |
|
| 3790 |
[*Example 6*:
|
| 3791 |
|
| 3792 |
``` cpp
|
| 3793 |
struct S {
|
|
@@ -3821,23 +3681,26 @@ destroyed before `obj1`.
|
|
| 3821 |
|
| 3822 |
— *end example*]
|
| 3823 |
|
| 3824 |
## Types <a id="basic.types">[[basic.types]]</a>
|
| 3825 |
|
|
|
|
|
|
|
| 3826 |
[*Note 1*: [[basic.types]] and the subclauses thereof impose
|
| 3827 |
requirements on implementations regarding the representation of types.
|
| 3828 |
There are two kinds of types: fundamental types and compound types.
|
| 3829 |
Types describe objects [[intro.object]], references [[dcl.ref]], or
|
| 3830 |
functions [[dcl.fct]]. — *end note*]
|
| 3831 |
|
| 3832 |
For any object (other than a potentially-overlapping subobject) of
|
| 3833 |
trivially copyable type `T`, whether or not the object holds a valid
|
| 3834 |
value of type `T`, the underlying bytes [[intro.memory]] making up the
|
| 3835 |
object can be copied into an array of `char`, `unsigned char`, or
|
| 3836 |
-
`std::byte` [[cstddef.syn]].
|
| 3837 |
-
|
| 3838 |
-
|
|
|
|
| 3839 |
|
| 3840 |
[*Example 1*:
|
| 3841 |
|
| 3842 |
``` cpp
|
| 3843 |
constexpr std::size_t N = sizeof(T);
|
|
@@ -3847,15 +3710,16 @@ std::memcpy(buf, &obj, N); // between these two calls to std::memcpy, obj m
|
|
| 3847 |
std::memcpy(&obj, buf, N); // at this point, each subobject of obj of scalar type holds its original value
|
| 3848 |
```
|
| 3849 |
|
| 3850 |
— *end example*]
|
| 3851 |
|
| 3852 |
-
For
|
| 3853 |
-
|
| 3854 |
-
|
| 3855 |
-
|
| 3856 |
-
|
|
|
|
| 3857 |
|
| 3858 |
[*Example 2*:
|
| 3859 |
|
| 3860 |
``` cpp
|
| 3861 |
T* t1p;
|
|
@@ -3874,32 +3738,38 @@ The *object representation* of an object of type `T` is the sequence of
|
|
| 3874 |
`T` is the set of bits that participate in representing a value of type
|
| 3875 |
`T`. Bits in the object representation that are not part of the value
|
| 3876 |
representation are *padding bits*. For trivially copyable types, the
|
| 3877 |
value representation is a set of bits in the object representation that
|
| 3878 |
determines a *value*, which is one discrete element of an
|
| 3879 |
-
*implementation-defined* set of values.[^
|
| 3880 |
|
| 3881 |
A class that has been declared but not defined, an enumeration type in
|
| 3882 |
certain contexts [[dcl.enum]], or an array of unknown bound or of
|
| 3883 |
-
incomplete element type, is an *incompletely-defined object type*.
|
|
|
|
| 3884 |
Incompletely-defined object types and cv `void` are *incomplete types*
|
| 3885 |
-
[[basic.fundamental]].
|
| 3886 |
-
incomplete type.
|
| 3887 |
|
| 3888 |
-
|
|
|
|
|
|
|
|
|
|
| 3889 |
translation unit and complete later on; the type “`class X`” is the same
|
| 3890 |
-
type at both points. The declared type of an array object
|
| 3891 |
array of incomplete class type and therefore incomplete; if the class
|
| 3892 |
type is completed later on in the translation unit, the array type
|
| 3893 |
becomes complete; the array type at those two points is the same type.
|
| 3894 |
-
The declared type of an array object
|
| 3895 |
and therefore be incomplete at one point in a translation unit and
|
| 3896 |
complete later on; the array types at those two points (“array of
|
| 3897 |
-
unknown bound of `T`” and “array of `N` `T`”) are different types.
|
| 3898 |
-
|
| 3899 |
-
|
| 3900 |
-
|
|
|
|
|
|
|
|
|
|
| 3901 |
|
| 3902 |
[*Example 3*:
|
| 3903 |
|
| 3904 |
``` cpp
|
| 3905 |
class X; // X is an incomplete type
|
|
@@ -3910,28 +3780,28 @@ UNKA* arrp; // arrp is a pointer to an incomplete type
|
|
| 3910 |
UNKA** arrpp;
|
| 3911 |
|
| 3912 |
void foo() {
|
| 3913 |
xp++; // error: X is incomplete
|
| 3914 |
arrp++; // error: incomplete type
|
| 3915 |
-
arrpp++; // OK
|
| 3916 |
}
|
| 3917 |
|
| 3918 |
struct X { int i; }; // now X is a complete type
|
| 3919 |
int arr[10]; // now the type of arr is complete
|
| 3920 |
|
| 3921 |
X x;
|
| 3922 |
void bar() {
|
| 3923 |
xp = &x; // OK; type is ``pointer to X''
|
| 3924 |
-
arrp = &arr; //
|
| 3925 |
-
xp++; // OK
|
| 3926 |
arrp++; // error: UNKA can't be completed
|
| 3927 |
}
|
| 3928 |
```
|
| 3929 |
|
| 3930 |
— *end example*]
|
| 3931 |
|
| 3932 |
-
[*Note
|
| 3933 |
contexts incomplete types are prohibited. — *end note*]
|
| 3934 |
|
| 3935 |
An *object type* is a (possibly cv-qualified) type that is not a
|
| 3936 |
function type, not a reference type, and not cv `void`.
|
| 3937 |
|
|
@@ -3957,27 +3827,30 @@ A type is a *literal type* if it is:
|
|
| 3957 |
- a reference type; or
|
| 3958 |
- an array of literal type; or
|
| 3959 |
- a possibly cv-qualified class type [[class]] that has all of the
|
| 3960 |
following properties:
|
| 3961 |
- it has a constexpr destructor [[dcl.constexpr]],
|
| 3962 |
-
-
|
| 3963 |
-
|
| 3964 |
-
|
| 3965 |
-
|
| 3966 |
-
|
| 3967 |
-
|
| 3968 |
-
non-
|
| 3969 |
-
|
| 3970 |
-
|
|
|
|
|
|
|
|
|
|
| 3971 |
|
| 3972 |
-
[*Note
|
| 3973 |
create an object within a constant expression. It is not a guarantee
|
| 3974 |
that it is possible to create such an object, nor is it a guarantee that
|
| 3975 |
any object of that type will be usable in a constant
|
| 3976 |
expression. — *end note*]
|
| 3977 |
|
| 3978 |
-
Two types *cv1* `T1` and *cv2* `T2` are *layout-compatible
|
| 3979 |
`T1` and `T2` are the same type, layout-compatible enumerations
|
| 3980 |
[[dcl.enum]], or layout-compatible standard-layout class types
|
| 3981 |
[[class.mem]].
|
| 3982 |
|
| 3983 |
### Fundamental types <a id="basic.fundamental">[[basic.fundamental]]</a>
|
|
@@ -4013,32 +3886,32 @@ arithmetic yields undefined behavior [[expr.pre]]. — *end note*]
|
|
| 4013 |
An unsigned integer type has the same object representation, value
|
| 4014 |
representation, and alignment requirements [[basic.align]] as the
|
| 4015 |
corresponding signed integer type. For each value x of a signed integer
|
| 4016 |
type, the value of the corresponding unsigned integer type congruent to
|
| 4017 |
x modulo 2ᴺ has the same value of corresponding bits in its value
|
| 4018 |
-
representation.[^
|
| 4019 |
|
| 4020 |
[*Example 1*: The value -1 of a signed integer type has the same
|
| 4021 |
representation as the largest value of the corresponding unsigned
|
| 4022 |
type. — *end example*]
|
| 4023 |
|
| 4024 |
**Table: Minimum width** <a id="basic.fundamental.width">[basic.fundamental.width]</a>
|
| 4025 |
|
| 4026 |
| Type | Minimum width $N$ |
|
| 4027 |
-
| ------------- | ----------------- |
|
| 4028 |
| `signed char` | 8 |
|
| 4029 |
-
| `short`
|
| 4030 |
| `int` | 16 |
|
| 4031 |
-
| `long`
|
| 4032 |
-
| `long long`
|
| 4033 |
|
| 4034 |
|
| 4035 |
The width of each signed integer type shall not be less than the values
|
| 4036 |
specified in [[basic.fundamental.width]]. The value representation of a
|
| 4037 |
signed or unsigned integer type comprises N bits, where N is the
|
| 4038 |
respective width. Each set of values for any padding bits
|
| 4039 |
-
[[basic.types]] in the object representation are alternative
|
| 4040 |
representations of the value specified by the value representation.
|
| 4041 |
|
| 4042 |
[*Note 3*: Padding bits have unspecified value, but cannot cause traps.
|
| 4043 |
In contrast, see ISO C 6.2.6.2. — *end note*]
|
| 4044 |
|
|
@@ -4065,30 +3938,25 @@ representation, alignment requirements [[basic.align]], and range of
|
|
| 4065 |
representable values as the underlying type. Further, each value has the
|
| 4066 |
same representation in both types.
|
| 4067 |
|
| 4068 |
Type `char` is a distinct type that has an *implementation-defined*
|
| 4069 |
choice of “`signed char`” or “`unsigned char`” as its underlying type.
|
| 4070 |
-
The
|
| 4071 |
-
|
| 4072 |
-
|
| 4073 |
-
character types
|
| 4074 |
-
|
| 4075 |
-
types, each possible bit pattern of the object representation represents
|
| 4076 |
-
a distinct value.
|
| 4077 |
|
| 4078 |
[*Note 5*: This requirement does not hold for other
|
| 4079 |
types. — *end note*]
|
| 4080 |
|
| 4081 |
[*Note 6*: A bit-field of narrow character type whose width is larger
|
| 4082 |
than the width of that type has padding bits; see
|
| 4083 |
-
[[basic.types]]. — *end note*]
|
| 4084 |
|
| 4085 |
Type `wchar_t` is a distinct type that has an *implementation-defined*
|
| 4086 |
-
signed or unsigned integer type as its underlying type.
|
| 4087 |
-
type `wchar_t` can represent distinct codes for all members of the
|
| 4088 |
-
largest extended character set specified among the supported locales
|
| 4089 |
-
[[locale]].
|
| 4090 |
|
| 4091 |
Type `char8_t` denotes a distinct type whose underlying type is
|
| 4092 |
`unsigned char`. Types `char16_t` and `char32_t` denote distinct types
|
| 4093 |
whose underlying types are `uint_least16_t` and `uint_least32_t`,
|
| 4094 |
respectively, in `<cstdint>`.
|
|
@@ -4099,58 +3967,140 @@ value representation, and alignment requirements as an
|
|
| 4099 |
`bool` are `true` and `false`.
|
| 4100 |
|
| 4101 |
[*Note 7*: There are no `signed`, `unsigned`, `short`, or `long bool`
|
| 4102 |
types or values. — *end note*]
|
| 4103 |
|
| 4104 |
-
|
| 4105 |
-
|
|
|
|
|
|
|
| 4106 |
types*. A synonym for integral type is *integer type*.
|
| 4107 |
|
| 4108 |
[*Note 8*: Enumerations [[dcl.enum]] are not integral; however,
|
| 4109 |
unscoped enumerations can be promoted to integral types as specified in
|
| 4110 |
[[conv.prom]]. — *end note*]
|
| 4111 |
|
| 4112 |
-
|
| 4113 |
-
|
| 4114 |
-
`float`, and the type `long double` provides at least
|
| 4115 |
-
as `double`. The set of values of the type `float` is
|
| 4116 |
-
set of values of the type `double`; the set of values of
|
| 4117 |
-
`double` is a subset of the set of values of the type
|
| 4118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4119 |
*implementation-defined*.
|
| 4120 |
|
| 4121 |
-
|
| 4122 |
-
|
| 4123 |
|
| 4124 |
-
|
| 4125 |
-
|
| 4126 |
-
|
| 4127 |
-
|
| 4128 |
|
| 4129 |
A type cv `void` is an incomplete type that cannot be completed; such a
|
| 4130 |
type has an empty set of values. It is used as the return type for
|
| 4131 |
functions that do not return a value. Any expression can be explicitly
|
| 4132 |
-
converted to type cv `void`
|
| 4133 |
-
[[expr.cast]]
|
| 4134 |
-
|
| 4135 |
-
[[
|
| 4136 |
-
|
| 4137 |
-
|
| 4138 |
-
|
| 4139 |
-
cv `void`.
|
| 4140 |
|
| 4141 |
A value of type `std::nullptr_t` is a null pointer constant
|
| 4142 |
[[conv.ptr]]. Such values participate in the pointer and the
|
| 4143 |
-
pointer-to-member conversions
|
| 4144 |
`sizeof(std::nullptr_t)` shall be equal to `sizeof(void*)`.
|
| 4145 |
|
| 4146 |
The types described in this subclause are called *fundamental types*.
|
| 4147 |
|
| 4148 |
-
[*Note
|
| 4149 |
types to have the same value representation, they are nevertheless
|
| 4150 |
different types. — *end note*]
|
| 4151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4152 |
### Compound types <a id="basic.compound">[[basic.compound]]</a>
|
| 4153 |
|
| 4154 |
Compound types can be constructed in the following ways:
|
| 4155 |
|
| 4156 |
- *arrays* of objects of a given type, [[dcl.array]];
|
|
@@ -4166,17 +4116,16 @@ Compound types can be constructed in the following ways:
|
|
| 4166 |
a set of types, enumerations and functions for manipulating these
|
| 4167 |
objects [[class.mfct]], and a set of restrictions on the access to
|
| 4168 |
these entities [[class.access]];
|
| 4169 |
- *unions*, which are classes capable of containing objects of different
|
| 4170 |
types at different times, [[class.union]];
|
| 4171 |
-
- *enumerations*, which comprise a set of named constant values
|
| 4172 |
-
distinct enumeration constitutes a different *enumerated type*,
|
| 4173 |
[[dcl.enum]];
|
| 4174 |
-
- *pointers to non-static class members*,
|
| 4175 |
-
|
| 4176 |
-
|
| 4177 |
-
|
| 4178 |
|
| 4179 |
These methods of constructing types can be applied recursively;
|
| 4180 |
restrictions are mentioned in [[dcl.meaning]]. Constructing a type such
|
| 4181 |
that the number of bytes in its object representation exceeds the
|
| 4182 |
maximum value representable in the type `std::size_t` [[support.types]]
|
|
@@ -4207,28 +4156,29 @@ allowed although there are restrictions on what can be done with them
|
|
| 4207 |
- the *null pointer value* for that type, or
|
| 4208 |
- an *invalid pointer value*.
|
| 4209 |
|
| 4210 |
A value of a pointer type that is a pointer to or past the end of an
|
| 4211 |
object *represents the address* of the first byte in memory
|
| 4212 |
-
[[intro.memory]] occupied by the object
|
| 4213 |
-
|
| 4214 |
-
|
|
|
|
| 4215 |
|
| 4216 |
[*Note 2*: A pointer past the end of an object [[expr.add]] is not
|
| 4217 |
-
considered to point to an unrelated object of the object’s type
|
| 4218 |
-
|
| 4219 |
-
the storage it denotes reaches the end of its storage
|
| 4220 |
-
[[basic.stc]]. — *end note*]
|
| 4221 |
|
| 4222 |
-
For purposes of pointer arithmetic [[expr.add]] and comparison
|
| 4223 |
-
[[expr.rel]], [[expr.eq]]
|
| 4224 |
-
|
| 4225 |
-
|
| 4226 |
-
|
| 4227 |
-
|
| 4228 |
-
|
| 4229 |
-
|
| 4230 |
[[basic.align]].
|
| 4231 |
|
| 4232 |
[*Note 3*: Pointers to over-aligned types [[basic.align]] have no
|
| 4233 |
special representation, but their range of valid values is restricted by
|
| 4234 |
the extended alignment requirement. — *end note*]
|
|
@@ -4237,13 +4187,12 @@ Two objects *a* and *b* are *pointer-interconvertible* if:
|
|
| 4237 |
|
| 4238 |
- they are the same object, or
|
| 4239 |
- one is a union object and the other is a non-static data member of
|
| 4240 |
that object [[class.union]], or
|
| 4241 |
- one is a standard-layout class object and the other is the first
|
| 4242 |
-
non-static data member of that object
|
| 4243 |
-
|
| 4244 |
-
[[class.mem]], or
|
| 4245 |
- there exists an object *c* such that *a* and *c* are
|
| 4246 |
pointer-interconvertible, and *c* and *b* are
|
| 4247 |
pointer-interconvertible.
|
| 4248 |
|
| 4249 |
If two objects are pointer-interconvertible, then they have the same
|
|
@@ -4252,48 +4201,53 @@ the other via a `reinterpret_cast` [[expr.reinterpret.cast]].
|
|
| 4252 |
|
| 4253 |
[*Note 4*: An array object and its first element are not
|
| 4254 |
pointer-interconvertible, even though they have the same
|
| 4255 |
address. — *end note*]
|
| 4256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4257 |
A pointer to cv `void` can be used to point to objects of unknown type.
|
| 4258 |
Such a pointer shall be able to hold any object pointer. An object of
|
| 4259 |
-
type cv `void
|
| 4260 |
-
requirements as cv `char
|
| 4261 |
|
| 4262 |
### CV-qualifiers <a id="basic.type.qualifier">[[basic.type.qualifier]]</a>
|
| 4263 |
|
| 4264 |
-
|
| 4265 |
-
|
| 4266 |
-
|
| 4267 |
-
|
| 4268 |
-
|
| 4269 |
-
|
| 4270 |
-
|
| 4271 |
-
|
| 4272 |
-
[[dcl.name]], or *new-type-id* [[expr.new]] when the object is created.
|
| 4273 |
|
| 4274 |
- A *const object* is an object of type `const T` or a non-mutable
|
| 4275 |
subobject of a const object.
|
| 4276 |
- A *volatile object* is an object of type `volatile T` or a subobject
|
| 4277 |
of a volatile object.
|
| 4278 |
- A *const volatile object* is an object of type `const volatile T`, a
|
| 4279 |
non-mutable subobject of a const volatile object, a const subobject of
|
| 4280 |
a volatile object, or a non-mutable volatile subobject of a const
|
| 4281 |
object.
|
| 4282 |
|
| 4283 |
-
|
| 4284 |
-
|
| 4285 |
-
|
|
|
|
| 4286 |
|
| 4287 |
Except for array types, a compound type [[basic.compound]] is not
|
| 4288 |
cv-qualified by the cv-qualifiers (if any) of the types from which it is
|
| 4289 |
compounded.
|
| 4290 |
|
| 4291 |
An array type whose elements are cv-qualified is also considered to have
|
| 4292 |
the same cv-qualifications as its elements.
|
| 4293 |
|
| 4294 |
-
[*Note
|
| 4295 |
underlying element type, so the notation “cv `T`”, where `T` is an array
|
| 4296 |
type, refers to an array whose elements are so-qualified
|
| 4297 |
[[dcl.array]]. — *end note*]
|
| 4298 |
|
| 4299 |
[*Example 1*:
|
|
@@ -4308,12 +4262,12 @@ const CA arr2 = { 0 };
|
|
| 4308 |
The type of both `arr1` and `arr2` is “array of 5 `const char`”, and the
|
| 4309 |
array type is considered to be const-qualified.
|
| 4310 |
|
| 4311 |
— *end example*]
|
| 4312 |
|
| 4313 |
-
[*Note
|
| 4314 |
-
types that have *cv-qualifier*s. — *end note*]
|
| 4315 |
|
| 4316 |
There is a partial ordering on cv-qualifiers, so that a type can be said
|
| 4317 |
to be *more cv-qualified* than another. [[basic.type.qualifier.rel]]
|
| 4318 |
shows the relations that constitute this ordering.
|
| 4319 |
|
|
@@ -4339,45 +4293,78 @@ no top-level cv-qualifiers. The type corresponding to the *type-id*
|
|
| 4339 |
`volatile int * const` has the top-level cv-qualifier `const`. For a
|
| 4340 |
class type `C`, the type corresponding to the *type-id*
|
| 4341 |
`void (C::* volatile)(int) const` has the top-level cv-qualifier
|
| 4342 |
`volatile`. — *end example*]
|
| 4343 |
|
| 4344 |
-
###
|
| 4345 |
|
| 4346 |
Every integer type has an *integer conversion rank* defined as follows:
|
| 4347 |
|
| 4348 |
- No two signed integer types other than `char` and `signed
|
| 4349 |
-
char` (if `char` is signed)
|
| 4350 |
-
|
| 4351 |
-
- The rank of a signed integer type
|
| 4352 |
-
|
| 4353 |
-
- The rank of `long long int`
|
| 4354 |
-
|
| 4355 |
-
|
| 4356 |
-
|
| 4357 |
-
- The rank of any unsigned integer type shall equal the rank of the
|
| 4358 |
corresponding signed integer type.
|
| 4359 |
-
- The rank of any standard integer type
|
| 4360 |
-
|
| 4361 |
-
- The rank of `char`
|
| 4362 |
`unsigned char`.
|
| 4363 |
-
- The rank of `bool`
|
| 4364 |
-
|
| 4365 |
-
- The ranks of `char8_t`, `char16_t`, `char32_t`, and `wchar_t`
|
| 4366 |
-
|
| 4367 |
- The rank of any extended signed integer type relative to another
|
| 4368 |
extended signed integer type with the same width is
|
| 4369 |
*implementation-defined*, but still subject to the other rules for
|
| 4370 |
determining the integer conversion rank.
|
| 4371 |
- For all integer types `T1`, `T2`, and `T3`, if `T1` has greater rank
|
| 4372 |
-
than `T2` and `T2` has greater rank than `T3`, then `T1`
|
| 4373 |
-
|
| 4374 |
|
| 4375 |
[*Note 1*: The integer conversion rank is used in the definition of the
|
| 4376 |
integral promotions [[conv.prom]] and the usual arithmetic conversions
|
| 4377 |
[[expr.arith.conv]]. — *end note*]
|
| 4378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4379 |
## Program execution <a id="basic.exec">[[basic.exec]]</a>
|
| 4380 |
|
| 4381 |
### Sequential execution <a id="intro.execution">[[intro.execution]]</a>
|
| 4382 |
|
| 4383 |
An instance of each object with automatic storage duration
|
|
@@ -4387,10 +4374,12 @@ the block and while the block is suspended (by a call of a function,
|
|
| 4387 |
suspension of a coroutine [[expr.await]], or receipt of a signal).
|
| 4388 |
|
| 4389 |
A *constituent expression* is defined as follows:
|
| 4390 |
|
| 4391 |
- The constituent expression of an expression is that expression.
|
|
|
|
|
|
|
| 4392 |
- The constituent expressions of a *braced-init-list* or of a (possibly
|
| 4393 |
parenthesized) *expression-list* are the constituent expressions of
|
| 4394 |
the elements of the respective list.
|
| 4395 |
- The constituent expressions of a *brace-or-equal-initializer* of the
|
| 4396 |
form `=` *initializer-clause* are the constituent expressions of the
|
|
@@ -4428,13 +4417,20 @@ or a subexpression of an immediate subexpression of E.
|
|
| 4428 |
|
| 4429 |
[*Note 1*: Expressions appearing in the *compound-statement* of a
|
| 4430 |
*lambda-expression* are not subexpressions of the
|
| 4431 |
*lambda-expression*. — *end note*]
|
| 4432 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4433 |
A *full-expression* is
|
| 4434 |
|
| 4435 |
-
- an unevaluated operand [[expr.
|
| 4436 |
- a *constant-expression* [[expr.const]],
|
| 4437 |
- an immediate invocation [[expr.const]],
|
| 4438 |
- an *init-declarator* [[dcl.decl]] or a *mem-initializer*
|
| 4439 |
[[class.base.init]], including the constituent expressions of the
|
| 4440 |
initializer,
|
|
@@ -4518,20 +4514,20 @@ overlap. — *end note*]
|
|
| 4518 |
Evaluations *A* and *B* are *indeterminately sequenced* when either *A*
|
| 4519 |
is sequenced before *B* or *B* is sequenced before *A*, but it is
|
| 4520 |
unspecified which.
|
| 4521 |
|
| 4522 |
[*Note 4*: Indeterminately sequenced evaluations cannot overlap, but
|
| 4523 |
-
either
|
| 4524 |
|
| 4525 |
An expression *X* is said to be sequenced before an expression *Y* if
|
| 4526 |
every value computation and every side effect associated with the
|
| 4527 |
expression *X* is sequenced before every value computation and every
|
| 4528 |
side effect associated with the expression *Y*.
|
| 4529 |
|
| 4530 |
Every value computation and side effect associated with a
|
| 4531 |
full-expression is sequenced before every value computation and side
|
| 4532 |
-
effect associated with the next full-expression to be evaluated.[^
|
| 4533 |
|
| 4534 |
Except where noted, evaluations of operands of individual operators and
|
| 4535 |
of subexpressions of individual expressions are unsequenced.
|
| 4536 |
|
| 4537 |
[*Note 5*: In an expression that is evaluated more than once during the
|
|
@@ -4562,22 +4558,23 @@ void g(int i) {
|
|
| 4562 |
}
|
| 4563 |
```
|
| 4564 |
|
| 4565 |
— *end example*]
|
| 4566 |
|
| 4567 |
-
When
|
| 4568 |
-
|
| 4569 |
-
|
| 4570 |
-
|
| 4571 |
-
|
| 4572 |
-
|
| 4573 |
-
|
| 4574 |
-
|
| 4575 |
-
|
| 4576 |
|
| 4577 |
-
|
| 4578 |
-
|
|
|
|
| 4579 |
|
| 4580 |
Several contexts in C++ cause evaluation of a function call, even though
|
| 4581 |
no corresponding function call syntax appears in the translation unit.
|
| 4582 |
|
| 4583 |
[*Example 4*: Evaluation of a *new-expression* invokes one or more
|
|
@@ -4586,37 +4583,40 @@ example, invocation of a conversion function [[class.conv.fct]] can
|
|
| 4586 |
arise in contexts in which no function call syntax
|
| 4587 |
appears. — *end example*]
|
| 4588 |
|
| 4589 |
The sequencing constraints on the execution of the called function (as
|
| 4590 |
described above) are features of the function calls as evaluated,
|
| 4591 |
-
|
| 4592 |
|
| 4593 |
If a signal handler is executed as a result of a call to the
|
| 4594 |
`std::raise` function, then the execution of the handler is sequenced
|
| 4595 |
after the invocation of the `std::raise` function and before its return.
|
| 4596 |
|
| 4597 |
-
[*Note
|
| 4598 |
of the signal handler is usually unsequenced with respect to the rest of
|
| 4599 |
the program. — *end note*]
|
| 4600 |
|
| 4601 |
### Multi-threaded executions and data races <a id="intro.multithread">[[intro.multithread]]</a>
|
| 4602 |
|
|
|
|
|
|
|
| 4603 |
A *thread of execution* (also known as a *thread*) is a single flow of
|
| 4604 |
control within a program, including the initial invocation of a specific
|
| 4605 |
top-level function, and recursively including every function invocation
|
| 4606 |
subsequently executed by the thread.
|
| 4607 |
|
| 4608 |
[*Note 1*: When one thread creates another, the initial call to the
|
| 4609 |
top-level function of the new thread is executed by the new thread, not
|
| 4610 |
by the creating thread. — *end note*]
|
| 4611 |
|
| 4612 |
Every thread in a program can potentially access every object and
|
| 4613 |
-
function in a program.[^
|
| 4614 |
-
|
| 4615 |
-
|
| 4616 |
-
|
| 4617 |
-
|
|
|
|
| 4618 |
|
| 4619 |
[*Note 2*: Usually the execution can be viewed as an interleaving of
|
| 4620 |
all its threads. However, some kinds of atomic operations, for example,
|
| 4621 |
allow executions inconsistent with a simple interleaving, as described
|
| 4622 |
below. — *end note*]
|
|
@@ -4633,12 +4633,12 @@ contains the signal handler invocation.
|
|
| 4633 |
The value of an object visible to a thread T at a particular point is
|
| 4634 |
the initial value of the object, a value assigned to the object by T, or
|
| 4635 |
a value assigned to the object by another thread, according to the rules
|
| 4636 |
below.
|
| 4637 |
|
| 4638 |
-
[*Note 1*: In some cases, there
|
| 4639 |
-
of this subclause is motivated by the desire to support atomic
|
| 4640 |
operations with explicit and detailed visibility constraints. However,
|
| 4641 |
it also implicitly supports a simpler view for more restricted
|
| 4642 |
programs. — *end note*]
|
| 4643 |
|
| 4644 |
Two expression evaluations *conflict* if one of them modifies a memory
|
|
@@ -4671,11 +4671,11 @@ All modifications to a particular atomic object M occur in some
|
|
| 4671 |
particular total order, called the *modification order* of M.
|
| 4672 |
|
| 4673 |
[*Note 3*: There is a separate order for each atomic object. There is
|
| 4674 |
no requirement that these can be combined into a single total order for
|
| 4675 |
all objects. In general this will be impossible since different threads
|
| 4676 |
-
|
| 4677 |
orders. — *end note*]
|
| 4678 |
|
| 4679 |
A *release sequence* headed by a release operation A on an atomic object
|
| 4680 |
M is a maximal contiguous sub-sequence of side effects in the
|
| 4681 |
modification order of M, where the first operation is A, and every
|
|
@@ -4884,12 +4884,12 @@ computation of an object being taken from the last side effect on that
|
|
| 4884 |
object in that interleaving. This is normally referred to as “sequential
|
| 4885 |
consistency”. However, this applies only to data-race-free programs, and
|
| 4886 |
data-race-free programs cannot observe most program transformations that
|
| 4887 |
do not change single-threaded program semantics. In fact, most
|
| 4888 |
single-threaded program transformations continue to be allowed, since
|
| 4889 |
-
any program that behaves differently as a result
|
| 4890 |
-
|
| 4891 |
|
| 4892 |
Two accesses to the same object of type `volatile std::sig_atomic_t` do
|
| 4893 |
not result in a data race if both occur in the same thread, even if one
|
| 4894 |
or more occurs in a signal handler. For each signal handler invocation,
|
| 4895 |
evaluations performed by the thread invoking a signal handler can be
|
|
@@ -4905,17 +4905,17 @@ potentially shared memory location that would not be modified by the
|
|
| 4905 |
abstract machine are generally precluded by this document, since such an
|
| 4906 |
assignment might overwrite another assignment by a different thread in
|
| 4907 |
cases in which an abstract machine execution would not have encountered
|
| 4908 |
a data race. This includes implementations of data member assignment
|
| 4909 |
that overwrite adjacent members in separate memory locations. Reordering
|
| 4910 |
-
of atomic loads in cases in which the atomics in question
|
| 4911 |
-
also generally precluded, since this
|
| 4912 |
rules. — *end note*]
|
| 4913 |
|
| 4914 |
[*Note 23*: Transformations that introduce a speculative read of a
|
| 4915 |
-
potentially shared memory location
|
| 4916 |
-
C++ program as defined in this document, since they potentially
|
| 4917 |
introduce a data race. However, they are typically valid in the context
|
| 4918 |
of an optimizing compiler that targets a specific machine with
|
| 4919 |
well-defined semantics for data races. They would be invalid for a
|
| 4920 |
hypothetical machine that is not tolerant of races or provides hardware
|
| 4921 |
race detection. — *end note*]
|
|
@@ -4938,25 +4938,25 @@ Executions of atomic functions that are either defined to be lock-free
|
|
| 4938 |
[[atomics.flag]] or indicated as lock-free [[atomics.lockfree]] are
|
| 4939 |
*lock-free executions*.
|
| 4940 |
|
| 4941 |
- If there is only one thread that is not blocked [[defns.block]] in a
|
| 4942 |
standard library function, a lock-free execution in that thread shall
|
| 4943 |
-
complete. \[*Note 2*: Concurrently executing threads
|
| 4944 |
progress of a lock-free execution. For example, this situation can
|
| 4945 |
occur with load-locked store-conditional implementations. This
|
| 4946 |
property is sometimes termed obstruction-free. — *end note*]
|
| 4947 |
- When one or more lock-free executions run concurrently, at least one
|
| 4948 |
should complete. \[*Note 3*: It is difficult for some implementations
|
| 4949 |
to provide absolute guarantees to this effect, since repeated and
|
| 4950 |
-
particularly inopportune interference from other threads
|
| 4951 |
forward progress, e.g., by repeatedly stealing a cache line for
|
| 4952 |
unrelated purposes between load-locked and store-conditional
|
| 4953 |
-
instructions.
|
| 4954 |
-
|
| 4955 |
-
|
| 4956 |
-
Outside this document, this property is
|
| 4957 |
-
lock-free. — *end note*]
|
| 4958 |
|
| 4959 |
During the execution of a thread of execution, each of the following is
|
| 4960 |
termed an *execution step*:
|
| 4961 |
|
| 4962 |
- termination of the thread of execution,
|
|
@@ -4968,11 +4968,11 @@ An invocation of a standard library function that blocks [[defns.block]]
|
|
| 4968 |
is considered to continuously execute execution steps while waiting for
|
| 4969 |
the condition that it blocks on to be satisfied.
|
| 4970 |
|
| 4971 |
[*Example 1*: A library I/O function that blocks until the I/O
|
| 4972 |
operation is complete can be considered to continuously check whether
|
| 4973 |
-
the operation is complete. Each such check
|
| 4974 |
execution steps, for example using observable behavior of the abstract
|
| 4975 |
machine. — *end example*]
|
| 4976 |
|
| 4977 |
[*Note 4*: Because of this and the preceding requirement regarding what
|
| 4978 |
threads of execution have to perform eventually, it follows that no
|
|
@@ -4987,30 +4987,28 @@ concurrent threads that are not blocked in a standard library function
|
|
| 4987 |
For a thread of execution providing *concurrent forward progress
|
| 4988 |
guarantees*, the implementation ensures that the thread will eventually
|
| 4989 |
make progress for as long as it has not terminated.
|
| 4990 |
|
| 4991 |
[*Note 5*: This is required regardless of whether or not other threads
|
| 4992 |
-
of
|
| 4993 |
fulfill this requirement means that this will happen in an unspecified
|
| 4994 |
but finite amount of time. — *end note*]
|
| 4995 |
|
| 4996 |
It is *implementation-defined* whether the implementation-created thread
|
| 4997 |
of execution that executes `main` [[basic.start.main]] and the threads
|
| 4998 |
of execution created by `std::thread` [[thread.thread.class]] or
|
| 4999 |
`std::jthread` [[thread.jthread.class]] provide concurrent forward
|
| 5000 |
-
progress guarantees.
|
| 5001 |
-
|
| 5002 |
-
[*Note 6*: General-purpose implementations should provide these
|
| 5003 |
-
guarantees. — *end note*]
|
| 5004 |
|
| 5005 |
For a thread of execution providing *parallel forward progress
|
| 5006 |
guarantees*, the implementation is not required to ensure that the
|
| 5007 |
thread will eventually make progress if it has not yet executed any
|
| 5008 |
execution step; once this thread has executed a step, it provides
|
| 5009 |
concurrent forward progress guarantees.
|
| 5010 |
|
| 5011 |
-
[*Note
|
| 5012 |
thread of execution, which will typically be specified by the entity
|
| 5013 |
that creates this thread of execution. For example, a thread of
|
| 5014 |
execution that provides concurrent forward progress guarantees and
|
| 5015 |
executes tasks from a set of tasks in an arbitrary order, one after the
|
| 5016 |
other, satisfies the requirements of parallel forward progress for these
|
|
@@ -5018,57 +5016,57 @@ tasks. — *end note*]
|
|
| 5018 |
|
| 5019 |
For a thread of execution providing *weakly parallel forward progress
|
| 5020 |
guarantees*, the implementation does not ensure that the thread will
|
| 5021 |
eventually make progress.
|
| 5022 |
|
| 5023 |
-
[*Note
|
| 5024 |
progress guarantees cannot be expected to make progress regardless of
|
| 5025 |
whether other threads make progress or not; however, blocking with
|
| 5026 |
forward progress guarantee delegation, as defined below, can be used to
|
| 5027 |
ensure that such threads of execution make progress
|
| 5028 |
eventually. — *end note*]
|
| 5029 |
|
| 5030 |
Concurrent forward progress guarantees are stronger than parallel
|
| 5031 |
forward progress guarantees, which in turn are stronger than weakly
|
| 5032 |
parallel forward progress guarantees.
|
| 5033 |
|
| 5034 |
-
[*Note
|
| 5035 |
-
of execution
|
| 5036 |
execution provide parallel forward progress guarantees, but will fail to
|
| 5037 |
make progress under weakly parallel guarantees. — *end note*]
|
| 5038 |
|
| 5039 |
When a thread of execution P is specified to *block with forward
|
| 5040 |
progress guarantee delegation* on the completion of a set S of threads
|
| 5041 |
of execution, then throughout the whole time of P being blocked on S,
|
| 5042 |
the implementation shall ensure that the forward progress guarantees
|
| 5043 |
provided by at least one thread of execution in S is at least as strong
|
| 5044 |
as P’s forward progress guarantees.
|
| 5045 |
|
| 5046 |
-
[*Note
|
| 5047 |
are chosen and for which number of execution steps. The strengthening is
|
| 5048 |
not permanent and not necessarily in place for the rest of the lifetime
|
| 5049 |
of the affected thread of execution. As long as P is blocked, the
|
| 5050 |
implementation has to eventually select and potentially strengthen a
|
| 5051 |
thread of execution in S. — *end note*]
|
| 5052 |
|
| 5053 |
Once a thread of execution in S terminates, it is removed from S. Once S
|
| 5054 |
is empty, P is unblocked.
|
| 5055 |
|
| 5056 |
-
[*Note
|
| 5057 |
effectively stronger forward progress guarantee for a certain amount of
|
| 5058 |
time, due to a second thread of execution A being blocked on it with
|
| 5059 |
forward progress guarantee delegation. In turn, if B then blocks with
|
| 5060 |
-
forward progress guarantee delegation on C, this
|
| 5061 |
provide a stronger forward progress guarantee to C. — *end note*]
|
| 5062 |
|
| 5063 |
-
[*Note
|
| 5064 |
they terminate and do not use blocking synchronization incorrectly),
|
| 5065 |
then P’s execution of the operation that blocks with forward progress
|
| 5066 |
guarantee delegation will not result in P’s progress guarantee being
|
| 5067 |
effectively weakened. — *end note*]
|
| 5068 |
|
| 5069 |
-
[*Note
|
| 5070 |
synchronization for threads of execution providing parallel or weakly
|
| 5071 |
parallel forward progress guarantees because the implementation is not
|
| 5072 |
required to strengthen a particular thread of execution whose too-weak
|
| 5073 |
progress guarantee is preventing overall progress. — *end note*]
|
| 5074 |
|
|
@@ -5078,28 +5076,26 @@ visible to all other threads in a finite period of time.
|
|
| 5078 |
|
| 5079 |
### Start and termination <a id="basic.start">[[basic.start]]</a>
|
| 5080 |
|
| 5081 |
#### `main` function <a id="basic.start.main">[[basic.start.main]]</a>
|
| 5082 |
|
| 5083 |
-
A program shall contain
|
| 5084 |
-
global
|
| 5085 |
-
[[intro.multithread]], [[thread.threads]]
|
| 5086 |
-
is invoked
|
| 5087 |
-
|
| 5088 |
-
It is *implementation-defined* whether a program in a freestanding
|
| 5089 |
-
environment is required to define a `main` function.
|
| 5090 |
|
| 5091 |
[*Note 1*: In a freestanding environment, startup and termination is
|
| 5092 |
*implementation-defined*; startup contains the execution of constructors
|
| 5093 |
-
for objects
|
| 5094 |
-
|
| 5095 |
duration. — *end note*]
|
| 5096 |
|
| 5097 |
-
An implementation shall not predefine the `main` function.
|
| 5098 |
-
shall
|
| 5099 |
-
|
| 5100 |
-
|
| 5101 |
|
| 5102 |
- a function of `()` returning `int` and
|
| 5103 |
- a function of `(int`, pointer to pointer to `char)` returning `int`
|
| 5104 |
|
| 5105 |
as the type of `main` [[dcl.fct]]. In the latter form, for purposes of
|
|
@@ -5112,33 +5108,37 @@ characters of null-terminated multibyte strings (NTMBSs)
|
|
| 5112 |
[[multibyte.strings]] and `argv[0]` shall be the pointer to the initial
|
| 5113 |
character of a NTMBS that represents the name used to invoke the program
|
| 5114 |
or `""`. The value of `argc` shall be non-negative. The value of
|
| 5115 |
`argv[argc]` shall be 0.
|
| 5116 |
|
| 5117 |
-
|
| 5118 |
-
added after `argv`.
|
| 5119 |
|
| 5120 |
The function `main` shall not be used within a program. The linkage
|
| 5121 |
[[basic.link]] of `main` is *implementation-defined*. A program that
|
| 5122 |
defines `main` as deleted or that declares `main` to be `inline`,
|
| 5123 |
-
`static`, or `
|
| 5124 |
-
a coroutine [[dcl.fct.def.coroutine]]. The `main` function
|
| 5125 |
-
declared with a *linkage-specification* [[dcl.link]]. A
|
| 5126 |
-
|
| 5127 |
-
|
| 5128 |
-
|
| 5129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5130 |
|
| 5131 |
[*Example 1*: Member functions, classes, and enumerations can be called
|
| 5132 |
`main`, as can entities in other namespaces. — *end example*]
|
| 5133 |
|
| 5134 |
Terminating the program without leaving the current block (e.g., by
|
| 5135 |
calling the function `std::exit(int)` [[support.start.term]]) does not
|
| 5136 |
destroy any objects with automatic storage duration [[class.dtor]]. If
|
| 5137 |
-
`std::exit` is
|
| 5138 |
-
|
| 5139 |
-
behavior.
|
| 5140 |
|
| 5141 |
A `return` statement [[stmt.return]] in `main` has the effect of leaving
|
| 5142 |
the main function (destroying any objects with automatic storage
|
| 5143 |
duration) and calling `std::exit` with the return value as the argument.
|
| 5144 |
If control flows off the end of the *compound-statement* of `main`, the
|
|
@@ -5156,16 +5156,16 @@ phases of initiation, initialization occurs as follows.
|
|
| 5156 |
with static or thread storage duration is constant-initialized
|
| 5157 |
[[expr.const]]. If constant initialization is not performed, a variable
|
| 5158 |
with static storage duration [[basic.stc.static]] or thread storage
|
| 5159 |
duration [[basic.stc.thread]] is zero-initialized [[dcl.init]].
|
| 5160 |
Together, zero-initialization and constant initialization are called
|
| 5161 |
-
*static initialization*; all other initialization is
|
| 5162 |
-
initialization*. All static initialization strongly happens
|
| 5163 |
-
[[intro.races]] any dynamic initialization.
|
| 5164 |
|
| 5165 |
-
[*Note 1*: The dynamic initialization of non-
|
| 5166 |
-
described in [[basic.start.dynamic]]; that of
|
| 5167 |
described in [[stmt.dcl]]. — *end note*]
|
| 5168 |
|
| 5169 |
An implementation is permitted to perform the initialization of a
|
| 5170 |
variable with static or thread storage duration as a static
|
| 5171 |
initialization even if such initialization is not required to be done
|
|
@@ -5180,49 +5180,48 @@ statically, provided that
|
|
| 5180 |
statically were initialized dynamically.
|
| 5181 |
|
| 5182 |
[*Note 2*:
|
| 5183 |
|
| 5184 |
As a consequence, if the initialization of an object `obj1` refers to an
|
| 5185 |
-
object `obj2`
|
| 5186 |
-
|
| 5187 |
-
|
| 5188 |
-
|
| 5189 |
-
|
| 5190 |
|
| 5191 |
``` cpp
|
| 5192 |
inline double fd() { return 1.0; }
|
| 5193 |
extern double d1;
|
| 5194 |
double d2 = d1; // unspecified:
|
| 5195 |
-
//
|
| 5196 |
// dynamically initialized to 0.0 if d1 is
|
| 5197 |
// dynamically initialized, or 1.0 otherwise
|
| 5198 |
-
double d1 = fd(); //
|
| 5199 |
```
|
| 5200 |
|
| 5201 |
— *end note*]
|
| 5202 |
|
| 5203 |
-
#### Dynamic initialization of non-
|
| 5204 |
|
| 5205 |
-
Dynamic initialization of a non-
|
| 5206 |
duration is unordered if the variable is an implicitly or explicitly
|
| 5207 |
instantiated specialization, is partially-ordered if the variable is an
|
| 5208 |
inline variable that is not an implicitly or explicitly instantiated
|
| 5209 |
specialization, and otherwise is ordered.
|
| 5210 |
|
| 5211 |
-
[*Note 1*:
|
| 5212 |
-
|
| 5213 |
-
initialization. — *end note*]
|
| 5214 |
|
| 5215 |
A declaration `D` is *appearance-ordered* before a declaration `E` if
|
| 5216 |
|
| 5217 |
- `D` appears in the same translation unit as `E`, or
|
| 5218 |
- the translation unit containing `E` has an interface dependency on the
|
| 5219 |
translation unit containing `D`,
|
| 5220 |
|
| 5221 |
in either case prior to `E`.
|
| 5222 |
|
| 5223 |
-
Dynamic initialization of non-
|
| 5224 |
storage duration are ordered as follows:
|
| 5225 |
|
| 5226 |
- If `V` and `W` have ordered initialization and the definition of `V`
|
| 5227 |
is appearance-ordered before the definition of `W`, or if `V` has
|
| 5228 |
partially-ordered initialization, `W` does not have unordered
|
|
@@ -5244,25 +5243,26 @@ storage duration are ordered as follows:
|
|
| 5244 |
sequenced.
|
| 5245 |
|
| 5246 |
[*Note 2*: This definition permits initialization of a sequence of
|
| 5247 |
ordered variables concurrently with another sequence. — *end note*]
|
| 5248 |
|
| 5249 |
-
A *non-initialization odr-use* is an odr-use [[
|
| 5250 |
-
|
| 5251 |
-
|
| 5252 |
|
| 5253 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 5254 |
-
non-
|
| 5255 |
before the first statement of `main` or is deferred. If it is deferred,
|
| 5256 |
it strongly happens before any non-initialization odr-use of any
|
| 5257 |
non-inline function or non-inline variable defined in the same
|
| 5258 |
-
translation unit as the variable to be initialized.
|
| 5259 |
-
*implementation-defined* in which threads and at which points in the
|
| 5260 |
-
program such deferred dynamic initialization occurs.
|
| 5261 |
|
| 5262 |
-
|
| 5263 |
-
|
|
|
|
|
|
|
|
|
|
| 5264 |
|
| 5265 |
[*Example 1*:
|
| 5266 |
|
| 5267 |
``` cpp
|
| 5268 |
// - File 1 -
|
|
@@ -5299,27 +5299,27 @@ point after the first statement of `main`, `b` will be initialized prior
|
|
| 5299 |
to its use in `A::A`.
|
| 5300 |
|
| 5301 |
— *end example*]
|
| 5302 |
|
| 5303 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 5304 |
-
non-
|
| 5305 |
before the first statement of `main` or is deferred. If it is deferred,
|
| 5306 |
it strongly happens before any non-initialization odr-use of that
|
| 5307 |
variable. It is *implementation-defined* in which threads and at which
|
| 5308 |
points in the program such deferred dynamic initialization occurs.
|
| 5309 |
|
| 5310 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 5311 |
-
non-
|
| 5312 |
before the first statement of the initial function of a thread or is
|
| 5313 |
deferred. If it is deferred, the initialization associated with the
|
| 5314 |
entity for thread *t* is sequenced before the first non-initialization
|
| 5315 |
odr-use by *t* of any non-inline variable with thread storage duration
|
| 5316 |
defined in the same translation unit as the variable to be initialized.
|
| 5317 |
It is *implementation-defined* in which threads and at which points in
|
| 5318 |
the program such deferred dynamic initialization occurs.
|
| 5319 |
|
| 5320 |
-
If the initialization of a non-
|
| 5321 |
storage duration exits via an exception, the function `std::terminate`
|
| 5322 |
is called [[except.terminate]].
|
| 5323 |
|
| 5324 |
#### Termination <a id="basic.start.term">[[basic.start.term]]</a>
|
| 5325 |
|
|
@@ -5346,23 +5346,25 @@ of the constructor or dynamic initialization of an object with thread
|
|
| 5346 |
storage duration is sequenced before that of another, the completion of
|
| 5347 |
the destructor of the second is sequenced before the initiation of the
|
| 5348 |
destructor of the first. If an object is initialized statically, the
|
| 5349 |
object is destroyed in the same order as if the object was dynamically
|
| 5350 |
initialized. For an object of array or class type, all subobjects of
|
| 5351 |
-
that object are destroyed before any block
|
| 5352 |
-
|
| 5353 |
-
|
| 5354 |
-
|
| 5355 |
-
|
| 5356 |
|
| 5357 |
-
If a function contains a block
|
| 5358 |
duration that has been destroyed and the function is called during the
|
| 5359 |
destruction of an object with static or thread storage duration, the
|
| 5360 |
program has undefined behavior if the flow of control passes through the
|
| 5361 |
-
definition of the previously destroyed block
|
| 5362 |
-
|
| 5363 |
-
|
|
|
|
|
|
|
| 5364 |
|
| 5365 |
If the completion of the initialization of an object with static storage
|
| 5366 |
duration strongly happens before a call to `std::atexit` (see
|
| 5367 |
`<cstdlib>`, [[support.start.term]]), the call to the function passed to
|
| 5368 |
`std::atexit` is sequenced before the call to the destructor for the
|
|
@@ -5379,11 +5381,11 @@ If there is a use of a standard library object or function not permitted
|
|
| 5379 |
within signal handlers [[support.runtime]] that does not happen before
|
| 5380 |
[[intro.multithread]] completion of destruction of objects with static
|
| 5381 |
storage duration and execution of `std::atexit` registered functions
|
| 5382 |
[[support.start.term]], the program has undefined behavior.
|
| 5383 |
|
| 5384 |
-
[*Note
|
| 5385 |
that does not happen before the object’s destruction, the program has
|
| 5386 |
undefined behavior. Terminating every thread before a call to
|
| 5387 |
`std::exit` or the exit from `main` is sufficient, but not necessary, to
|
| 5388 |
satisfy these requirements. These requirements permit thread managers as
|
| 5389 |
static-storage-duration objects. — *end note*]
|
|
@@ -5391,49 +5393,50 @@ static-storage-duration objects. — *end note*]
|
|
| 5391 |
Calling the function `std::abort()` declared in `<cstdlib>` terminates
|
| 5392 |
the program without executing any destructors and without calling the
|
| 5393 |
functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
| 5394 |
|
| 5395 |
<!-- Link reference definitions -->
|
| 5396 |
-
[allocator.members]:
|
| 5397 |
-
[allocator.traits.members]:
|
| 5398 |
-
[atomics]:
|
| 5399 |
-
[atomics.flag]:
|
| 5400 |
-
[atomics.lockfree]:
|
| 5401 |
-
[atomics.order]:
|
| 5402 |
[bad.alloc]: support.md#bad.alloc
|
| 5403 |
[basic]: #basic
|
| 5404 |
[basic.align]: #basic.align
|
| 5405 |
[basic.compound]: #basic.compound
|
| 5406 |
[basic.def]: #basic.def
|
| 5407 |
[basic.def.odr]: #basic.def.odr
|
| 5408 |
[basic.exec]: #basic.exec
|
|
|
|
| 5409 |
[basic.fundamental]: #basic.fundamental
|
| 5410 |
[basic.fundamental.width]: #basic.fundamental.width
|
| 5411 |
-
[basic.funscope]: #basic.funscope
|
| 5412 |
[basic.indet]: #basic.indet
|
| 5413 |
[basic.life]: #basic.life
|
| 5414 |
[basic.link]: #basic.link
|
| 5415 |
[basic.lookup]: #basic.lookup
|
| 5416 |
[basic.lookup.argdep]: #basic.lookup.argdep
|
| 5417 |
-
[basic.lookup.classref]: #basic.lookup.classref
|
| 5418 |
[basic.lookup.elab]: #basic.lookup.elab
|
|
|
|
| 5419 |
[basic.lookup.qual]: #basic.lookup.qual
|
|
|
|
| 5420 |
[basic.lookup.udir]: #basic.lookup.udir
|
| 5421 |
[basic.lookup.unqual]: #basic.lookup.unqual
|
| 5422 |
[basic.lval]: expr.md#basic.lval
|
| 5423 |
[basic.memobj]: #basic.memobj
|
| 5424 |
[basic.namespace]: dcl.md#basic.namespace
|
| 5425 |
[basic.pre]: #basic.pre
|
| 5426 |
[basic.scope]: #basic.scope
|
| 5427 |
[basic.scope.block]: #basic.scope.block
|
| 5428 |
[basic.scope.class]: #basic.scope.class
|
| 5429 |
-
[basic.scope.declarative]: #basic.scope.declarative
|
| 5430 |
[basic.scope.enum]: #basic.scope.enum
|
| 5431 |
-
[basic.scope.
|
| 5432 |
[basic.scope.namespace]: #basic.scope.namespace
|
| 5433 |
[basic.scope.param]: #basic.scope.param
|
| 5434 |
[basic.scope.pdecl]: #basic.scope.pdecl
|
|
|
|
| 5435 |
[basic.scope.temp]: #basic.scope.temp
|
| 5436 |
[basic.start]: #basic.start
|
| 5437 |
[basic.start.dynamic]: #basic.start.dynamic
|
| 5438 |
[basic.start.main]: #basic.start.main
|
| 5439 |
[basic.start.static]: #basic.start.static
|
|
@@ -5441,22 +5444,25 @@ functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
|
| 5441 |
[basic.stc]: #basic.stc
|
| 5442 |
[basic.stc.auto]: #basic.stc.auto
|
| 5443 |
[basic.stc.dynamic]: #basic.stc.dynamic
|
| 5444 |
[basic.stc.dynamic.allocation]: #basic.stc.dynamic.allocation
|
| 5445 |
[basic.stc.dynamic.deallocation]: #basic.stc.dynamic.deallocation
|
| 5446 |
-
[basic.stc.dynamic.
|
|
|
|
| 5447 |
[basic.stc.inherit]: #basic.stc.inherit
|
| 5448 |
[basic.stc.static]: #basic.stc.static
|
| 5449 |
[basic.stc.thread]: #basic.stc.thread
|
| 5450 |
[basic.type.qualifier]: #basic.type.qualifier
|
| 5451 |
[basic.type.qualifier.rel]: #basic.type.qualifier.rel
|
| 5452 |
[basic.types]: #basic.types
|
| 5453 |
-
[
|
| 5454 |
-
[
|
|
|
|
| 5455 |
[class]: class.md#class
|
| 5456 |
[class.abstract]: class.md#class.abstract
|
| 5457 |
[class.access]: class.md#class.access
|
|
|
|
| 5458 |
[class.base.init]: class.md#class.base.init
|
| 5459 |
[class.bit]: class.md#class.bit
|
| 5460 |
[class.cdtor]: class.md#class.cdtor
|
| 5461 |
[class.conv.fct]: class.md#class.conv.fct
|
| 5462 |
[class.copy.assign]: class.md#class.copy.assign
|
|
@@ -5465,26 +5471,24 @@ functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
|
| 5465 |
[class.default.ctor]: class.md#class.default.ctor
|
| 5466 |
[class.derived]: class.md#class.derived
|
| 5467 |
[class.dtor]: class.md#class.dtor
|
| 5468 |
[class.free]: class.md#class.free
|
| 5469 |
[class.friend]: class.md#class.friend
|
| 5470 |
-
[class.local]: class.md#class.local
|
| 5471 |
[class.mem]: class.md#class.mem
|
| 5472 |
-
[class.member.lookup]:
|
| 5473 |
[class.mfct]: class.md#class.mfct
|
| 5474 |
-
[class.mfct.non
|
| 5475 |
[class.name]: class.md#class.name
|
| 5476 |
-
[class.nest]: class.md#class.nest
|
| 5477 |
[class.pre]: class.md#class.pre
|
| 5478 |
[class.prop]: class.md#class.prop
|
| 5479 |
[class.qual]: #class.qual
|
| 5480 |
[class.spaceship]: class.md#class.spaceship
|
| 5481 |
[class.static]: class.md#class.static
|
| 5482 |
[class.static.data]: class.md#class.static.data
|
| 5483 |
[class.temporary]: #class.temporary
|
| 5484 |
-
[class.this]: class.md#class.this
|
| 5485 |
[class.union]: class.md#class.union
|
|
|
|
| 5486 |
[class.virtual]: class.md#class.virtual
|
| 5487 |
[conv]: expr.md#conv
|
| 5488 |
[conv.array]: expr.md#conv.array
|
| 5489 |
[conv.func]: expr.md#conv.func
|
| 5490 |
[conv.integral]: expr.md#conv.integral
|
|
@@ -5512,11 +5516,10 @@ functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
|
| 5512 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 5513 |
[dcl.init]: dcl.md#dcl.init
|
| 5514 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 5515 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 5516 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 5517 |
-
[dcl.inline]: dcl.md#dcl.inline
|
| 5518 |
[dcl.link]: dcl.md#dcl.link
|
| 5519 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 5520 |
[dcl.mptr]: dcl.md#dcl.mptr
|
| 5521 |
[dcl.name]: dcl.md#dcl.name
|
| 5522 |
[dcl.pre]: dcl.md#dcl.pre
|
|
@@ -5524,34 +5527,31 @@ functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
|
| 5524 |
[dcl.ref]: dcl.md#dcl.ref
|
| 5525 |
[dcl.spec]: dcl.md#dcl.spec
|
| 5526 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 5527 |
[dcl.stc]: dcl.md#dcl.stc
|
| 5528 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
|
|
|
| 5529 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 5530 |
[dcl.typedef]: dcl.md#dcl.typedef
|
| 5531 |
[defns.block]: intro.md#defns.block
|
| 5532 |
-
[defns.signature]: intro.md#defns.signature
|
| 5533 |
-
[defns.signature.templ]: intro.md#defns.signature.templ
|
| 5534 |
[depr.local]: future.md#depr.local
|
| 5535 |
[depr.static.constexpr]: future.md#depr.static.constexpr
|
| 5536 |
[diff.cpp11.basic]: compatibility.md#diff.cpp11.basic
|
| 5537 |
[enum.udecl]: dcl.md#enum.udecl
|
| 5538 |
[except.handle]: except.md#except.handle
|
| 5539 |
[except.pre]: except.md#except.pre
|
| 5540 |
[except.spec]: except.md#except.spec
|
| 5541 |
[except.terminate]: except.md#except.terminate
|
| 5542 |
[except.throw]: except.md#except.throw
|
| 5543 |
-
[expr]: expr.md#expr
|
| 5544 |
[expr.add]: expr.md#expr.add
|
| 5545 |
[expr.alignof]: expr.md#expr.alignof
|
| 5546 |
[expr.arith.conv]: expr.md#expr.arith.conv
|
| 5547 |
[expr.ass]: expr.md#expr.ass
|
| 5548 |
[expr.await]: expr.md#expr.await
|
| 5549 |
[expr.call]: expr.md#expr.call
|
| 5550 |
[expr.cast]: expr.md#expr.cast
|
| 5551 |
[expr.comma]: expr.md#expr.comma
|
| 5552 |
-
[expr.compound]: expr.md#expr.compound
|
| 5553 |
[expr.cond]: expr.md#expr.cond
|
| 5554 |
[expr.const]: expr.md#expr.const
|
| 5555 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 5556 |
[expr.context]: expr.md#expr.context
|
| 5557 |
[expr.delete]: expr.md#expr.delete
|
|
@@ -5561,12 +5561,12 @@ functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
|
| 5561 |
[expr.log.or]: expr.md#expr.log.or
|
| 5562 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 5563 |
[expr.new]: expr.md#expr.new
|
| 5564 |
[expr.pre]: expr.md#expr.pre
|
| 5565 |
[expr.prim.id]: expr.md#expr.prim.id
|
| 5566 |
-
[expr.prim.id.dtor]: expr.md#expr.prim.id.dtor
|
| 5567 |
[expr.prim.id.qual]: expr.md#expr.prim.id.qual
|
|
|
|
| 5568 |
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
| 5569 |
[expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
|
| 5570 |
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 5571 |
[expr.prim.this]: expr.md#expr.prim.this
|
| 5572 |
[expr.prop]: expr.md#expr.prop
|
|
@@ -5582,184 +5582,175 @@ functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
|
| 5582 |
[get.new.handler]: support.md#get.new.handler
|
| 5583 |
[headers]: library.md#headers
|
| 5584 |
[intro.execution]: #intro.execution
|
| 5585 |
[intro.memory]: #intro.memory
|
| 5586 |
[intro.multithread]: #intro.multithread
|
|
|
|
| 5587 |
[intro.object]: #intro.object
|
| 5588 |
[intro.progress]: #intro.progress
|
| 5589 |
[intro.races]: #intro.races
|
| 5590 |
[lex.charset]: lex.md#lex.charset
|
|
|
|
| 5591 |
[lex.name]: lex.md#lex.name
|
| 5592 |
[lex.separate]: lex.md#lex.separate
|
| 5593 |
-
[locale]: localization.md#locale
|
| 5594 |
-
[meta.trans.other]: utilities.md#meta.trans.other
|
| 5595 |
[module.context]: module.md#module.context
|
| 5596 |
[module.global.frag]: module.md#module.global.frag
|
| 5597 |
-
[module.import]: module.md#module.import
|
| 5598 |
[module.interface]: module.md#module.interface
|
| 5599 |
[module.reach]: module.md#module.reach
|
| 5600 |
[module.unit]: module.md#module.unit
|
| 5601 |
[multibyte.strings]: library.md#multibyte.strings
|
| 5602 |
[namespace.def]: dcl.md#namespace.def
|
| 5603 |
-
[namespace.memdef]: dcl.md#namespace.memdef
|
| 5604 |
[namespace.qual]: #namespace.qual
|
| 5605 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 5606 |
[namespace.udir]: dcl.md#namespace.udir
|
|
|
|
| 5607 |
[new.delete]: support.md#new.delete
|
| 5608 |
[new.delete.array]: support.md#new.delete.array
|
| 5609 |
[new.delete.placement]: support.md#new.delete.placement
|
| 5610 |
[new.delete.single]: support.md#new.delete.single
|
| 5611 |
[new.handler]: support.md#new.handler
|
|
|
|
|
|
|
| 5612 |
[over]: over.md#over
|
| 5613 |
[over.literal]: over.md#over.literal
|
| 5614 |
[over.match]: over.md#over.match
|
|
|
|
| 5615 |
[over.oper]: over.md#over.oper
|
| 5616 |
[over.over]: over.md#over.over
|
| 5617 |
-
[ptr.align]:
|
| 5618 |
[ptr.launder]: support.md#ptr.launder
|
| 5619 |
[replacement.functions]: library.md#replacement.functions
|
| 5620 |
[special]: class.md#special
|
|
|
|
|
|
|
| 5621 |
[stmt.block]: stmt.md#stmt.block
|
| 5622 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 5623 |
[stmt.expr]: stmt.md#stmt.expr
|
| 5624 |
-
[stmt.goto]: stmt.md#stmt.goto
|
| 5625 |
[stmt.if]: stmt.md#stmt.if
|
| 5626 |
-
[stmt.
|
|
|
|
| 5627 |
[stmt.ranged]: stmt.md#stmt.ranged
|
| 5628 |
[stmt.return]: stmt.md#stmt.return
|
|
|
|
| 5629 |
[support.dynamic]: support.md#support.dynamic
|
| 5630 |
-
[support.limits]: support.md#support.limits
|
| 5631 |
[support.runtime]: support.md#support.runtime
|
| 5632 |
[support.start.term]: support.md#support.start.term
|
| 5633 |
[support.types]: support.md#support.types
|
|
|
|
| 5634 |
[temp.deduct.guide]: temp.md#temp.deduct.guide
|
| 5635 |
[temp.dep]: temp.md#temp.dep
|
| 5636 |
[temp.dep.candidate]: temp.md#temp.dep.candidate
|
|
|
|
|
|
|
| 5637 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 5638 |
[temp.explicit]: temp.md#temp.explicit
|
|
|
|
| 5639 |
[temp.local]: temp.md#temp.local
|
| 5640 |
[temp.names]: temp.md#temp.names
|
| 5641 |
-
[temp.nondep]: temp.md#temp.nondep
|
| 5642 |
[temp.over]: temp.md#temp.over
|
|
|
|
| 5643 |
[temp.param]: temp.md#temp.param
|
| 5644 |
[temp.point]: temp.md#temp.point
|
| 5645 |
[temp.pre]: temp.md#temp.pre
|
| 5646 |
[temp.res]: temp.md#temp.res
|
| 5647 |
[temp.spec]: temp.md#temp.spec
|
|
|
|
| 5648 |
[temp.type]: temp.md#temp.type
|
|
|
|
|
|
|
|
|
|
| 5649 |
[thread]: thread.md#thread
|
| 5650 |
[thread.jthread.class]: thread.md#thread.jthread.class
|
| 5651 |
[thread.thread.class]: thread.md#thread.thread.class
|
| 5652 |
[thread.threads]: thread.md#thread.threads
|
| 5653 |
-
[util.dynamic.safety]: utilities.md#util.dynamic.safety
|
| 5654 |
|
| 5655 |
[^1]: Appearing inside the brace-enclosed *declaration-seq* in a
|
| 5656 |
*linkage-specification* does not affect whether a declaration is a
|
| 5657 |
definition.
|
| 5658 |
|
| 5659 |
[^2]: An implementation is not required to call allocation and
|
| 5660 |
deallocation functions from constructors or destructors; however,
|
| 5661 |
this is a permissible implementation technique.
|
| 5662 |
|
| 5663 |
-
[^3]:
|
| 5664 |
-
|
| 5665 |
-
used in the function body.
|
| 5666 |
|
| 5667 |
-
[^4]:
|
| 5668 |
-
name may be used in a *base-specifier* or in the
|
| 5669 |
-
*member-specification* of the class definition.
|
| 5670 |
-
|
| 5671 |
-
[^5]: This lookup applies whether the definition of `X` is nested within
|
| 5672 |
-
`Y`’s definition or whether `X`’s definition appears in a namespace
|
| 5673 |
-
scope enclosing `Y`’s definition [[class.nest]].
|
| 5674 |
-
|
| 5675 |
-
[^6]: That is, an unqualified name that occurs, for instance, in a type
|
| 5676 |
-
in the *parameter-declaration-clause* or in the
|
| 5677 |
-
*noexcept-specifier*.
|
| 5678 |
-
|
| 5679 |
-
[^7]: This lookup applies whether the member function is defined within
|
| 5680 |
-
the definition of class `X` or whether the member function is
|
| 5681 |
-
defined in a namespace scope enclosing `X`’s definition.
|
| 5682 |
-
|
| 5683 |
-
[^8]: Lookups in which function names are ignored include names
|
| 5684 |
appearing in a *nested-name-specifier*, an
|
| 5685 |
*elaborated-type-specifier*, or a *base-specifier*.
|
| 5686 |
|
| 5687 |
-
[^
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5688 |
in the header `<climits>`.
|
| 5689 |
|
| 5690 |
-
[^
|
| 5691 |
objects at the same machine address or not store an object at all if
|
| 5692 |
the program cannot observe the difference [[intro.execution]].
|
| 5693 |
|
| 5694 |
-
[^
|
| 5695 |
-
|
| 5696 |
|
| 5697 |
-
[^
|
| 5698 |
implicitly—upon exit from the block for an object with automatic
|
| 5699 |
storage duration, upon exit from the thread for an object with
|
| 5700 |
thread storage duration, or upon exit from the program for an object
|
| 5701 |
with static storage duration.
|
| 5702 |
|
| 5703 |
-
[^
|
| 5704 |
value causes a system-generated runtime fault.
|
| 5705 |
|
| 5706 |
-
[^
|
| 5707 |
`std::malloc()` or `std::calloc()`, so the rules are substantially
|
| 5708 |
the same. C++ differs from C in requiring a zero request to return a
|
| 5709 |
non-null pointer.
|
| 5710 |
|
| 5711 |
-
[^
|
| 5712 |
an allocation function `void operator new(std::size_t, std::size_t)`
|
| 5713 |
-
as a placement allocation function
|
| 5714 |
|
| 5715 |
-
[^
|
| 5716 |
-
through pointers to memory not allocated by `::operator new`. This
|
| 5717 |
-
maintains the ability of many C++ implementations to use binary
|
| 5718 |
-
libraries and components written in other languages. In particular,
|
| 5719 |
-
this applies to C binaries, because indirection through pointers to
|
| 5720 |
-
memory allocated by `std::malloc` is not restricted.
|
| 5721 |
-
|
| 5722 |
-
[^17]: The same rules apply to initialization of an `initializer_list`
|
| 5723 |
object [[dcl.init.list]] with its underlying temporary array.
|
| 5724 |
|
| 5725 |
-
[^
|
| 5726 |
`std::memcpy` or `std::memmove`.
|
| 5727 |
|
| 5728 |
-
[^
|
| 5729 |
`std::memcpy` or `std::memmove`.
|
| 5730 |
|
| 5731 |
-
[^
|
| 5732 |
that of ISO/IEC 9899 Programming Language C.
|
| 5733 |
|
| 5734 |
-
[^
|
| 5735 |
object type is unknown.
|
| 5736 |
|
| 5737 |
-
[^
|
| 5738 |
|
| 5739 |
-
[^
|
| 5740 |
them are ordinary pointers to objects or functions.
|
| 5741 |
|
| 5742 |
-
[^
|
| 5743 |
byte in memory that it will occupy or used to occupy.
|
| 5744 |
|
| 5745 |
-
[^
|
| 5746 |
imply interchangeability as arguments to functions, return values
|
| 5747 |
from functions, and non-static data members of unions.
|
| 5748 |
|
| 5749 |
-
[^
|
| 5750 |
evaluated, a sequence of zero or more invocations of destructor
|
| 5751 |
functions for temporary objects takes place, usually in reverse
|
| 5752 |
order of the construction of each temporary object.
|
| 5753 |
|
| 5754 |
-
[^
|
| 5755 |
other.
|
| 5756 |
|
| 5757 |
-
[^
|
| 5758 |
is associated with one specific thread, and can be accessed by a
|
| 5759 |
different thread only indirectly through a pointer or reference
|
| 5760 |
[[basic.compound]].
|
| 5761 |
|
| 5762 |
-
[^
|
| 5763 |
initialization with side effects is initialized in this case, even
|
| 5764 |
-
if it is not itself odr-used
|
| 5765 |
-
[[basic.stc.static]]).
|
|
|
|
| 17 |
|
| 18 |
An *entity* is a value, object, reference, structured binding, function,
|
| 19 |
enumerator, type, class member, bit-field, template, template
|
| 20 |
specialization, namespace, or pack.
|
| 21 |
|
| 22 |
+
A *name* is an *identifier* [[lex.name]], *operator-function-id*
|
| 23 |
+
[[over.oper]], *literal-operator-id* [[over.literal]], or
|
| 24 |
+
*conversion-function-id* [[class.conv.fct]].
|
| 25 |
+
|
| 26 |
+
Every name is introduced by a *declaration*, which is a
|
| 27 |
+
|
| 28 |
+
- *name-declaration*, *block-declaration*, or *member-declaration*
|
| 29 |
+
[[dcl.pre]], [[class.mem]],
|
| 30 |
+
- *init-declarator* [[dcl.decl]],
|
| 31 |
+
- *identifier* in a structured binding declaration [[dcl.struct.bind]],
|
| 32 |
+
- *init-capture* [[expr.prim.lambda.capture]],
|
| 33 |
+
- *condition* with a *declarator* [[stmt.pre]],
|
| 34 |
+
- *member-declarator* [[class.mem]],
|
| 35 |
+
- *using-declarator* [[namespace.udecl]],
|
| 36 |
+
- *parameter-declaration* [[dcl.fct]],
|
| 37 |
+
- *type-parameter* [[temp.param]],
|
| 38 |
+
- *elaborated-type-specifier* that introduces a name [[dcl.type.elab]],
|
| 39 |
+
- *class-specifier* [[class.pre]],
|
| 40 |
+
- *enum-specifier* or *enumerator-definition* [[dcl.enum]],
|
| 41 |
+
- *exception-declaration* [[except.pre]], or
|
| 42 |
+
- implicit declaration of an injected-class-name [[class.pre]].
|
| 43 |
+
|
| 44 |
+
[*Note 3*: The interpretation of a *for-range-declaration* produces one
|
| 45 |
+
or more of the above [[stmt.ranged]]. — *end note*]
|
| 46 |
+
|
| 47 |
+
An entity E is denoted by the name (if any) that is introduced by a
|
| 48 |
+
declaration of E or by a *typedef-name* introduced by a declaration
|
| 49 |
+
specifying E.
|
| 50 |
|
| 51 |
A *variable* is introduced by the declaration of a reference other than
|
| 52 |
a non-static data member or of an object. The variable’s name, if any,
|
| 53 |
denotes the reference or object.
|
| 54 |
|
|
|
|
| 65 |
|
| 66 |
Two names are *the same* if
|
| 67 |
|
| 68 |
- they are *identifier*s composed of the same character sequence, or
|
| 69 |
- they are *operator-function-id*s formed with the same operator, or
|
| 70 |
+
- they are *conversion-function-id*s formed with equivalent
|
| 71 |
+
[[temp.over.link]] types, or
|
|
|
|
| 72 |
- they are *literal-operator-id*s [[over.literal]] formed with the same
|
| 73 |
literal suffix identifier.
|
| 74 |
|
| 75 |
A name used in more than one translation unit can potentially refer to
|
| 76 |
the same entity in these translation units depending on the linkage
|
| 77 |
[[basic.link]] of the name specified in each translation unit.
|
| 78 |
|
| 79 |
## Declarations and definitions <a id="basic.def">[[basic.def]]</a>
|
| 80 |
|
| 81 |
+
A declaration [[dcl.dcl]] may (re)introduce one or more names and/or
|
| 82 |
+
entities into a translation unit. If so, the declaration specifies the
|
| 83 |
+
interpretation and semantic properties of these names. A declaration of
|
| 84 |
+
an entity or *typedef-name* X is a redeclaration of X if another
|
| 85 |
+
declaration of X is reachable from it [[module.reach]]. A declaration
|
| 86 |
+
may also have effects including:
|
| 87 |
|
| 88 |
- a static assertion [[dcl.pre]],
|
| 89 |
- controlling template instantiation [[temp.explicit]],
|
| 90 |
- guiding template argument deduction for constructors
|
| 91 |
[[temp.deduct.guide]],
|
|
|
|
| 98 |
- it declares a function without specifying the function’s body
|
| 99 |
[[dcl.fct.def]],
|
| 100 |
- it contains the `extern` specifier [[dcl.stc]] or a
|
| 101 |
*linkage-specification*[^1] [[dcl.link]] and neither an *initializer*
|
| 102 |
nor a *function-body*,
|
| 103 |
+
- it declares a non-inline static data member in a class definition
|
| 104 |
+
[[class.mem]], [[class.static]],
|
| 105 |
- it declares a static data member outside a class definition and the
|
| 106 |
variable was defined within the class with the `constexpr` specifier
|
| 107 |
(this usage is deprecated; see [[depr.static.constexpr]]),
|
| 108 |
+
- it is an *elaborated-type-specifier* [[class.name]],
|
| 109 |
- it is an *opaque-enum-declaration* [[dcl.enum]],
|
| 110 |
- it is a *template-parameter* [[temp.param]],
|
| 111 |
- it is a *parameter-declaration* [[dcl.fct]] in a function declarator
|
| 112 |
that is not the *declarator* of a *function-definition*,
|
| 113 |
- it is a `typedef` declaration [[dcl.typedef]],
|
|
|
|
| 209 |
|
| 210 |
[*Note 2*: A class name can also be implicitly declared by an
|
| 211 |
*elaborated-type-specifier* [[dcl.type.elab]]. — *end note*]
|
| 212 |
|
| 213 |
In the definition of an object, the type of that object shall not be an
|
| 214 |
+
incomplete type [[term.incomplete.type]], an abstract class type
|
| 215 |
+
[[class.abstract]], or a (possibly multidimensional) array thereof.
|
| 216 |
|
| 217 |
## One-definition rule <a id="basic.def.odr">[[basic.def.odr]]</a>
|
| 218 |
|
| 219 |
+
Each of the following is termed a *definable item*:
|
| 220 |
+
|
| 221 |
+
- a class type [[class]],
|
| 222 |
+
- an enumeration type [[dcl.enum]],
|
| 223 |
+
- a function [[dcl.fct]],
|
| 224 |
+
- a variable [[basic.pre]],
|
| 225 |
+
- a templated entity [[temp.pre]],
|
| 226 |
+
- a default argument for a parameter (for a function in a given scope)
|
| 227 |
+
[[dcl.fct.default]], or
|
| 228 |
+
- a default template argument [[temp.param]].
|
| 229 |
+
|
| 230 |
No translation unit shall contain more than one definition of any
|
| 231 |
+
definable item.
|
|
|
|
|
|
|
| 232 |
|
| 233 |
An expression or conversion is *potentially evaluated* unless it is an
|
| 234 |
+
unevaluated operand [[expr.context]], a subexpression thereof, or a
|
| 235 |
conversion in an initialization or conversion sequence in such a
|
| 236 |
context. The set of *potential results* of an expression E is defined as
|
| 237 |
follows:
|
| 238 |
|
| 239 |
- If E is an *id-expression* [[expr.prim.id]], the set contains only E.
|
|
|
|
| 278 |
— *end note*]
|
| 279 |
|
| 280 |
A function is *named by* an expression or conversion as follows:
|
| 281 |
|
| 282 |
- A function is named by an expression or conversion if it is the
|
| 283 |
+
selected member of an overload set
|
| 284 |
+
[[basic.lookup]], [[over.match]], [[over.over]] in an overload
|
| 285 |
+
resolution performed as part of forming that expression or conversion,
|
| 286 |
+
unless it is a pure virtual function and either the expression is not
|
| 287 |
+
an *id-expression* naming the function with an explicitly qualified
|
| 288 |
+
name or the expression forms a pointer to member [[expr.unary.op]].
|
| 289 |
+
\[*Note 2*: This covers taking the address of functions
|
| 290 |
+
[[conv.func]], [[expr.unary.op]], calls to named functions
|
| 291 |
+
[[expr.call]], operator overloading [[over]], user-defined conversions
|
| 292 |
+
[[class.conv.fct]], allocation functions for *new-expression*s
|
| 293 |
+
[[expr.new]], as well as non-default initialization [[dcl.init]]. A
|
| 294 |
+
constructor selected to copy or move an object of class type is
|
| 295 |
+
considered to be named by an expression or conversion even if the call
|
| 296 |
+
is actually elided by the implementation
|
| 297 |
[[class.copy.elision]]. — *end note*]
|
| 298 |
- A deallocation function for a class is named by a *new-expression* if
|
| 299 |
it is the single matching deallocation function for the allocation
|
| 300 |
function selected by overload resolution, as specified in
|
| 301 |
[[expr.new]].
|
| 302 |
- A deallocation function for a class is named by a *delete-expression*
|
| 303 |
if it is the selected usual deallocation function as specified in
|
| 304 |
[[expr.delete]] and [[class.free]].
|
| 305 |
|
| 306 |
+
A variable is named by an expression if the expression is an
|
| 307 |
+
*id-expression* that denotes it. A variable `x` that is named by a
|
| 308 |
+
potentially-evaluated expression E is *odr-used* by E unless
|
| 309 |
|
| 310 |
- `x` is a reference that is usable in constant expressions
|
| 311 |
[[expr.const]], or
|
| 312 |
- `x` is a variable of non-reference type that is usable in constant
|
| 313 |
expressions and has no mutable subobjects, and E is an element of the
|
| 314 |
set of potential results of an expression of non-volatile-qualified
|
| 315 |
non-class type to which the lvalue-to-rvalue conversion [[conv.lval]]
|
| 316 |
is applied, or
|
| 317 |
- `x` is a variable of non-reference type, and E is an element of the
|
| 318 |
+
set of potential results of a discarded-value expression
|
| 319 |
+
[[expr.context]] to which the lvalue-to-rvalue conversion is not
|
| 320 |
+
applied.
|
| 321 |
|
| 322 |
A structured binding is odr-used if it appears as a
|
| 323 |
potentially-evaluated expression.
|
| 324 |
|
| 325 |
`*this` is odr-used if `this` appears as a potentially-evaluated
|
| 326 |
expression (including as the result of the implicit transformation in
|
| 327 |
+
the body of a non-static member function [[class.mfct.non.static]]).
|
| 328 |
|
| 329 |
A virtual member function is odr-used if it is not pure. A function is
|
| 330 |
odr-used if it is named by a potentially-evaluated expression or
|
| 331 |
conversion. A non-placement allocation or deallocation function for a
|
| 332 |
class is odr-used by the definition of a constructor of that class. A
|
|
|
|
| 339 |
implicitly-defined copy-assignment or move-assignment function for
|
| 340 |
another class as specified in [[class.copy.assign]]. A constructor for
|
| 341 |
a class is odr-used as specified in [[dcl.init]]. A destructor for a
|
| 342 |
class is odr-used if it is potentially invoked [[class.dtor]].
|
| 343 |
|
| 344 |
+
A local entity [[basic.pre]] is *odr-usable* in a scope
|
| 345 |
+
[[basic.scope.scope]] if:
|
| 346 |
|
| 347 |
- either the local entity is not `*this`, or an enclosing class or
|
| 348 |
non-lambda function parameter scope exists and, if the innermost such
|
| 349 |
scope is a function parameter scope, it corresponds to a non-static
|
| 350 |
member function, and
|
| 351 |
+
- for each intervening scope [[basic.scope.scope]] between the point at
|
| 352 |
+
which the entity is introduced and the scope (where `*this` is
|
| 353 |
+
considered to be introduced within the innermost enclosing class or
|
| 354 |
+
non-lambda function definition scope), either:
|
| 355 |
+
- the intervening scope is a block scope, or
|
| 356 |
+
- the intervening scope is the function parameter scope of a
|
| 357 |
+
*lambda-expression* that has a *simple-capture* naming the entity or
|
| 358 |
+
has a *capture-default*, and the block scope of the
|
| 359 |
+
*lambda-expression* is also an intervening scope.
|
| 360 |
|
| 361 |
+
If a local entity is odr-used in a scope in which it is not odr-usable,
|
| 362 |
+
the program is ill-formed.
|
| 363 |
|
| 364 |
[*Example 2*:
|
| 365 |
|
| 366 |
``` cpp
|
| 367 |
void f(int n) {
|
|
|
|
| 376 |
}
|
| 377 |
```
|
| 378 |
|
| 379 |
— *end example*]
|
| 380 |
|
| 381 |
+
Every program shall contain at least one definition of every function or
|
| 382 |
+
variable that is odr-used in that program outside of a discarded
|
| 383 |
+
statement [[stmt.if]]; no diagnostic required. The definition can appear
|
| 384 |
+
explicitly in the program, it can be found in the standard or a
|
| 385 |
+
user-defined library, or (when appropriate) it is implicitly defined
|
| 386 |
(see [[class.default.ctor]], [[class.copy.ctor]], [[class.dtor]], and
|
| 387 |
[[class.copy.assign]]).
|
| 388 |
|
| 389 |
[*Example 3*:
|
| 390 |
|
|
|
|
| 408 |
translation unit excluding its *private-module-fragment* (if any). A
|
| 409 |
definition of an inline function or variable shall be reachable from the
|
| 410 |
end of every definition domain in which it is odr-used outside of a
|
| 411 |
discarded statement.
|
| 412 |
|
| 413 |
+
A definition of a class shall be reachable in every context in which the
|
| 414 |
+
class is used in a way that requires the class type to be complete.
|
|
|
|
| 415 |
|
| 416 |
[*Example 4*:
|
| 417 |
|
| 418 |
The following complete translation unit is well-formed, even though it
|
| 419 |
never defines `X`:
|
|
|
|
| 437 |
- `T` is used as the allocated type or array element type in a
|
| 438 |
*new-expression* [[expr.new]], or
|
| 439 |
- an lvalue-to-rvalue conversion is applied to a glvalue referring to an
|
| 440 |
object of type `T` [[conv.lval]], or
|
| 441 |
- an expression is converted (either implicitly or explicitly) to type
|
| 442 |
+
`T`
|
| 443 |
+
[[conv]], [[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]], [[expr.cast]],
|
| 444 |
+
or
|
| 445 |
- an expression that is not a null pointer constant, and has type other
|
| 446 |
than cv `void*`, is converted to the type pointer to `T` or reference
|
| 447 |
to `T` using a standard conversion [[conv]], a `dynamic_cast`
|
| 448 |
[[expr.dynamic.cast]] or a `static_cast` [[expr.static.cast]], or
|
| 449 |
- a class member access operator is applied to an expression of type `T`
|
|
|
|
| 459 |
- an *exception-declaration* has type `T`, reference to `T`, or pointer
|
| 460 |
to `T` [[except.handle]].
|
| 461 |
|
| 462 |
— *end note*]
|
| 463 |
|
| 464 |
+
For any definable item `D` with definitions in multiple translation
|
| 465 |
+
units,
|
| 466 |
|
| 467 |
+
- if `D` is a non-inline non-templated function or variable, or
|
| 468 |
+
- if the definitions in different translation units do not satisfy the
|
| 469 |
+
following requirements,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 470 |
|
| 471 |
+
the program is ill-formed; a diagnostic is required only if the
|
| 472 |
+
definable item is attached to a named module and a prior definition is
|
| 473 |
+
reachable at the point where a later definition occurs. Given such an
|
| 474 |
+
item, for all definitions of `D`, or, if `D` is an unnamed enumeration,
|
| 475 |
+
for all definitions of `D` that are reachable at any given program
|
| 476 |
+
point, the following requirements shall be satisfied.
|
| 477 |
|
| 478 |
- Each such definition shall not be attached to a named module
|
| 479 |
[[module.unit]].
|
| 480 |
- Each such definition shall consist of the same sequence of tokens,
|
| 481 |
where the definition of a closure type is considered to consist of the
|
|
|
|
| 498 |
- In each such definition, except within the default arguments and
|
| 499 |
default template arguments of `D`, corresponding *lambda-expression*s
|
| 500 |
shall have the same closure type (see below).
|
| 501 |
- In each such definition, corresponding entities shall have the same
|
| 502 |
language linkage.
|
| 503 |
+
- In each such definition, const objects with static or thread storage
|
| 504 |
+
duration shall be constant-initialized if the object is
|
| 505 |
+
constant-initialized in any such definition.
|
| 506 |
+
- In each such definition, corresponding manifestly constant-evaluated
|
| 507 |
+
expressions that are not value-dependent shall have the same value
|
| 508 |
+
[[expr.const]], [[temp.dep.constexpr]].
|
| 509 |
- In each such definition, the overloaded operators referred to, the
|
| 510 |
implicit calls to conversion functions, constructors, operator new
|
| 511 |
functions and operator delete functions, shall refer to the same
|
| 512 |
function.
|
| 513 |
- In each such definition, a default argument used by an (implicit or
|
| 514 |
explicit) function call or a default template argument used by an
|
| 515 |
(implicit or explicit) *template-id* or *simple-template-id* is
|
| 516 |
treated as if its token sequence were present in the definition of
|
| 517 |
`D`; that is, the default argument or default template argument is
|
| 518 |
subject to the requirements described in this paragraph (recursively).
|
| 519 |
+
- If `D` is a class with an implicitly-declared constructor
|
| 520 |
+
[[class.default.ctor]], [[class.copy.ctor]], it is as if the
|
| 521 |
constructor was implicitly defined in every translation unit where it
|
| 522 |
is odr-used, and the implicit definition in every translation unit
|
| 523 |
shall call the same constructor for a subobject of `D`.
|
| 524 |
\[*Example 5*:
|
| 525 |
``` cpp
|
|
|
|
| 554 |
implicit definition in every translation unit shall call the same
|
| 555 |
comparison operators for each subobject of `D`.
|
| 556 |
|
| 557 |
If `D` is a template and is defined in more than one translation unit,
|
| 558 |
then the preceding requirements shall apply both to names from the
|
| 559 |
+
template’s enclosing scope used in the template definition, and also to
|
| 560 |
+
dependent names at the point of instantiation [[temp.dep]]. These
|
| 561 |
+
requirements also apply to corresponding entities defined within each
|
| 562 |
+
definition of `D` (including the closure types of *lambda-expression*s,
|
| 563 |
+
but excluding entities defined within default arguments or default
|
| 564 |
+
template arguments of either `D` or an entity not defined within `D`).
|
| 565 |
+
For each such entity and for `D` itself, the behavior is as if there is
|
| 566 |
+
a single entity with a single definition, including in the application
|
| 567 |
+
of these requirements to other entities.
|
|
|
|
| 568 |
|
| 569 |
[*Note 4*: The entity is still declared in multiple translation units,
|
| 570 |
and [[basic.link]] still applies to these declarations. In particular,
|
| 571 |
*lambda-expression*s [[expr.prim.lambda]] appearing in the type of `D`
|
| 572 |
+
can result in the different declarations having distinct types, and
|
| 573 |
+
*lambda-expression*s appearing in a default argument of `D` might still
|
| 574 |
denote different types in different translation units. — *end note*]
|
| 575 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
[*Example 6*:
|
| 577 |
|
| 578 |
``` cpp
|
| 579 |
inline void f(bool cond, void (*p)()) {
|
| 580 |
if (cond) f(false, []{});
|
|
|
|
| 607 |
[[dcl.enum]], those unnamed enumeration types shall be the same; no
|
| 608 |
diagnostic required.
|
| 609 |
|
| 610 |
## Scope <a id="basic.scope">[[basic.scope]]</a>
|
| 611 |
|
| 612 |
+
### General <a id="basic.scope.scope">[[basic.scope.scope]]</a>
|
| 613 |
+
|
| 614 |
+
The declarations in a program appear in a number of *scopes* that are in
|
| 615 |
+
general discontiguous. The *global scope* contains the entire program;
|
| 616 |
+
every other scope S is introduced by a declaration,
|
| 617 |
+
*parameter-declaration-clause*, *statement*, or *handler* (as described
|
| 618 |
+
in the following subclauses of [[basic.scope]]) appearing in another
|
| 619 |
+
scope which thereby contains S. An *enclosing scope* at a program point
|
| 620 |
+
is any scope that contains it; the smallest such scope is said to be the
|
| 621 |
+
*immediate scope* at that point. A scope *intervenes* between a program
|
| 622 |
+
point P and a scope S (that does not contain P) if it is or contains S
|
| 623 |
+
but does not contain P.
|
| 624 |
+
|
| 625 |
+
Unless otherwise specified:
|
| 626 |
+
|
| 627 |
+
- The smallest scope that contains a scope S is the *parent scope* of S.
|
| 628 |
+
- No two declarations (re)introduce the same entity.
|
| 629 |
+
- A declaration *inhabits* the immediate scope at its locus
|
| 630 |
+
[[basic.scope.pdecl]].
|
| 631 |
+
- A declaration’s *target scope* is the scope it inhabits.
|
| 632 |
+
- Any names (re)introduced by a declaration are *bound* to it in its
|
| 633 |
+
target scope.
|
| 634 |
+
|
| 635 |
+
An entity *belongs* to a scope S if S is the target scope of a
|
| 636 |
+
declaration of the entity.
|
| 637 |
+
|
| 638 |
+
[*Note 1*:
|
| 639 |
+
|
| 640 |
+
Special cases include that:
|
| 641 |
+
|
| 642 |
+
- Template parameter scopes are parents only to other template parameter
|
| 643 |
+
scopes [[basic.scope.temp]].
|
| 644 |
+
- Corresponding declarations with appropriate linkage declare the same
|
| 645 |
+
entity [[basic.link]].
|
| 646 |
+
- The declaration in a *template-declaration* inhabits the same scope as
|
| 647 |
+
the *template-declaration*.
|
| 648 |
+
- Friend declarations and declarations of qualified names and template
|
| 649 |
+
specializations do not bind names [[dcl.meaning]]; those with
|
| 650 |
+
qualified names target a specified scope, and other friend
|
| 651 |
+
declarations and certain *elaborated-type-specifier*s
|
| 652 |
+
[[dcl.type.elab]] target a larger enclosing scope.
|
| 653 |
+
- Block-scope extern declarations target a larger enclosing scope but
|
| 654 |
+
bind a name in their immediate scope.
|
| 655 |
+
- The names of unscoped enumerators are bound in the two innermost
|
| 656 |
+
enclosing scopes [[dcl.enum]].
|
| 657 |
+
- A class’s name is also bound in its own scope [[class.pre]].
|
| 658 |
+
- The names of the members of an anonymous union are bound in the
|
| 659 |
+
union’s parent scope [[class.union.anon]].
|
| 660 |
+
|
| 661 |
+
— *end note*]
|
| 662 |
+
|
| 663 |
+
Two non-static member functions have *corresponding object parameters*
|
| 664 |
+
if:
|
| 665 |
+
|
| 666 |
+
- exactly one is an implicit object member function with no
|
| 667 |
+
*ref-qualifier* and the types of their object parameters [[dcl.fct]],
|
| 668 |
+
after removing top-level references, are the same, or
|
| 669 |
+
- their object parameters have the same type.
|
| 670 |
+
|
| 671 |
+
Two non-static member function templates have *corresponding object
|
| 672 |
+
parameters* if:
|
| 673 |
+
|
| 674 |
+
- exactly one is an implicit object member function with no
|
| 675 |
+
*ref-qualifier* and the types of their object parameters, after
|
| 676 |
+
removing any references, are equivalent, or
|
| 677 |
+
- the types of their object parameters are equivalent.
|
| 678 |
+
|
| 679 |
+
Two function templates have *corresponding signatures* if their
|
| 680 |
+
*template-parameter-list*s have the same length, their corresponding
|
| 681 |
+
*template-parameter*s are equivalent, they have equivalent
|
| 682 |
+
non-object-parameter-type-lists and return types (if any), and, if both
|
| 683 |
+
are non-static members, they have corresponding object parameters.
|
| 684 |
+
|
| 685 |
+
Two declarations *correspond* if they (re)introduce the same name, both
|
| 686 |
+
declare constructors, or both declare destructors, unless
|
| 687 |
+
|
| 688 |
+
- either is a *using-declarator*, or
|
| 689 |
+
- one declares a type (not a *typedef-name*) and the other declares a
|
| 690 |
+
variable, non-static data member other than of an anonymous union
|
| 691 |
+
[[class.union.anon]], enumerator, function, or function template, or
|
| 692 |
+
- each declares a function or function template, except when
|
| 693 |
+
- both declare functions with the same
|
| 694 |
+
non-object-parameter-type-list,[^3] equivalent [[temp.over.link]]
|
| 695 |
+
trailing *requires-clause*s (if any, except as specified in
|
| 696 |
+
[[temp.friend]]), and, if both are non-static members, they have
|
| 697 |
+
corresponding object parameters, or
|
| 698 |
+
- both declare function templates with corresponding signatures and
|
| 699 |
+
equivalent *template-head*s and trailing *requires-clause*s (if
|
| 700 |
+
any).
|
| 701 |
+
|
| 702 |
+
[*Note 2*:
|
| 703 |
+
|
| 704 |
+
Declarations can correspond even if neither binds a name.
|
| 705 |
|
| 706 |
[*Example 1*:
|
| 707 |
|
| 708 |
+
``` cpp
|
| 709 |
+
struct A {
|
| 710 |
+
friend void f(); // #1
|
| 711 |
+
};
|
| 712 |
+
struct B {
|
| 713 |
+
friend void f() {} // corresponds to, and defines, #1
|
| 714 |
+
};
|
| 715 |
+
```
|
| 716 |
+
|
| 717 |
+
— *end example*]
|
| 718 |
+
|
| 719 |
+
— *end note*]
|
| 720 |
+
|
| 721 |
+
[*Example 2*:
|
| 722 |
|
| 723 |
``` cpp
|
| 724 |
+
typedef int Int;
|
| 725 |
+
enum E : int { a };
|
| 726 |
+
void f(int); // #1
|
| 727 |
+
void f(Int) {} // defines #1
|
| 728 |
+
void f(E) {} // OK, another overload
|
| 729 |
+
|
| 730 |
+
struct X {
|
| 731 |
+
static void f();
|
| 732 |
+
void f() const; // error: redeclaration
|
| 733 |
+
void g();
|
| 734 |
+
void g() const; // OK
|
| 735 |
+
void g() &; // error: redeclaration
|
| 736 |
+
|
| 737 |
+
void h(this X&, int);
|
| 738 |
+
void h(int) &&; // OK, another overload
|
| 739 |
+
void j(this const X&);
|
| 740 |
+
void j() const &; // error: redeclaration
|
| 741 |
+
void k();
|
| 742 |
+
void k(this X&); // error: redeclaration
|
| 743 |
+
};
|
| 744 |
+
```
|
| 745 |
+
|
| 746 |
+
— *end example*]
|
| 747 |
+
|
| 748 |
+
Two declarations *potentially conflict* if they correspond and cause
|
| 749 |
+
their shared name to denote different entities [[basic.link]]. The
|
| 750 |
+
program is ill-formed if, in any scope, a name is bound to two
|
| 751 |
+
declarations that potentially conflict and one precedes the other
|
| 752 |
+
[[basic.lookup]].
|
| 753 |
+
|
| 754 |
+
[*Note 3*: Overload resolution can consider potentially conflicting
|
| 755 |
+
declarations found in multiple scopes (e.g., via *using-directive*s or
|
| 756 |
+
for operator functions), in which case it is often
|
| 757 |
+
ambiguous. — *end note*]
|
| 758 |
+
|
| 759 |
+
[*Example 3*:
|
| 760 |
+
|
| 761 |
+
``` cpp
|
| 762 |
+
void f() {
|
| 763 |
+
int x,y;
|
| 764 |
+
void x(); // error: different entity for x
|
| 765 |
+
int y; // error: redefinition
|
| 766 |
+
}
|
| 767 |
+
enum { f }; // error: different entity for ::f
|
| 768 |
+
namespace A {}
|
| 769 |
+
namespace B = A;
|
| 770 |
+
namespace B = A; // OK, no effect
|
| 771 |
+
namespace B = B; // OK, no effect
|
| 772 |
+
namespace A = B; // OK, no effect
|
| 773 |
+
namespace B {} // error: different entity for B
|
| 774 |
+
```
|
| 775 |
+
|
| 776 |
+
— *end example*]
|
| 777 |
+
|
| 778 |
+
A declaration is *nominable* in a class, class template, or namespace E
|
| 779 |
+
at a point P if it precedes P, it does not inhabit a block scope, and
|
| 780 |
+
its target scope is the scope associated with E or, if E is a namespace,
|
| 781 |
+
any element of the inline namespace set of E [[namespace.def]].
|
| 782 |
+
|
| 783 |
+
[*Example 4*:
|
| 784 |
+
|
| 785 |
+
``` cpp
|
| 786 |
+
namespace A {
|
| 787 |
+
void f() {void g();}
|
| 788 |
+
inline namespace B {
|
| 789 |
+
struct S {
|
| 790 |
+
friend void h();
|
| 791 |
+
static int i;
|
| 792 |
+
};
|
| 793 |
+
}
|
| 794 |
}
|
| 795 |
```
|
| 796 |
|
| 797 |
+
At the end of this example, the declarations of `f`, `B`, `S`, and `h`
|
| 798 |
+
are nominable in `A`, but those of `g` and `i` are not.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 799 |
|
| 800 |
— *end example*]
|
| 801 |
|
| 802 |
+
When instantiating a templated entity [[temp.pre]], any scope S
|
| 803 |
+
introduced by any part of the template definition is considered to be
|
| 804 |
+
introduced by the instantiated entity and to contain the instantiations
|
| 805 |
+
of any declarations that inhabit S.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 806 |
|
| 807 |
### Point of declaration <a id="basic.scope.pdecl">[[basic.scope.pdecl]]</a>
|
| 808 |
|
| 809 |
+
The *locus* of a declaration [[basic.pre]] that is a declarator is
|
| 810 |
+
immediately after the complete declarator [[dcl.decl]].
|
|
|
|
| 811 |
|
| 812 |
[*Example 1*:
|
| 813 |
|
| 814 |
``` cpp
|
| 815 |
unsigned char x = 12;
|
|
|
|
| 822 |
|
| 823 |
— *end example*]
|
| 824 |
|
| 825 |
[*Note 1*:
|
| 826 |
|
| 827 |
+
A name from an outer scope remains visible up to the locus of the
|
| 828 |
+
declaration that hides it.
|
| 829 |
|
| 830 |
[*Example 2*:
|
| 831 |
|
| 832 |
``` cpp
|
| 833 |
const int i = 2;
|
|
|
|
| 838 |
|
| 839 |
— *end example*]
|
| 840 |
|
| 841 |
— *end note*]
|
| 842 |
|
| 843 |
+
The locus of a *class-specifier* is immediately after the *identifier*
|
| 844 |
+
or *simple-template-id* (if any) in its *class-head* [[class.pre]]. The
|
| 845 |
+
locus of an *enum-specifier* is immediately after its *enum-head*; the
|
| 846 |
+
locus of an *opaque-enum-declaration* is immediately after it
|
| 847 |
+
[[dcl.enum]]. The locus of an *alias-declaration* is immediately after
|
| 848 |
+
it.
|
|
|
|
|
|
|
| 849 |
|
| 850 |
+
The locus of a *using-declarator* that does not name a constructor is
|
| 851 |
+
immediately after the *using-declarator* [[namespace.udecl]].
|
|
|
|
| 852 |
|
| 853 |
+
The locus of an *enumerator-definition* is immediately after it.
|
|
|
|
| 854 |
|
| 855 |
[*Example 3*:
|
| 856 |
|
| 857 |
``` cpp
|
| 858 |
const int x = 12;
|
|
|
|
| 862 |
Here, the enumerator `x` is initialized with the value of the constant
|
| 863 |
`x`, namely 12.
|
| 864 |
|
| 865 |
— *end example*]
|
| 866 |
|
|
|
|
|
|
|
|
|
|
| 867 |
[*Note 2*:
|
| 868 |
|
| 869 |
+
After the declaration of a class member, the member name can be found in
|
| 870 |
+
the scope of its class even if the class is an incomplete class.
|
| 871 |
+
|
| 872 |
+
[*Example 4*:
|
| 873 |
|
| 874 |
``` cpp
|
| 875 |
struct X {
|
| 876 |
enum E { z = 16 };
|
| 877 |
int b[X::z]; // OK
|
| 878 |
};
|
| 879 |
```
|
| 880 |
|
| 881 |
+
— *end example*]
|
| 882 |
+
|
| 883 |
— *end note*]
|
| 884 |
|
| 885 |
+
The locus of an *elaborated-type-specifier* that is a declaration
|
| 886 |
+
[[dcl.type.elab]] is immediately after it.
|
| 887 |
|
| 888 |
+
The locus of an injected-class-name declaration [[class.pre]] is
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 889 |
immediately following the opening brace of the class definition.
|
| 890 |
|
| 891 |
+
The locus of the implicit declaration of a function-local predefined
|
| 892 |
+
variable [[dcl.fct.def.general]] is immediately before the
|
| 893 |
+
*function-body* of its function’s definition.
|
| 894 |
|
| 895 |
+
The locus of the declaration of a structured binding [[dcl.struct.bind]]
|
| 896 |
+
is immediately after the *identifier-list* of the structured binding
|
| 897 |
declaration.
|
| 898 |
|
| 899 |
+
The locus of a *for-range-declaration* of a range-based `for` statement
|
|
|
|
| 900 |
[[stmt.ranged]] is immediately after the *for-range-initializer*.
|
| 901 |
|
| 902 |
+
The locus of a *template-parameter* is immediately after it.
|
|
|
|
| 903 |
|
| 904 |
+
[*Example 5*:
|
| 905 |
|
| 906 |
``` cpp
|
| 907 |
typedef unsigned char T;
|
| 908 |
template<class T
|
| 909 |
+
= T // lookup finds the typedef-name
|
| 910 |
, T // lookup finds the template parameter
|
| 911 |
N = 0> struct A { };
|
| 912 |
```
|
| 913 |
|
| 914 |
— *end example*]
|
| 915 |
|
| 916 |
+
The locus of a *concept-definition* is immediately after its
|
| 917 |
+
concept-name [[temp.concept]].
|
| 918 |
+
|
| 919 |
+
[*Note 3*: The *constraint-expression* cannot use the
|
| 920 |
+
*concept-name*. — *end note*]
|
| 921 |
+
|
| 922 |
+
The locus of a *namespace-definition* with an *identifier* is
|
| 923 |
+
immediately after the *identifier*.
|
| 924 |
+
|
| 925 |
+
[*Note 4*: An identifier is invented for an
|
| 926 |
+
*unnamed-namespace-definition* [[namespace.unnamed]]. — *end note*]
|
| 927 |
+
|
| 928 |
+
[*Note 5*: Friend declarations can introduce functions or classes that
|
| 929 |
+
belong to the nearest enclosing namespace or block scope, but they do
|
| 930 |
+
not bind names anywhere [[class.friend]]. Function declarations at block
|
| 931 |
+
scope and variable declarations with the `extern` specifier at block
|
| 932 |
+
scope declare entities that belong to the nearest enclosing namespace,
|
| 933 |
+
but they do not bind names in it. — *end note*]
|
| 934 |
|
| 935 |
[*Note 6*: For point of instantiation of a template, see
|
| 936 |
[[temp.point]]. — *end note*]
|
| 937 |
|
| 938 |
### Block scope <a id="basic.scope.block">[[basic.scope.block]]</a>
|
| 939 |
|
| 940 |
+
Each
|
| 941 |
+
|
| 942 |
+
- selection or iteration statement [[stmt.select]], [[stmt.iter]],
|
| 943 |
+
- substatement of such a statement,
|
| 944 |
+
- *handler* [[except.pre]], or
|
| 945 |
+
- compound statement [[stmt.block]] that is not the *compound-statement*
|
| 946 |
+
of a *handler*
|
| 947 |
+
|
| 948 |
+
introduces a *block scope* that includes that statement or *handler*.
|
| 949 |
+
|
| 950 |
+
[*Note 1*: A substatement that is also a block has only one
|
| 951 |
+
scope. — *end note*]
|
| 952 |
+
|
| 953 |
+
A variable that belongs to a block scope is a *block variable*.
|
|
|
|
| 954 |
|
| 955 |
[*Example 1*:
|
| 956 |
|
| 957 |
+
``` cpp
|
| 958 |
+
int i = 42;
|
| 959 |
+
int a[10];
|
| 960 |
+
|
| 961 |
+
for (int i = 0; i < 10; i++)
|
| 962 |
+
a[i] = i;
|
| 963 |
+
|
| 964 |
+
int j = i; // j = 42
|
| 965 |
+
```
|
| 966 |
+
|
| 967 |
+
— *end example*]
|
| 968 |
+
|
| 969 |
+
If a declaration whose target scope is the block scope S of a
|
| 970 |
+
|
| 971 |
+
- *compound-statement* of a *lambda-expression*, *function-body*, or
|
| 972 |
+
*function-try-block*,
|
| 973 |
+
- substatement of a selection or iteration statement that is not itself
|
| 974 |
+
a selection or iteration statement, or
|
| 975 |
+
- *handler* of a *function-try-block*
|
| 976 |
+
|
| 977 |
+
potentially conflicts with a declaration whose target scope is the
|
| 978 |
+
parent scope of S, the program is ill-formed.
|
| 979 |
+
|
| 980 |
+
[*Example 2*:
|
| 981 |
+
|
| 982 |
``` cpp
|
| 983 |
if (int x = f()) {
|
| 984 |
int x; // error: redeclaration of x
|
| 985 |
}
|
| 986 |
else {
|
|
|
|
| 990 |
|
| 991 |
— *end example*]
|
| 992 |
|
| 993 |
### Function parameter scope <a id="basic.scope.param">[[basic.scope.param]]</a>
|
| 994 |
|
| 995 |
+
A *parameter-declaration-clause* P introduces a
|
| 996 |
+
*function parameter scope* that includes P.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 997 |
|
| 998 |
+
[*Note 1*: A function parameter cannot be used for its value within the
|
| 999 |
+
*parameter-declaration-clause* [[dcl.fct.default]]. — *end note*]
|
| 1000 |
|
| 1001 |
+
- If P is associated with a *declarator* and is preceded by a
|
| 1002 |
+
(possibly-parenthesized) *noptr-declarator* of the form
|
| 1003 |
+
*declarator-id* *attribute-specifier-seq*ₒₚₜ , its scope extends to
|
| 1004 |
+
the end of the nearest enclosing *init-declarator*,
|
| 1005 |
+
*member-declarator*, *declarator* of a *parameter-declaration* or a
|
| 1006 |
+
*nodeclspec-function-declaration*, or *function-definition*, but does
|
| 1007 |
+
not include the locus of the associated *declarator*. \[*Note 2*: In
|
| 1008 |
+
this case, P declares the parameters of a function (or a function or
|
| 1009 |
+
template parameter declared with function type). A member function’s
|
| 1010 |
+
parameter scope is nested within its class’s scope. — *end note*]
|
| 1011 |
+
- If P is associated with a *lambda-declarator*, its scope extends to
|
| 1012 |
+
the end of the *compound-statement* in the *lambda-expression*.
|
| 1013 |
+
- If P is associated with a *requirement-parameter-list*, its scope
|
| 1014 |
+
extends to the end of the *requirement-body* of the
|
| 1015 |
+
requires-expression.
|
| 1016 |
+
- If P is associated with a *deduction-guide*, its scope extends to the
|
| 1017 |
+
end of the *deduction-guide*.
|
| 1018 |
+
|
| 1019 |
+
### Lambda scope <a id="basic.scope.lambda">[[basic.scope.lambda]]</a>
|
| 1020 |
+
|
| 1021 |
+
A *lambda-expression* `E` introduces a *lambda scope* that starts
|
| 1022 |
+
immediately after the *lambda-introducer* of `E` and extends to the end
|
| 1023 |
+
of the *compound-statement* of `E`.
|
| 1024 |
|
| 1025 |
### Namespace scope <a id="basic.scope.namespace">[[basic.scope.namespace]]</a>
|
| 1026 |
|
| 1027 |
+
Any *namespace-definition* for a namespace N introduces a
|
| 1028 |
+
*namespace scope* that includes the *namespace-body* for every
|
| 1029 |
+
*namespace-definition* for N. For each non-friend redeclaration or
|
| 1030 |
+
specialization whose target scope is or is contained by the scope, the
|
| 1031 |
+
portion after the *declarator-id*, *class-head-name*, or
|
| 1032 |
+
*enum-head-name* is also included in the scope. The global scope is the
|
| 1033 |
+
namespace scope of the global namespace [[basic.namespace]].
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1034 |
|
| 1035 |
[*Example 1*:
|
| 1036 |
|
| 1037 |
``` cpp
|
| 1038 |
+
namespace Q {
|
| 1039 |
+
namespace V { void f(); }
|
| 1040 |
+
void V::f() { // in the scope of V
|
| 1041 |
+
void h(); // declares Q::V::h
|
|
|
|
| 1042 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1043 |
}
|
| 1044 |
```
|
| 1045 |
|
| 1046 |
— *end example*]
|
| 1047 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1048 |
### Class scope <a id="basic.scope.class">[[basic.scope.class]]</a>
|
| 1049 |
|
| 1050 |
+
Any declaration of a class or class template C introduces a
|
| 1051 |
+
*class scope* that includes the *member-specification* of the
|
| 1052 |
+
*class-specifier* for C (if any). For each non-friend redeclaration or
|
| 1053 |
+
specialization whose target scope is or is contained by the scope, the
|
| 1054 |
+
portion after the *declarator-id*, *class-head-name*, or
|
| 1055 |
+
*enum-head-name* is also included in the scope.
|
| 1056 |
|
| 1057 |
+
[*Note 1*:
|
|
|
|
|
|
|
| 1058 |
|
| 1059 |
+
Lookup from a program point before the *class-specifier* of a class will
|
| 1060 |
+
find no bindings in the class scope.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1061 |
|
| 1062 |
[*Example 1*:
|
| 1063 |
|
| 1064 |
``` cpp
|
| 1065 |
+
template<class D>
|
| 1066 |
+
struct B {
|
| 1067 |
+
D::type x; // #1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1068 |
};
|
| 1069 |
|
| 1070 |
+
struct A { using type = int; };
|
| 1071 |
+
struct C : A, B<C> {}; // error at #1: C::type not found
|
|
|
|
|
|
|
| 1072 |
```
|
| 1073 |
|
| 1074 |
— *end example*]
|
| 1075 |
|
| 1076 |
+
— *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1077 |
|
| 1078 |
### Enumeration scope <a id="basic.scope.enum">[[basic.scope.enum]]</a>
|
| 1079 |
|
| 1080 |
+
Any declaration of an enumeration E introduces an *enumeration scope*
|
| 1081 |
+
that includes the *enumerator-list* of the *enum-specifier* for E (if
|
| 1082 |
+
any).
|
| 1083 |
|
| 1084 |
### Template parameter scope <a id="basic.scope.temp">[[basic.scope.temp]]</a>
|
| 1085 |
|
| 1086 |
+
Each template *template-parameter* introduces a
|
| 1087 |
+
*template parameter scope* that includes the *template-head* of the
|
| 1088 |
+
*template-parameter*.
|
| 1089 |
+
|
| 1090 |
+
Each *template-declaration* D introduces a template parameter scope that
|
| 1091 |
+
extends from the beginning of its *template-parameter-list* to the end
|
| 1092 |
+
of the *template-declaration*. Any declaration outside the
|
| 1093 |
+
*template-parameter-list* that would inhabit that scope instead inhabits
|
| 1094 |
+
the same scope as D. The parent scope of any scope S that is not a
|
| 1095 |
+
template parameter scope is the smallest scope that contains S and is
|
| 1096 |
+
not a template parameter scope.
|
| 1097 |
+
|
| 1098 |
+
[*Note 1*: Therefore, only template parameters belong to a template
|
| 1099 |
+
parameter scope, and only template parameter scopes have a template
|
| 1100 |
+
parameter scope as a parent scope. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1101 |
|
| 1102 |
## Name lookup <a id="basic.lookup">[[basic.lookup]]</a>
|
| 1103 |
|
| 1104 |
+
### General <a id="basic.lookup.general">[[basic.lookup.general]]</a>
|
| 1105 |
+
|
| 1106 |
The name lookup rules apply uniformly to all names (including
|
| 1107 |
*typedef-name*s [[dcl.typedef]], *namespace-name*s [[basic.namespace]],
|
| 1108 |
and *class-name*s [[class.name]]) wherever the grammar allows such names
|
| 1109 |
in the context discussed by a particular rule. Name lookup associates
|
| 1110 |
the use of a name with a set of declarations [[basic.def]] of that name.
|
| 1111 |
+
Unless otherwise specified, the program is ill-formed if no declarations
|
| 1112 |
+
are found. If the declarations found by name lookup all denote functions
|
| 1113 |
+
or function templates, the declarations are said to form an *overload
|
| 1114 |
+
set*. Otherwise, if the declarations found by name lookup do not all
|
| 1115 |
+
denote the same entity, they are *ambiguous* and the program is
|
| 1116 |
+
ill-formed. Overload resolution [[over.match]], [[over.over]] takes
|
| 1117 |
+
place after name lookup has succeeded. The access rules [[class.access]]
|
| 1118 |
+
are considered only once name lookup and function overload resolution
|
| 1119 |
+
(if applicable) have succeeded. Only after name lookup, function
|
| 1120 |
+
overload resolution (if applicable) and access checking have succeeded
|
| 1121 |
+
are the semantic properties introduced by the declarations used in
|
| 1122 |
+
further processing.
|
| 1123 |
+
|
| 1124 |
+
A program point P is said to follow any declaration in the same
|
| 1125 |
+
translation unit whose locus [[basic.scope.pdecl]] is before P.
|
| 1126 |
+
|
| 1127 |
+
[*Note 1*: The declaration might appear in a scope that does not
|
| 1128 |
+
contain P. — *end note*]
|
| 1129 |
+
|
| 1130 |
+
A declaration X *precedes* a program point P in a translation unit L if
|
| 1131 |
+
P follows X, X inhabits a class scope and is reachable from P, or else X
|
| 1132 |
+
appears in a translation unit D and
|
| 1133 |
+
|
| 1134 |
+
- P follows a *module-import-declaration* or *module-declaration* that
|
| 1135 |
+
imports D (directly or indirectly), and
|
| 1136 |
+
- X appears after the *module-declaration* in D (if any) and before the
|
| 1137 |
+
*private-module-fragment* in D (if any), and
|
| 1138 |
+
- either X is exported or else D and L are part of the same module and X
|
| 1139 |
+
does not inhabit a namespace with internal linkage or declare a name
|
| 1140 |
+
with internal linkage. \[*Note 2*: Names declared by a
|
| 1141 |
+
*using-declaration* have no linkage. — *end note*]
|
| 1142 |
+
|
| 1143 |
+
[*Note 3*:
|
| 1144 |
+
|
| 1145 |
+
A *module-import-declaration* imports both the named translation unit(s)
|
| 1146 |
+
and any modules named by exported *module-import-declaration*s within
|
| 1147 |
+
them, recursively.
|
| 1148 |
+
|
| 1149 |
+
[*Example 1*:
|
| 1150 |
+
|
| 1151 |
+
Translation unit #1
|
| 1152 |
+
|
| 1153 |
+
``` cpp
|
| 1154 |
+
export module Q;
|
| 1155 |
+
export int sq(int i) { return i*i; }
|
| 1156 |
+
```
|
| 1157 |
+
|
| 1158 |
+
Translation unit #2
|
| 1159 |
+
|
| 1160 |
+
``` cpp
|
| 1161 |
+
export module R;
|
| 1162 |
+
export import Q;
|
| 1163 |
+
```
|
| 1164 |
+
|
| 1165 |
+
Translation unit #3
|
| 1166 |
+
|
| 1167 |
+
``` cpp
|
| 1168 |
+
import R;
|
| 1169 |
+
int main() { return sq(9); } // OK, sq from module Q
|
| 1170 |
+
```
|
| 1171 |
+
|
| 1172 |
+
— *end example*]
|
| 1173 |
+
|
| 1174 |
+
— *end note*]
|
| 1175 |
+
|
| 1176 |
+
A *single search* in a scope S for a name N from a program point P finds
|
| 1177 |
+
all declarations that precede P to which any name that is the same as N
|
| 1178 |
+
[[basic.pre]] is bound in S. If any such declaration is a
|
| 1179 |
+
*using-declarator* whose terminal name [[expr.prim.id.unqual]] is not
|
| 1180 |
+
dependent [[temp.dep.type]], it is replaced by the declarations named by
|
| 1181 |
+
the *using-declarator* [[namespace.udecl]].
|
| 1182 |
+
|
| 1183 |
+
In certain contexts, only certain kinds of declarations are included.
|
| 1184 |
+
After any such restriction, any declarations of classes or enumerations
|
| 1185 |
+
are discarded if any other declarations are found.
|
| 1186 |
+
|
| 1187 |
+
[*Note 4*: A type (but not a *typedef-name* or template) is therefore
|
| 1188 |
+
hidden by any other entity in its scope. — *end note*]
|
| 1189 |
+
|
| 1190 |
+
However, if a lookup is *type-only*, only declarations of types and
|
| 1191 |
+
templates whose specializations are types are considered; furthermore,
|
| 1192 |
+
if declarations of a *typedef-name* and of the type to which it refers
|
| 1193 |
+
are found, the declaration of the *typedef-name* is discarded instead of
|
| 1194 |
+
the type declaration.
|
| 1195 |
+
|
| 1196 |
+
### Member name lookup <a id="class.member.lookup">[[class.member.lookup]]</a>
|
| 1197 |
+
|
| 1198 |
+
A *search* in a scope X for a name M from a program point P is a single
|
| 1199 |
+
search in X for M from P unless X is the scope of a class or class
|
| 1200 |
+
template T, in which case the following steps define the result of the
|
| 1201 |
+
search.
|
| 1202 |
+
|
| 1203 |
+
[*Note 1*: The result differs only if M is a *conversion-function-id*
|
| 1204 |
+
or if the single search would find nothing. — *end note*]
|
| 1205 |
+
|
| 1206 |
+
The *lookup set* for a name N in a class or class template C, called
|
| 1207 |
+
S(N,C), consists of two component sets: the *declaration set*, a set of
|
| 1208 |
+
members named N; and the *subobject set*, a set of subobjects where
|
| 1209 |
+
declarations of these members were found (possibly via
|
| 1210 |
+
*using-declaration*s). In the declaration set, type declarations
|
| 1211 |
+
(including injected-class-names) are replaced by the types they
|
| 1212 |
+
designate. S(N,C) is calculated as follows:
|
| 1213 |
+
|
| 1214 |
+
The declaration set is the result of a single search in the scope of C
|
| 1215 |
+
for N from immediately after the *class-specifier* of C if P is in a
|
| 1216 |
+
complete-class context of C or from P otherwise. If the resulting
|
| 1217 |
+
declaration set is not empty, the subobject set contains C itself, and
|
| 1218 |
+
calculation is complete.
|
| 1219 |
+
|
| 1220 |
+
Otherwise (i.e., C does not contain a declaration of N or the resulting
|
| 1221 |
+
declaration set is empty), S(N,C) is initially empty. Calculate the
|
| 1222 |
+
lookup set for N in each direct non-dependent [[temp.dep.type]] base
|
| 1223 |
+
class subobject Bᵢ, and merge each such lookup set S(N,Bᵢ) in turn into
|
| 1224 |
+
S(N,C).
|
| 1225 |
+
|
| 1226 |
+
[*Note 2*: If C is incomplete, only base classes whose *base-specifier*
|
| 1227 |
+
appears before P are considered. If C is an instantiated class, its base
|
| 1228 |
+
classes are not dependent. — *end note*]
|
| 1229 |
+
|
| 1230 |
+
The following steps define the result of merging lookup set S(N,Bᵢ) into
|
| 1231 |
+
the intermediate S(N,C):
|
| 1232 |
+
|
| 1233 |
+
- If each of the subobject members of S(N,Bᵢ) is a base class subobject
|
| 1234 |
+
of at least one of the subobject members of S(N,C), or if S(N,Bᵢ) is
|
| 1235 |
+
empty, S(N,C) is unchanged and the merge is complete. Conversely, if
|
| 1236 |
+
each of the subobject members of S(N,C) is a base class subobject of
|
| 1237 |
+
at least one of the subobject members of S(N,Bᵢ), or if S(N,C) is
|
| 1238 |
+
empty, the new S(N,C) is a copy of S(N,Bᵢ).
|
| 1239 |
+
- Otherwise, if the declaration sets of S(N,Bᵢ) and S(N,C) differ, the
|
| 1240 |
+
merge is ambiguous: the new S(N,C) is a lookup set with an invalid
|
| 1241 |
+
declaration set and the union of the subobject sets. In subsequent
|
| 1242 |
+
merges, an invalid declaration set is considered different from any
|
| 1243 |
+
other.
|
| 1244 |
+
- Otherwise, the new S(N,C) is a lookup set with the shared set of
|
| 1245 |
+
declarations and the union of the subobject sets.
|
| 1246 |
+
|
| 1247 |
+
The result of the search is the declaration set of S(M,T). If it is an
|
| 1248 |
+
invalid set, the program is ill-formed. If it differs from the result of
|
| 1249 |
+
a search in T for M in a complete-class context [[class.mem]] of T, the
|
| 1250 |
+
program is ill-formed, no diagnostic required.
|
| 1251 |
+
|
| 1252 |
+
[*Example 1*:
|
| 1253 |
+
|
| 1254 |
+
``` cpp
|
| 1255 |
+
struct A { int x; }; // S(x,A) = { { A::x }, { A } }
|
| 1256 |
+
struct B { float x; }; // S(x,B) = { { B::x }, { B } }
|
| 1257 |
+
struct C: public A, public B { }; // S(x,C) = { invalid, { A in C, B in C } }
|
| 1258 |
+
struct D: public virtual C { }; // S(x,D) = S(x,C)
|
| 1259 |
+
struct E: public virtual C { char x; }; // S(x,E) = { { E::x }, { E } }
|
| 1260 |
+
struct F: public D, public E { }; // S(x,F) = S(x,E)
|
| 1261 |
+
int main() {
|
| 1262 |
+
F f;
|
| 1263 |
+
f.x = 0; // OK, lookup finds E::x
|
| 1264 |
+
}
|
| 1265 |
+
```
|
| 1266 |
+
|
| 1267 |
+
S(`x`,`F`) is unambiguous because the `A` and `B` base class subobjects
|
| 1268 |
+
of `D` are also base class subobjects of `E`, so S(`x`,`D`) is discarded
|
| 1269 |
+
in the first merge step.
|
| 1270 |
+
|
| 1271 |
+
— *end example*]
|
| 1272 |
+
|
| 1273 |
+
If M is a non-dependent *conversion-function-id*, conversion function
|
| 1274 |
+
templates that are members of T are considered. For each such template
|
| 1275 |
+
F, the lookup set S(t,T) is constructed, considering a function template
|
| 1276 |
+
declaration to have the name t only if it corresponds to a declaration
|
| 1277 |
+
of F [[basic.scope.scope]]. The members of the declaration set of each
|
| 1278 |
+
such lookup set, which shall not be an invalid set, are included in the
|
| 1279 |
+
result.
|
| 1280 |
+
|
| 1281 |
+
[*Note 3*: Overload resolution will discard those that cannot convert
|
| 1282 |
+
to the type specified by M [[temp.over]]. — *end note*]
|
| 1283 |
+
|
| 1284 |
+
[*Note 4*: A static member, a nested type or an enumerator defined in a
|
| 1285 |
+
base class `T` can unambiguously be found even if an object has more
|
| 1286 |
+
than one base class subobject of type `T`. Two base class subobjects
|
| 1287 |
+
share the non-static member subobjects of their common virtual base
|
| 1288 |
+
classes. — *end note*]
|
| 1289 |
+
|
| 1290 |
+
[*Example 2*:
|
| 1291 |
+
|
| 1292 |
+
``` cpp
|
| 1293 |
+
struct V {
|
| 1294 |
+
int v;
|
| 1295 |
+
};
|
| 1296 |
+
struct A {
|
| 1297 |
+
int a;
|
| 1298 |
+
static int s;
|
| 1299 |
+
enum { e };
|
| 1300 |
+
};
|
| 1301 |
+
struct B : A, virtual V { };
|
| 1302 |
+
struct C : A, virtual V { };
|
| 1303 |
+
struct D : B, C { };
|
| 1304 |
+
|
| 1305 |
+
void f(D* pd) {
|
| 1306 |
+
pd->v++; // OK, only one v (virtual)
|
| 1307 |
+
pd->s++; // OK, only one s (static)
|
| 1308 |
+
int i = pd->e; // OK, only one e (enumerator)
|
| 1309 |
+
pd->a++; // error: ambiguous: two a{s} in D
|
| 1310 |
+
}
|
| 1311 |
+
```
|
| 1312 |
+
|
| 1313 |
+
— *end example*]
|
| 1314 |
+
|
| 1315 |
+
[*Note 5*: When virtual base classes are used, a hidden declaration
|
| 1316 |
+
can be reached along a path through the subobject lattice that does not
|
| 1317 |
+
pass through the hiding declaration. This is not an ambiguity. The
|
| 1318 |
+
identical use with non-virtual base classes is an ambiguity; in that
|
| 1319 |
+
case there is no unique instance of the name that hides all the
|
| 1320 |
+
others. — *end note*]
|
| 1321 |
+
|
| 1322 |
+
[*Example 3*:
|
| 1323 |
+
|
| 1324 |
+
``` cpp
|
| 1325 |
+
struct V { int f(); int x; };
|
| 1326 |
+
struct W { int g(); int y; };
|
| 1327 |
+
struct B : virtual V, W {
|
| 1328 |
+
int f(); int x;
|
| 1329 |
+
int g(); int y;
|
| 1330 |
+
};
|
| 1331 |
+
struct C : virtual V, W { };
|
| 1332 |
+
|
| 1333 |
+
struct D : B, C { void glorp(); };
|
| 1334 |
+
```
|
| 1335 |
+
|
| 1336 |
+
<a id="fig:class.lookup"></a>
|
| 1337 |
+
|
| 1338 |
+
![Name lookup \[fig:class.lookup\]](images/figname.svg)
|
| 1339 |
+
|
| 1340 |
+
As illustrated in Figure [[fig:class.lookup]], the names declared in
|
| 1341 |
+
`V` and the left-hand instance of `W` are hidden by those in `B`, but
|
| 1342 |
+
the names declared in the right-hand instance of `W` are not hidden at
|
| 1343 |
+
all.
|
| 1344 |
+
|
| 1345 |
+
``` cpp
|
| 1346 |
+
void D::glorp() {
|
| 1347 |
+
x++; // OK, B::x hides V::x
|
| 1348 |
+
f(); // OK, B::f() hides V::f()
|
| 1349 |
+
y++; // error: B::y and C's W::y
|
| 1350 |
+
g(); // error: B::g() and C's W::g()
|
| 1351 |
+
}
|
| 1352 |
+
```
|
| 1353 |
+
|
| 1354 |
+
— *end example*]
|
| 1355 |
+
|
| 1356 |
+
An explicit or implicit conversion from a pointer to or an expression
|
| 1357 |
+
designating an object of a derived class to a pointer or reference to
|
| 1358 |
+
one of its base classes shall unambiguously refer to a unique object
|
| 1359 |
+
representing the base class.
|
| 1360 |
+
|
| 1361 |
+
[*Example 4*:
|
| 1362 |
+
|
| 1363 |
+
``` cpp
|
| 1364 |
+
struct V { };
|
| 1365 |
+
struct A { };
|
| 1366 |
+
struct B : A, virtual V { };
|
| 1367 |
+
struct C : A, virtual V { };
|
| 1368 |
+
struct D : B, C { };
|
| 1369 |
+
|
| 1370 |
+
void g() {
|
| 1371 |
+
D d;
|
| 1372 |
+
B* pb = &d;
|
| 1373 |
+
A* pa = &d; // error: ambiguous: C's A or B's A?
|
| 1374 |
+
V* pv = &d; // OK, only one V subobject
|
| 1375 |
+
}
|
| 1376 |
+
```
|
| 1377 |
+
|
| 1378 |
+
— *end example*]
|
| 1379 |
+
|
| 1380 |
+
[*Note 6*: Even if the result of name lookup is unambiguous, use of a
|
| 1381 |
+
name found in multiple subobjects might still be ambiguous
|
| 1382 |
+
[[conv.mem]], [[expr.ref]], [[class.access.base]]. — *end note*]
|
| 1383 |
+
|
| 1384 |
+
[*Example 5*:
|
| 1385 |
+
|
| 1386 |
+
``` cpp
|
| 1387 |
+
struct B1 {
|
| 1388 |
+
void f();
|
| 1389 |
+
static void f(int);
|
| 1390 |
+
int i;
|
| 1391 |
+
};
|
| 1392 |
+
struct B2 {
|
| 1393 |
+
void f(double);
|
| 1394 |
+
};
|
| 1395 |
+
struct I1: B1 { };
|
| 1396 |
+
struct I2: B1 { };
|
| 1397 |
+
|
| 1398 |
+
struct D: I1, I2, B2 {
|
| 1399 |
+
using B1::f;
|
| 1400 |
+
using B2::f;
|
| 1401 |
+
void g() {
|
| 1402 |
+
f(); // Ambiguous conversion of this
|
| 1403 |
+
f(0); // Unambiguous (static)
|
| 1404 |
+
f(0.0); // Unambiguous (only one B2)
|
| 1405 |
+
int B1::* mpB1 = &D::i; // Unambiguous
|
| 1406 |
+
int D::* mpD = &D::i; // Ambiguous conversion
|
| 1407 |
+
}
|
| 1408 |
+
};
|
| 1409 |
+
```
|
| 1410 |
+
|
| 1411 |
+
— *end example*]
|
| 1412 |
|
| 1413 |
### Unqualified name lookup <a id="basic.lookup.unqual">[[basic.lookup.unqual]]</a>
|
| 1414 |
|
| 1415 |
+
A *using-directive* is *active* in a scope S at a program point P if it
|
| 1416 |
+
precedes P and inhabits either S or the scope of a namespace nominated
|
| 1417 |
+
by a *using-directive* that is active in S at P.
|
| 1418 |
+
|
| 1419 |
+
An *unqualified search* in a scope S from a program point P includes the
|
| 1420 |
+
results of searches from P in
|
| 1421 |
+
|
| 1422 |
+
- S, and
|
| 1423 |
+
- for any scope U that contains P and is or is contained by S, each
|
| 1424 |
+
namespace contained by S that is nominated by a *using-directive* that
|
| 1425 |
+
is active in U at P.
|
| 1426 |
+
|
| 1427 |
+
If no declarations are found, the results of the unqualified search are
|
| 1428 |
+
the results of an unqualified search in the parent scope of S, if any,
|
| 1429 |
+
from P.
|
| 1430 |
+
|
| 1431 |
+
[*Note 1*: When a class scope is searched, the scopes of its base
|
| 1432 |
+
classes are also searched [[class.member.lookup]]. If it inherits from a
|
| 1433 |
+
single base, it is as if the scope of the base immediately contains the
|
| 1434 |
+
scope of the derived class. Template parameter scopes that are
|
| 1435 |
+
associated with one scope in the chain of parents are also considered
|
| 1436 |
+
[[temp.local]]. — *end note*]
|
| 1437 |
+
|
| 1438 |
+
*Unqualified name lookup* from a program point performs an unqualified
|
| 1439 |
+
search in its immediate scope.
|
| 1440 |
+
|
| 1441 |
+
An *unqualified name* is a name that does not immediately follow a
|
| 1442 |
+
*nested-name-specifier* or the `.` or `->` in a class member access
|
| 1443 |
+
expression [[expr.ref]], possibly after a `template` keyword or `~`.
|
| 1444 |
+
Unless otherwise specified, such a name undergoes unqualified name
|
| 1445 |
+
lookup from the point where it appears.
|
| 1446 |
+
|
| 1447 |
+
An unqualified name that is a component name [[expr.prim.id.unqual]] of
|
| 1448 |
+
a *type-specifier* or *ptr-operator* of a *conversion-type-id* is looked
|
| 1449 |
+
up in the same fashion as the *conversion-function-id* in which it
|
| 1450 |
+
appears. If that lookup finds nothing, it undergoes unqualified name
|
| 1451 |
+
lookup; in each case, only names that denote types or templates whose
|
| 1452 |
+
specializations are types are considered.
|
| 1453 |
+
|
| 1454 |
+
[*Example 1*:
|
| 1455 |
+
|
| 1456 |
+
``` cpp
|
| 1457 |
+
struct T1 { struct U { int i; }; };
|
| 1458 |
+
struct T2 { };
|
| 1459 |
+
struct U1 {};
|
| 1460 |
+
struct U2 {};
|
| 1461 |
+
|
| 1462 |
+
struct B {
|
| 1463 |
+
using T = T1;
|
| 1464 |
+
using U = U1;
|
| 1465 |
+
operator U1 T1::*();
|
| 1466 |
+
operator U1 T2::*();
|
| 1467 |
+
operator U2 T1::*();
|
| 1468 |
+
operator U2 T2::*();
|
| 1469 |
+
};
|
| 1470 |
+
|
| 1471 |
+
template<class X, class T>
|
| 1472 |
+
int g() {
|
| 1473 |
+
using U = U2;
|
| 1474 |
+
X().operator U T::*(); // #1, searches for T in the scope of X first
|
| 1475 |
+
X().operator U decltype(T())::*(); // #2
|
| 1476 |
+
return 0;
|
| 1477 |
+
}
|
| 1478 |
+
int x = g<B, T2>(); // #1 calls B::operator U1 T1::*
|
| 1479 |
+
// #2 calls B::operator U1 T2::*
|
| 1480 |
+
```
|
| 1481 |
+
|
| 1482 |
+
— *end example*]
|
| 1483 |
+
|
| 1484 |
+
In a friend declaration *declarator* whose *declarator-id* is a
|
| 1485 |
+
*qualified-id* whose lookup context [[basic.lookup.qual]] is a class or
|
| 1486 |
+
namespace S, lookup for an unqualified name that appears after the
|
| 1487 |
+
*declarator-id* performs a search in the scope associated with S. If
|
| 1488 |
+
that lookup finds nothing, it undergoes unqualified name lookup.
|
| 1489 |
+
|
| 1490 |
+
[*Example 2*:
|
| 1491 |
+
|
| 1492 |
+
``` cpp
|
| 1493 |
+
using I = int;
|
| 1494 |
+
using D = double;
|
| 1495 |
+
namespace A {
|
| 1496 |
+
inline namespace N {using C = char; }
|
| 1497 |
+
using F = float;
|
| 1498 |
+
void f(I);
|
| 1499 |
+
void f(D);
|
| 1500 |
+
void f(C);
|
| 1501 |
+
void f(F);
|
| 1502 |
+
}
|
| 1503 |
+
struct X0 {using F = float; };
|
| 1504 |
+
struct W {
|
| 1505 |
+
using D = void;
|
| 1506 |
+
struct X : X0 {
|
| 1507 |
+
void g(I);
|
| 1508 |
+
void g(::D);
|
| 1509 |
+
void g(F);
|
| 1510 |
+
};
|
| 1511 |
+
};
|
| 1512 |
+
namespace B {
|
| 1513 |
+
typedef short I, F;
|
| 1514 |
+
class Y {
|
| 1515 |
+
friend void A::f(I); // error: no void A::f(short)
|
| 1516 |
+
friend void A::f(D); // OK
|
| 1517 |
+
friend void A::f(C); // error: A::N::C not found
|
| 1518 |
+
friend void A::f(F); // OK
|
| 1519 |
+
friend void W::X::g(I); // error: no void X::g(short)
|
| 1520 |
+
friend void W::X::g(D); // OK
|
| 1521 |
+
friend void W::X::g(F); // OK
|
| 1522 |
+
};
|
| 1523 |
+
}
|
| 1524 |
+
```
|
| 1525 |
+
|
| 1526 |
+
— *end example*]
|
| 1527 |
+
|
| 1528 |
+
### Argument-dependent name lookup <a id="basic.lookup.argdep">[[basic.lookup.argdep]]</a>
|
| 1529 |
+
|
| 1530 |
+
When the *postfix-expression* in a function call [[expr.call]] is an
|
| 1531 |
+
*unqualified-id*, and unqualified lookup [[basic.lookup.unqual]] for the
|
| 1532 |
+
name in the *unqualified-id* does not find any
|
| 1533 |
+
|
| 1534 |
+
- declaration of a class member, or
|
| 1535 |
+
- function declaration inhabiting a block scope, or
|
| 1536 |
+
- declaration not of a function or function template
|
| 1537 |
+
|
| 1538 |
+
then lookup for the name also includes the result of
|
| 1539 |
+
*argument-dependent lookup* in a set of associated namespaces that
|
| 1540 |
+
depends on the types of the arguments (and for template template
|
| 1541 |
+
arguments, the namespace of the template argument), as specified below.
|
| 1542 |
+
|
| 1543 |
+
[*Example 1*:
|
| 1544 |
+
|
| 1545 |
+
``` cpp
|
| 1546 |
+
namespace N {
|
| 1547 |
+
struct S { };
|
| 1548 |
+
void f(S);
|
| 1549 |
+
}
|
| 1550 |
+
|
| 1551 |
+
void g() {
|
| 1552 |
+
N::S s;
|
| 1553 |
+
f(s); // OK, calls N::f
|
| 1554 |
+
(f)(s); // error: N::f not considered; parentheses prevent argument-dependent lookup
|
| 1555 |
+
}
|
| 1556 |
+
```
|
| 1557 |
+
|
| 1558 |
+
— *end example*]
|
| 1559 |
|
| 1560 |
[*Note 1*:
|
| 1561 |
|
| 1562 |
For purposes of determining (during parsing) whether an expression is a
|
| 1563 |
*postfix-expression* for a function call, the usual name lookup rules
|
|
|
|
| 1573 |
template <class T> int f(T);
|
| 1574 |
template <class T> int g(T);
|
| 1575 |
template <class T> int h(T);
|
| 1576 |
}
|
| 1577 |
|
| 1578 |
+
int x = f<N::A>(N::A()); // OK, lookup of f finds nothing, f treated as template name
|
| 1579 |
+
int y = g<N::A>(N::A()); // OK, lookup of g finds a function, g treated as template name
|
| 1580 |
int z = h<N::A>(N::A()); // error: h< does not begin a template-id
|
| 1581 |
```
|
| 1582 |
|
| 1583 |
+
The rules have no effect on the syntactic interpretation of an
|
| 1584 |
+
expression. For example,
|
| 1585 |
|
| 1586 |
``` cpp
|
| 1587 |
typedef int f;
|
| 1588 |
namespace N {
|
| 1589 |
struct A {
|
|
|
|
| 1594 |
}
|
| 1595 |
};
|
| 1596 |
}
|
| 1597 |
```
|
| 1598 |
|
| 1599 |
+
Because the expression is not a function call, argument-dependent name
|
| 1600 |
+
lookup does not apply and the friend function `f` is not found.
|
|
|
|
| 1601 |
|
| 1602 |
— *end note*]
|
| 1603 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1604 |
For each argument type `T` in the function call, there is a set of zero
|
| 1605 |
+
or more *associated entities* to be considered. The set of entities is
|
| 1606 |
+
determined entirely by the types of the function arguments (and any
|
| 1607 |
+
template template arguments). Any *typedef-name*s and
|
| 1608 |
+
*using-declaration*s used to specify the types do not contribute to this
|
| 1609 |
+
set. The set of entities is determined in the following way:
|
|
|
|
|
|
|
| 1610 |
|
| 1611 |
+
- If `T` is a fundamental type, its associated set of entities is empty.
|
|
|
|
| 1612 |
- If `T` is a class type (including unions), its associated entities
|
| 1613 |
are: the class itself; the class of which it is a member, if any; and
|
| 1614 |
+
its direct and indirect base classes. Furthermore, if `T` is a class
|
| 1615 |
+
template specialization, its associated entities also include: the
|
| 1616 |
+
entities associated with the types of the template arguments provided
|
| 1617 |
+
for template type parameters; the templates used as template template
|
| 1618 |
+
arguments; and the classes of which any member templates used as
|
| 1619 |
+
template template arguments are members. \[*Note 2*: Non-type template
|
| 1620 |
+
arguments do not contribute to the set of associated
|
| 1621 |
+
entities. — *end note*]
|
| 1622 |
+
- If `T` is an enumeration type, its associated entities are `T` and, if
|
| 1623 |
+
it is a class member, the member’s class.
|
| 1624 |
+
- If `T` is a pointer to `U` or an array of `U`, its associated entities
|
| 1625 |
+
are those associated with `U`.
|
| 1626 |
+
- If `T` is a function type, its associated entities are those
|
| 1627 |
+
associated with the function parameter types and those associated with
|
| 1628 |
+
the return type.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1629 |
- If `T` is a pointer to a member function of a class `X`, its
|
| 1630 |
+
associated entities are those associated with the function parameter
|
| 1631 |
+
types and return type, together with those associated with `X`.
|
|
|
|
| 1632 |
- If `T` is a pointer to a data member of class `X`, its associated
|
| 1633 |
+
entities are those associated with the member type together with those
|
| 1634 |
+
associated with `X`.
|
| 1635 |
|
| 1636 |
+
In addition, if the argument is an overload set or the address of such a
|
| 1637 |
+
set, its associated entities are the union of those associated with each
|
| 1638 |
+
of the members of the set, i.e., the entities associated with its
|
| 1639 |
+
parameter types and return type. Additionally, if the aforementioned
|
| 1640 |
+
overload set is named with a *template-id*, its associated entities also
|
| 1641 |
+
include its template *template-argument*s and those associated with its
|
| 1642 |
+
type *template-argument*s.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1643 |
|
| 1644 |
+
The *associated namespaces* for a call are the innermost enclosing
|
| 1645 |
+
non-inline namespaces for its associated entities as well as every
|
| 1646 |
+
element of the inline namespace set [[namespace.def]] of those
|
| 1647 |
+
namespaces. Argument-dependent lookup finds all declarations of
|
| 1648 |
+
functions and function templates that
|
| 1649 |
|
| 1650 |
+
- are found by a search of any associated namespace, or
|
| 1651 |
+
- are declared as a friend [[class.friend]] of any class with a
|
| 1652 |
+
reachable definition in the set of associated entities, or
|
| 1653 |
+
- are exported, are attached to a named module `M` [[module.interface]],
|
| 1654 |
+
do not appear in the translation unit containing the point of the
|
| 1655 |
+
lookup, and have the same innermost enclosing non-inline namespace
|
| 1656 |
+
scope as a declaration of an associated entity attached to `M`
|
| 1657 |
+
[[basic.link]].
|
| 1658 |
|
| 1659 |
+
If the lookup is for a dependent name
|
| 1660 |
+
[[temp.dep]], [[temp.dep.candidate]], the above lookup is also performed
|
| 1661 |
+
from each point in the instantiation context [[module.context]] of the
|
| 1662 |
+
lookup, additionally ignoring any declaration that appears in another
|
| 1663 |
+
translation unit, is attached to the global module, and is either
|
| 1664 |
+
discarded [[module.global.frag]] or has internal linkage.
|
|
|
|
|
|
|
| 1665 |
|
| 1666 |
[*Example 2*:
|
| 1667 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1668 |
Translation unit #1
|
| 1669 |
|
| 1670 |
``` cpp
|
| 1671 |
export module M;
|
| 1672 |
namespace R {
|
|
|
|
| 1710 |
}
|
| 1711 |
```
|
| 1712 |
|
| 1713 |
— *end example*]
|
| 1714 |
|
| 1715 |
+
[*Note 3*: The associated namespace can include namespaces already
|
| 1716 |
+
considered by ordinary unqualified lookup. — *end note*]
|
| 1717 |
+
|
| 1718 |
+
[*Example 3*:
|
| 1719 |
+
|
| 1720 |
+
``` cpp
|
| 1721 |
+
namespace NS {
|
| 1722 |
+
class T { };
|
| 1723 |
+
void f(T);
|
| 1724 |
+
void g(T, int);
|
| 1725 |
+
}
|
| 1726 |
+
NS::T parm;
|
| 1727 |
+
void g(NS::T, float);
|
| 1728 |
+
int main() {
|
| 1729 |
+
f(parm); // OK, calls NS::f
|
| 1730 |
+
extern void g(NS::T, float);
|
| 1731 |
+
g(parm, 1); // OK, calls g(NS::T, float)
|
| 1732 |
+
}
|
| 1733 |
+
```
|
| 1734 |
+
|
| 1735 |
+
— *end example*]
|
| 1736 |
+
|
| 1737 |
### Qualified name lookup <a id="basic.lookup.qual">[[basic.lookup.qual]]</a>
|
| 1738 |
|
| 1739 |
+
#### General <a id="basic.lookup.qual.general">[[basic.lookup.qual.general]]</a>
|
| 1740 |
+
|
| 1741 |
+
Lookup of an *identifier* followed by a `::` scope resolution operator
|
| 1742 |
+
considers only namespaces, types, and templates whose specializations
|
| 1743 |
+
are types. If a name, *template-id*, or *decltype-specifier* is followed
|
| 1744 |
+
by a `::`, it shall designate a namespace, class, enumeration, or
|
| 1745 |
+
dependent type, and the `::` is never interpreted as a complete
|
| 1746 |
+
*nested-name-specifier*.
|
|
|
|
| 1747 |
|
| 1748 |
[*Example 1*:
|
| 1749 |
|
| 1750 |
``` cpp
|
| 1751 |
class A {
|
|
|
|
| 1755 |
int main() {
|
| 1756 |
int A;
|
| 1757 |
A::n = 42; // OK
|
| 1758 |
A b; // error: A does not name a type
|
| 1759 |
}
|
| 1760 |
+
template<int> struct B : A {};
|
| 1761 |
+
namespace N {
|
| 1762 |
+
template<int> void B();
|
| 1763 |
+
int f() {
|
| 1764 |
+
return B<0>::n; // error: N::B<0> is not a type
|
| 1765 |
+
}
|
| 1766 |
+
}
|
| 1767 |
```
|
| 1768 |
|
| 1769 |
— *end example*]
|
| 1770 |
|
| 1771 |
+
A member-qualified name is the (unique) component name
|
| 1772 |
+
[[expr.prim.id.unqual]], if any, of
|
| 1773 |
+
|
| 1774 |
+
- an *unqualified-id* or
|
| 1775 |
+
- a *nested-name-specifier* of the form *type-name* `::` or
|
| 1776 |
+
*namespace-name* `::`
|
| 1777 |
+
|
| 1778 |
+
in the *id-expression* of a class member access expression [[expr.ref]].
|
| 1779 |
+
A *qualified name* is
|
| 1780 |
+
|
| 1781 |
+
- a member-qualified name or
|
| 1782 |
+
- the terminal name of
|
| 1783 |
+
- a *qualified-id*,
|
| 1784 |
+
- a *using-declarator*,
|
| 1785 |
+
- a *typename-specifier*,
|
| 1786 |
+
- a *qualified-namespace-specifier*, or
|
| 1787 |
+
- a *nested-name-specifier*, *elaborated-type-specifier*, or
|
| 1788 |
+
*class-or-decltype* that has a *nested-name-specifier*
|
| 1789 |
+
[[expr.prim.id.qual]].
|
| 1790 |
+
|
| 1791 |
+
The *lookup context* of a member-qualified name is the type of its
|
| 1792 |
+
associated object expression (considered dependent if the object
|
| 1793 |
+
expression is type-dependent). The lookup context of any other qualified
|
| 1794 |
+
name is the type, template, or namespace nominated by the preceding
|
| 1795 |
+
*nested-name-specifier*.
|
| 1796 |
+
|
| 1797 |
+
[*Note 1*: When parsing a class member access, the name following the
|
| 1798 |
+
`->` or `.` is a qualified name even though it is not yet known of which
|
| 1799 |
+
kind. — *end note*]
|
| 1800 |
|
| 1801 |
[*Example 2*:
|
| 1802 |
|
| 1803 |
+
In
|
| 1804 |
+
|
| 1805 |
``` cpp
|
| 1806 |
+
N::C::m.Base::f()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1807 |
```
|
| 1808 |
|
| 1809 |
+
`Base` is a member-qualified name; the other qualified names are `C`,
|
| 1810 |
+
`m`, and `f`.
|
| 1811 |
+
|
| 1812 |
— *end example*]
|
| 1813 |
|
| 1814 |
+
*Qualified name lookup* in a class, namespace, or enumeration performs a
|
| 1815 |
+
search of the scope associated with it [[class.member.lookup]] except as
|
| 1816 |
+
specified below. Unless otherwise specified, a qualified name undergoes
|
| 1817 |
+
qualified name lookup in its lookup context from the point where it
|
| 1818 |
+
appears unless the lookup context either is dependent and is not the
|
| 1819 |
+
current instantiation [[temp.dep.type]] or is not a class or class
|
| 1820 |
+
template. If nothing is found by qualified lookup for a member-qualified
|
| 1821 |
+
name that is the terminal name [[expr.prim.id.unqual]] of a
|
| 1822 |
+
*nested-name-specifier* and is not dependent, it undergoes unqualified
|
| 1823 |
+
lookup.
|
| 1824 |
+
|
| 1825 |
+
[*Note 2*: During lookup for a template specialization, no names are
|
| 1826 |
+
dependent. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1827 |
|
| 1828 |
[*Example 3*:
|
| 1829 |
|
| 1830 |
+
``` cpp
|
| 1831 |
+
int f();
|
| 1832 |
+
struct A {
|
| 1833 |
+
int B, C;
|
| 1834 |
+
template<int> using D = void;
|
| 1835 |
+
using T = void;
|
| 1836 |
+
void f();
|
| 1837 |
+
};
|
| 1838 |
+
using B = A;
|
| 1839 |
+
template<int> using C = A;
|
| 1840 |
+
template<int> using D = A;
|
| 1841 |
+
template<int> using X = A;
|
| 1842 |
+
|
| 1843 |
+
template<class T>
|
| 1844 |
+
void g(T *p) { // as instantiated for g<A>:
|
| 1845 |
+
p->X<0>::f(); // error: A::X not found in ((p->X) < 0) > ::f()
|
| 1846 |
+
p->template X<0>::f(); // OK, ::X found in definition context
|
| 1847 |
+
p->B::f(); // OK, non-type A::B ignored
|
| 1848 |
+
p->template C<0>::f(); // error: A::C is not a template
|
| 1849 |
+
p->template D<0>::f(); // error: A::D<0> is not a class type
|
| 1850 |
+
p->T::f(); // error: A::T is not a class type
|
| 1851 |
+
}
|
| 1852 |
+
template void g(A*);
|
| 1853 |
+
```
|
| 1854 |
+
|
| 1855 |
+
— *end example*]
|
| 1856 |
+
|
| 1857 |
+
If a qualified name Q follows a `~`:
|
| 1858 |
+
|
| 1859 |
+
- If Q is a member-qualified name, it undergoes unqualified lookup as
|
| 1860 |
+
well as qualified lookup.
|
| 1861 |
+
- Otherwise, its *nested-name-specifier* N shall nominate a type. If N
|
| 1862 |
+
has another *nested-name-specifier* S, Q is looked up as if its lookup
|
| 1863 |
+
context were that nominated by S.
|
| 1864 |
+
- Otherwise, if the terminal name of N is a member-qualified name M, Q
|
| 1865 |
+
is looked up as if `\~`Q appeared in place of M (as above).
|
| 1866 |
+
- Otherwise, Q undergoes unqualified lookup.
|
| 1867 |
+
- Each lookup for Q considers only types (if Q is not followed by a `<`)
|
| 1868 |
+
and templates whose specializations are types. If it finds nothing or
|
| 1869 |
+
is ambiguous, it is discarded.
|
| 1870 |
+
- The *type-name* that is or contains Q shall refer to its (original)
|
| 1871 |
+
lookup context (ignoring cv-qualification) under the interpretation
|
| 1872 |
+
established by at least one (successful) lookup performed.
|
| 1873 |
+
|
| 1874 |
+
[*Example 4*:
|
| 1875 |
+
|
| 1876 |
``` cpp
|
| 1877 |
struct C {
|
| 1878 |
typedef int I;
|
| 1879 |
};
|
| 1880 |
typedef int I1, I2;
|
| 1881 |
extern int* p;
|
| 1882 |
extern int* q;
|
| 1883 |
+
void f() {
|
| 1884 |
p->C::I::~I(); // I is looked up in the scope of C
|
| 1885 |
+
q->I1::~I2(); // I2 is found by unqualified lookup
|
| 1886 |
+
}
|
| 1887 |
struct A {
|
| 1888 |
~A();
|
| 1889 |
};
|
| 1890 |
typedef A AB;
|
| 1891 |
int main() {
|
|
|
|
| 1894 |
}
|
| 1895 |
```
|
| 1896 |
|
| 1897 |
— *end example*]
|
| 1898 |
|
|
|
|
|
|
|
|
|
|
| 1899 |
#### Class members <a id="class.qual">[[class.qual]]</a>
|
| 1900 |
|
| 1901 |
+
In a lookup for a qualified name N whose lookup context is a class C in
|
| 1902 |
+
which function names are not ignored,[^4]
|
|
|
|
|
|
|
|
|
|
| 1903 |
|
| 1904 |
+
- if the search finds the injected-class-name of `C` [[class.pre]], or
|
| 1905 |
+
- if N is dependent and is the terminal name of a *using-declarator*
|
| 1906 |
+
[[namespace.udecl]] that names a constructor,
|
| 1907 |
|
| 1908 |
+
N is instead considered to name the constructor of class `C`. Such a
|
| 1909 |
+
constructor name shall be used only in the *declarator-id* of a (friend)
|
| 1910 |
+
declaration of a constructor or in a *using-declaration*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1911 |
|
| 1912 |
[*Example 1*:
|
| 1913 |
|
| 1914 |
``` cpp
|
| 1915 |
struct A { A(); };
|
|
|
|
| 1923 |
struct A::A a2; // object of type A
|
| 1924 |
```
|
| 1925 |
|
| 1926 |
— *end example*]
|
| 1927 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1928 |
#### Namespace members <a id="namespace.qual">[[namespace.qual]]</a>
|
| 1929 |
|
| 1930 |
+
Qualified name lookup in a namespace N additionally searches every
|
| 1931 |
+
element of the inline namespace set of N [[namespace.def]]. If nothing
|
| 1932 |
+
is found, the results of the lookup are the results of qualified name
|
| 1933 |
+
lookup in each namespace nominated by a *using-directive* that precedes
|
| 1934 |
+
the point of the lookup and inhabits N or an element of N’s inline
|
| 1935 |
+
namespace set.
|
| 1936 |
|
| 1937 |
+
[*Note 1*: If a *using-directive* refers to a namespace that has
|
| 1938 |
+
already been considered, it does not affect the result. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1939 |
|
| 1940 |
[*Example 1*:
|
| 1941 |
|
| 1942 |
``` cpp
|
| 1943 |
int x;
|
|
|
|
| 1991 |
}
|
| 1992 |
```
|
| 1993 |
|
| 1994 |
— *end example*]
|
| 1995 |
|
| 1996 |
+
[*Note 2*:
|
| 1997 |
|
| 1998 |
The same declaration found more than once is not an ambiguity (because
|
| 1999 |
it is still a unique declaration).
|
| 2000 |
|
| 2001 |
[*Example 2*:
|
|
|
|
| 2018 |
using namespace C;
|
| 2019 |
}
|
| 2020 |
|
| 2021 |
void f()
|
| 2022 |
{
|
| 2023 |
+
BC::a++; // OK, S is { `A::a`, `A::a` }
|
| 2024 |
}
|
| 2025 |
|
| 2026 |
namespace D {
|
| 2027 |
using A::a;
|
| 2028 |
}
|
|
|
|
| 2032 |
using namespace D;
|
| 2033 |
}
|
| 2034 |
|
| 2035 |
void g()
|
| 2036 |
{
|
| 2037 |
+
BD::a++; // OK, S is { `A::a`, `A::a` }
|
| 2038 |
}
|
| 2039 |
```
|
| 2040 |
|
| 2041 |
— *end example*]
|
| 2042 |
|
|
|
|
| 2061 |
using namespace A;
|
| 2062 |
}
|
| 2063 |
|
| 2064 |
void f()
|
| 2065 |
{
|
| 2066 |
+
A::a++; // OK, a declared directly in A, S is { `A::a` }
|
| 2067 |
+
B::a++; // OK, both A and B searched (once), S is { `A::a` }
|
| 2068 |
+
A::b++; // OK, both A and B searched (once), S is { `B::b` }
|
| 2069 |
+
B::b++; // OK, b declared directly in B, S is { `B::b` }
|
| 2070 |
}
|
| 2071 |
```
|
| 2072 |
|
| 2073 |
— *end example*]
|
| 2074 |
|
| 2075 |
+
[*Note 3*: Class and enumeration declarations are not discarded because
|
| 2076 |
+
of other declarations found in other searches. — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2077 |
|
| 2078 |
[*Example 4*:
|
| 2079 |
|
| 2080 |
``` cpp
|
| 2081 |
namespace A {
|
|
|
|
| 2096 |
}
|
| 2097 |
```
|
| 2098 |
|
| 2099 |
— *end example*]
|
| 2100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2101 |
### Elaborated type specifiers <a id="basic.lookup.elab">[[basic.lookup.elab]]</a>
|
| 2102 |
|
| 2103 |
+
If the *class-key* or `enum` keyword in an *elaborated-type-specifier*
|
| 2104 |
+
is followed by an *identifier* that is not followed by `::`, lookup for
|
| 2105 |
+
the *identifier* is type-only [[basic.lookup.general]].
|
| 2106 |
+
|
| 2107 |
+
[*Note 1*: In general, the recognition of an
|
| 2108 |
+
*elaborated-type-specifier* depends on the following tokens. If the
|
| 2109 |
+
*identifier* is followed by `::`, see
|
| 2110 |
+
[[basic.lookup.qual]]. — *end note*]
|
| 2111 |
+
|
| 2112 |
+
If the terminal name of the *elaborated-type-specifier* is a qualified
|
| 2113 |
+
name, lookup for it is type-only. If the name lookup does not find a
|
| 2114 |
+
previously declared *type-name*, the *elaborated-type-specifier* is
|
| 2115 |
+
ill-formed.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2116 |
|
| 2117 |
[*Example 1*:
|
| 2118 |
|
| 2119 |
``` cpp
|
| 2120 |
struct Node {
|
| 2121 |
+
struct Node* Next; // OK, refers to injected-class-name Node
|
| 2122 |
+
struct Data* Data; // OK, declares type Data at global scope and member Data
|
| 2123 |
};
|
| 2124 |
|
| 2125 |
struct Data {
|
| 2126 |
+
struct Node* Node; // OK, refers to Node at global scope
|
| 2127 |
friend struct ::Glob; // error: Glob is not declared, cannot introduce a qualified type[dcl.type.elab]
|
| 2128 |
+
friend struct Glob; // OK, refers to (as yet) undeclared Glob at global scope.
|
| 2129 |
...
|
| 2130 |
};
|
| 2131 |
|
| 2132 |
struct Base {
|
| 2133 |
+
struct Data; // OK, declares nested Data
|
| 2134 |
+
struct ::Data* thatData; // OK, refers to ::Data
|
| 2135 |
+
struct Base::Data* thisData; // OK, refers to nested Data
|
| 2136 |
+
friend class ::Data; // OK, global Data is a friend
|
| 2137 |
+
friend class Data; // OK, nested Data is a friend
|
| 2138 |
struct Data { ... }; // Defines nested Data
|
| 2139 |
};
|
| 2140 |
|
| 2141 |
+
struct Data; // OK, redeclares Data at global scope
|
| 2142 |
struct ::Data; // error: cannot introduce a qualified type[dcl.type.elab]
|
| 2143 |
struct Base::Data; // error: cannot introduce a qualified type[dcl.type.elab]
|
| 2144 |
struct Base::Datum; // error: Datum undefined
|
| 2145 |
+
struct Base::Data* pBase; // OK, refers to nested Data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2146 |
```
|
| 2147 |
|
| 2148 |
— *end example*]
|
| 2149 |
|
| 2150 |
### Using-directives and namespace aliases <a id="basic.lookup.udir">[[basic.lookup.udir]]</a>
|
|
|
|
| 2163 |
translation-unit:
|
| 2164 |
declaration-seqₒₚₜ
|
| 2165 |
global-module-fragmentₒₚₜ module-declaration declaration-seqₒₚₜ private-module-fragmentₒₚₜ
|
| 2166 |
```
|
| 2167 |
|
| 2168 |
+
A name is said to have *linkage* when it can denote the same object,
|
| 2169 |
reference, function, type, template, namespace or value as a name
|
| 2170 |
introduced by a declaration in another scope:
|
| 2171 |
|
| 2172 |
- When a name has *external linkage*, the entity it denotes can be
|
| 2173 |
referred to by names from scopes of other translation units or from
|
|
|
|
| 2179 |
- When a name has *internal linkage*, the entity it denotes can be
|
| 2180 |
referred to by names from other scopes in the same translation unit.
|
| 2181 |
- When a name has *no linkage*, the entity it denotes cannot be referred
|
| 2182 |
to by names from other scopes.
|
| 2183 |
|
| 2184 |
+
The name of an entity that belongs to a namespace scope
|
| 2185 |
+
[[basic.scope.namespace]] has internal linkage if it is the name of
|
| 2186 |
|
| 2187 |
- a variable, variable template, function, or function template that is
|
| 2188 |
explicitly declared `static`; or
|
| 2189 |
- a non-template variable of non-volatile const-qualified type, unless
|
| 2190 |
+
- it is declared in the purview of a module interface unit (outside
|
| 2191 |
+
the *private-module-fragment*, if any) or module partition, or
|
| 2192 |
- it is explicitly declared `extern`, or
|
| 2193 |
+
- it is inline, or
|
| 2194 |
- it was previously declared and the prior declaration did not have
|
| 2195 |
internal linkage; or
|
| 2196 |
- a data member of an anonymous union.
|
| 2197 |
|
| 2198 |
[*Note 1*: An instantiated variable template that has const-qualified
|
| 2199 |
type can have external or module linkage, even if not declared
|
| 2200 |
`extern`. — *end note*]
|
| 2201 |
|
| 2202 |
An unnamed namespace or a namespace declared directly or indirectly
|
| 2203 |
within an unnamed namespace has internal linkage. All other namespaces
|
| 2204 |
+
have external linkage. The name of an entity that belongs to a namespace
|
| 2205 |
+
scope that has not been given internal linkage above and that is the
|
| 2206 |
+
name of
|
| 2207 |
|
| 2208 |
- a variable; or
|
| 2209 |
- a function; or
|
| 2210 |
- a named class [[class.pre]], or an unnamed class defined in a typedef
|
| 2211 |
declaration in which the class has the typedef name for linkage
|
|
|
|
| 2224 |
- otherwise, if the declaration of the name is attached to a named
|
| 2225 |
module [[module.unit]] and is not exported [[module.interface]], the
|
| 2226 |
name has module linkage;
|
| 2227 |
- otherwise, the name has external linkage.
|
| 2228 |
|
| 2229 |
+
In addition, a member function, a static data member, a named class or
|
| 2230 |
+
enumeration that inhabits a class scope, or an unnamed class or
|
| 2231 |
+
enumeration defined in a typedef declaration that inhabits a class scope
|
| 2232 |
+
such that the class or enumeration has the typedef name for linkage
|
| 2233 |
+
purposes [[dcl.typedef]], has the same linkage, if any, as the name of
|
| 2234 |
+
the class of which it is a member.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2235 |
|
| 2236 |
[*Example 1*:
|
| 2237 |
|
| 2238 |
``` cpp
|
| 2239 |
static void f();
|
| 2240 |
extern "C" void h();
|
| 2241 |
static int i = 0; // #1
|
| 2242 |
+
void q() {
|
| 2243 |
extern void f(); // internal linkage
|
| 2244 |
+
extern void g(); // ::g, external linkage
|
| 2245 |
extern void h(); // C language linkage
|
| 2246 |
int i; // #2: i has no linkage
|
| 2247 |
{
|
| 2248 |
extern void f(); // internal linkage
|
| 2249 |
+
extern int i; // #3: internal linkage
|
| 2250 |
}
|
| 2251 |
}
|
| 2252 |
```
|
| 2253 |
|
| 2254 |
+
Even though the declaration at line \#2 hides the declaration at line
|
| 2255 |
+
\#1, the declaration at line \#3 still redeclares \#1 and receives
|
| 2256 |
+
internal linkage.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2257 |
|
| 2258 |
— *end example*]
|
| 2259 |
|
| 2260 |
Names not covered by these rules have no linkage. Moreover, except as
|
| 2261 |
noted, a name declared at block scope [[basic.scope.block]] has no
|
| 2262 |
linkage.
|
| 2263 |
|
| 2264 |
+
Two declarations of entities declare the same entity if, considering
|
| 2265 |
+
declarations of unnamed types to introduce their names for linkage
|
| 2266 |
+
purposes, if any [[dcl.typedef]], [[dcl.enum]], they correspond
|
| 2267 |
+
[[basic.scope.scope]], have the same target scope that is not a function
|
| 2268 |
+
or template parameter scope, and either
|
| 2269 |
|
| 2270 |
+
- they appear in the same translation unit, or
|
| 2271 |
+
- they both declare names with module linkage and are attached to the
|
| 2272 |
+
same module, or
|
| 2273 |
+
- they both declare names with external linkage.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2274 |
|
| 2275 |
+
[*Note 2*: There are other circumstances in which declarations declare
|
| 2276 |
+
the same entity
|
| 2277 |
+
[[dcl.link]], [[temp.type]], [[temp.spec.partial]]. — *end note*]
|
| 2278 |
|
| 2279 |
+
If a declaration H that declares a name with internal linkage precedes a
|
| 2280 |
+
declaration D in another translation unit U and would declare the same
|
| 2281 |
+
entity as D if it appeared in U, the program is ill-formed.
|
|
|
|
|
|
|
| 2282 |
|
| 2283 |
+
[*Note 3*: Such an H can appear only in a header unit. — *end note*]
|
|
|
|
| 2284 |
|
| 2285 |
+
If two declarations of an entity are attached to different modules, the
|
| 2286 |
+
program is ill-formed; no diagnostic is required if neither is reachable
|
| 2287 |
+
from the other.
|
| 2288 |
+
|
| 2289 |
+
[*Example 2*:
|
| 2290 |
|
| 2291 |
\`"decls.h"\`
|
| 2292 |
|
| 2293 |
``` cpp
|
| 2294 |
int f(); // #1, attached to the global module
|
|
|
|
| 2299 |
|
| 2300 |
``` cpp
|
| 2301 |
module;
|
| 2302 |
#include "decls.h"
|
| 2303 |
export module M;
|
| 2304 |
+
export using ::f; // OK, does not declare an entity, exports #1
|
| 2305 |
int g(); // error: matches #2, but attached to M
|
| 2306 |
export int h(); // #3
|
| 2307 |
export int k(); // #4
|
| 2308 |
```
|
| 2309 |
|
|
|
|
| 2319 |
|
| 2320 |
As a consequence of these rules, all declarations of an entity are
|
| 2321 |
attached to the same module; the entity is said to be *attached* to that
|
| 2322 |
module.
|
| 2323 |
|
| 2324 |
+
For any two declarations of an entity E:
|
| 2325 |
+
|
| 2326 |
+
- If one declares E to be a variable or function, the other shall
|
| 2327 |
+
declare E as one of the same type.
|
| 2328 |
+
- If one declares E to be an enumerator, the other shall do so.
|
| 2329 |
+
- If one declares E to be a namespace, the other shall do so.
|
| 2330 |
+
- If one declares E to be a type, the other shall declare E to be a type
|
| 2331 |
+
of the same kind [[dcl.type.elab]].
|
| 2332 |
+
- If one declares E to be a class template, the other shall do so with
|
| 2333 |
+
the same kind and an equivalent *template-head* [[temp.over.link]].
|
| 2334 |
+
\[*Note 4*: The declarations can supply different default template
|
| 2335 |
+
arguments. — *end note*]
|
| 2336 |
+
- If one declares E to be a function template or a (partial
|
| 2337 |
+
specialization of a) variable template, the other shall declare E to
|
| 2338 |
+
be one with an equivalent *template-head* and type.
|
| 2339 |
+
- If one declares E to be an alias template, the other shall declare E
|
| 2340 |
+
to be one with an equivalent *template-head* and *defining-type-id*.
|
| 2341 |
+
- If one declares E to be a concept, the other shall do so.
|
| 2342 |
+
|
| 2343 |
+
Types are compared after all adjustments of types (during which typedefs
|
| 2344 |
+
[[dcl.typedef]] are replaced by their definitions); declarations for an
|
| 2345 |
+
array object can specify array types that differ by the presence or
|
| 2346 |
+
absence of a major array bound [[dcl.array]]. No diagnostic is required
|
| 2347 |
+
if neither declaration is reachable from the other.
|
| 2348 |
+
|
| 2349 |
+
[*Example 3*:
|
| 2350 |
+
|
| 2351 |
+
``` cpp
|
| 2352 |
+
int f(int x, int x); // error: different entities for x
|
| 2353 |
+
void g(); // #1
|
| 2354 |
+
void g(int); // OK, different entity from #1
|
| 2355 |
+
int g(); // error: same entity as #1 with different type
|
| 2356 |
+
void h(); // #2
|
| 2357 |
+
namespace h {} // error: same entity as #2, but not a function
|
| 2358 |
+
```
|
| 2359 |
+
|
| 2360 |
+
— *end example*]
|
| 2361 |
+
|
| 2362 |
+
[*Note 5*: Linkage to non-C++ declarations can be achieved using a
|
| 2363 |
*linkage-specification* [[dcl.link]]. — *end note*]
|
| 2364 |
|
| 2365 |
A declaration D *names* an entity E if
|
| 2366 |
|
| 2367 |
- D contains a *lambda-expression* whose closure type is E,
|
| 2368 |
- E is not a function or function template and D contains an
|
| 2369 |
*id-expression*, *type-specifier*, *nested-name-specifier*,
|
| 2370 |
*template-name*, or *concept-name* denoting E, or
|
| 2371 |
- E is a function or function template and D contains an expression that
|
| 2372 |
names E [[basic.def.odr]] or an *id-expression* that refers to a set
|
| 2373 |
+
of overloads that contains E. \[*Note 6*: Non-dependent names in an
|
| 2374 |
instantiated declaration do not refer to a set of overloads
|
| 2375 |
+
[[temp.res]]. — *end note*]
|
| 2376 |
|
| 2377 |
A declaration is an *exposure* if it either names a TU-local entity
|
| 2378 |
(defined below), ignoring
|
| 2379 |
|
| 2380 |
- the *function-body* for a non-inline function or function template
|
|
|
|
| 2384 |
- the *initializer* for a variable or variable template (but not the
|
| 2385 |
variable’s type),
|
| 2386 |
- friend declarations in a class definition, and
|
| 2387 |
- any reference to a non-volatile const object or reference with
|
| 2388 |
internal or no linkage initialized with a constant expression that is
|
| 2389 |
+
not an odr-use [[term.odr.use]],
|
| 2390 |
|
| 2391 |
or defines a constexpr variable initialized to a TU-local value (defined
|
| 2392 |
below).
|
| 2393 |
|
| 2394 |
+
[*Note 7*: An inline function template can be an exposure even though
|
| 2395 |
+
certain explicit specializations of it would be usable in other
|
| 2396 |
+
translation units. — *end note*]
|
| 2397 |
|
| 2398 |
An entity is *TU-local* if it is
|
| 2399 |
|
| 2400 |
- a type, function, variable, or template that
|
| 2401 |
- has a name with internal linkage, or
|
|
|
|
| 2406 |
*defining-type-specifier* that is used to declare only TU-local
|
| 2407 |
entities,
|
| 2408 |
- a specialization of a TU-local template,
|
| 2409 |
- a specialization of a template with any TU-local template argument, or
|
| 2410 |
- a specialization of a template whose (possibly instantiated)
|
| 2411 |
+
declaration is an exposure. \[*Note 8*: A specialization can be
|
| 2412 |
+
produced by implicit or explicit instantiation. — *end note*]
|
| 2413 |
|
| 2414 |
A value or object is *TU-local* if either
|
| 2415 |
|
| 2416 |
- it is, or is a pointer to, a TU-local function or the object
|
| 2417 |
+
associated with a TU-local variable, or
|
| 2418 |
- it is an object of class or array type and any of its subobjects or
|
| 2419 |
any of the objects or functions to which its non-static data members
|
| 2420 |
of reference type refer is TU-local and is usable in constant
|
| 2421 |
expressions.
|
| 2422 |
|
|
|
|
| 2462 |
void adl(A);
|
| 2463 |
static void adl(int);
|
| 2464 |
}
|
| 2465 |
void adl(double);
|
| 2466 |
|
| 2467 |
+
inline void h(auto x) { adl(x); } // OK, but certain specializations are exposures
|
| 2468 |
```
|
| 2469 |
|
| 2470 |
Translation unit #2
|
| 2471 |
|
| 2472 |
``` cpp
|
|
|
|
| 2487 |
## Memory and objects <a id="basic.memobj">[[basic.memobj]]</a>
|
| 2488 |
|
| 2489 |
### Memory model <a id="intro.memory">[[intro.memory]]</a>
|
| 2490 |
|
| 2491 |
The fundamental storage unit in the C++ memory model is the *byte*. A
|
| 2492 |
+
byte is at least large enough to contain the ordinary literal encoding
|
| 2493 |
+
of any element of the basic literal character set [[lex.charset]] and
|
| 2494 |
+
the eight-bit code units of the Unicode[^5]
|
| 2495 |
+
|
| 2496 |
+
UTF-8 encoding form and is composed of a contiguous sequence of
|
| 2497 |
+
bits,[^6]
|
| 2498 |
+
|
| 2499 |
+
the number of which is *implementation-defined*. The least significant
|
| 2500 |
+
bit is called the *low-order bit*; the most significant bit is called
|
| 2501 |
+
the *high-order bit*. The memory available to a C++ program consists of
|
| 2502 |
+
one or more sequences of contiguous bytes. Every byte has a unique
|
| 2503 |
+
address.
|
| 2504 |
|
| 2505 |
[*Note 1*: The representation of types is described in
|
| 2506 |
+
[[basic.types.general]]. — *end note*]
|
| 2507 |
|
| 2508 |
+
A *memory location* is either an object of scalar type that is not a
|
| 2509 |
+
bit-field or a maximal sequence of adjacent bit-fields all having
|
| 2510 |
+
nonzero width.
|
| 2511 |
|
| 2512 |
[*Note 2*: Various features of the language, such as references and
|
| 2513 |
virtual functions, might involve additional memory locations that are
|
| 2514 |
not accessible to programs but are managed by the
|
| 2515 |
implementation. — *end note*]
|
|
|
|
| 2537 |
int b:5,
|
| 2538 |
c:11,
|
| 2539 |
:0,
|
| 2540 |
d:8;
|
| 2541 |
struct {int ee:8;} e;
|
| 2542 |
+
};
|
| 2543 |
```
|
| 2544 |
|
| 2545 |
contains four separate memory locations: The member `a` and bit-fields
|
| 2546 |
`d` and `e.ee` are each separate memory locations, and can be modified
|
| 2547 |
concurrently without interfering with each other. The bit-fields `b` and
|
|
|
|
| 2556 |
The constructs in a C++ program create, destroy, refer to, access, and
|
| 2557 |
manipulate objects. An *object* is created by a definition
|
| 2558 |
[[basic.def]], by a *new-expression* [[expr.new]], by an operation that
|
| 2559 |
implicitly creates objects (see below), when implicitly changing the
|
| 2560 |
active member of a union [[class.union]], or when a temporary object is
|
| 2561 |
+
created [[conv.rval]], [[class.temporary]]. An object occupies a region
|
| 2562 |
+
of storage in its period of construction [[class.cdtor]], throughout its
|
| 2563 |
+
lifetime [[basic.life]], and in its period of destruction
|
| 2564 |
[[class.cdtor]].
|
| 2565 |
|
| 2566 |
[*Note 1*: A function is not an object, regardless of whether or not it
|
| 2567 |
occupies storage in the way that objects do. — *end note*]
|
| 2568 |
|
| 2569 |
The properties of an object are determined when the object is created.
|
| 2570 |
An object can have a name [[basic.pre]]. An object has a storage
|
| 2571 |
duration [[basic.stc]] which influences its lifetime [[basic.life]]. An
|
| 2572 |
+
object has a type [[basic.types]].
|
| 2573 |
+
|
| 2574 |
+
[*Note 2*: Some objects are polymorphic [[class.virtual]]; the
|
| 2575 |
+
implementation generates information associated with each such object
|
| 2576 |
+
that makes it possible to determine that object’s type during program
|
| 2577 |
+
execution. — *end note*]
|
| 2578 |
|
| 2579 |
Objects can contain other objects, called *subobjects*. A subobject can
|
| 2580 |
be a *member subobject* [[class.mem]], a *base class subobject*
|
| 2581 |
[[class.derived]], or an array element. An object that is not a
|
| 2582 |
subobject of any other object is called a *complete object*. If an
|
|
|
|
| 2594 |
“array of N `std::byte`” [[cstddef.syn]], that array *provides storage*
|
| 2595 |
for the created object if:
|
| 2596 |
|
| 2597 |
- the lifetime of *e* has begun and not ended, and
|
| 2598 |
- the storage for the new object fits entirely within *e*, and
|
| 2599 |
+
- there is no array object that satisfies these constraints nested
|
| 2600 |
+
within *e*.
|
| 2601 |
|
| 2602 |
+
[*Note 3*: If that portion of the array previously provided storage for
|
| 2603 |
another object, the lifetime of that object ends because its storage was
|
| 2604 |
reused [[basic.life]]. — *end note*]
|
| 2605 |
|
| 2606 |
[*Example 1*:
|
| 2607 |
|
|
|
|
| 2642 |
- If `x` is a complete object, then the complete object of `x` is
|
| 2643 |
itself.
|
| 2644 |
- Otherwise, the complete object of `x` is the complete object of the
|
| 2645 |
(unique) object that contains `x`.
|
| 2646 |
|
| 2647 |
+
If a complete object, a member subobject, or an array element is of
|
| 2648 |
+
class type, its type is considered the *most derived class*, to
|
| 2649 |
distinguish it from the class type of any base class subobject; an
|
| 2650 |
object of a most derived class type or of a non-class type is called a
|
| 2651 |
*most derived object*.
|
| 2652 |
|
| 2653 |
A *potentially-overlapping subobject* is either:
|
|
|
|
| 2660 |
|
| 2661 |
- is not a potentially-overlapping subobject, or
|
| 2662 |
- is not of class type, or
|
| 2663 |
- is of a class type with virtual member functions or virtual base
|
| 2664 |
classes, or
|
| 2665 |
+
- has subobjects of nonzero size or unnamed bit-fields of nonzero
|
| 2666 |
+
length.
|
| 2667 |
|
| 2668 |
Otherwise, if the object is a base class subobject of a standard-layout
|
| 2669 |
class type with no non-static data members, it has zero size. Otherwise,
|
| 2670 |
the circumstances under which the object has zero size are
|
| 2671 |
*implementation-defined*. Unless it is a bit-field [[class.bit]], an
|
| 2672 |
object with nonzero size shall occupy one or more bytes of storage,
|
| 2673 |
including every byte that is occupied in full or in part by any of its
|
| 2674 |
subobjects. An object of trivially copyable or standard-layout type
|
| 2675 |
+
[[basic.types.general]] shall occupy contiguous bytes of storage.
|
| 2676 |
|
| 2677 |
Unless an object is a bit-field or a subobject of zero size, the address
|
| 2678 |
of that object is the address of the first byte it occupies. Two objects
|
| 2679 |
with overlapping lifetimes that are not bit-fields may have the same
|
| 2680 |
address if one is nested within the other, or if at least one is a
|
| 2681 |
subobject of zero size and they are of different types; otherwise, they
|
| 2682 |
+
have distinct addresses and occupy disjoint bytes of storage.[^7]
|
| 2683 |
|
| 2684 |
[*Example 2*:
|
| 2685 |
|
| 2686 |
``` cpp
|
| 2687 |
static const char test1 = 'x';
|
|
|
|
| 2697 |
|
| 2698 |
Some operations are described as *implicitly creating objects* within a
|
| 2699 |
specified region of storage. For each operation that is specified as
|
| 2700 |
implicitly creating objects, that operation implicitly creates and
|
| 2701 |
starts the lifetime of zero or more objects of implicit-lifetime types
|
| 2702 |
+
[[basic.types.general]] in its specified region of storage if doing so
|
| 2703 |
+
would result in the program having defined behavior. If no such set of
|
| 2704 |
+
objects would give the program defined behavior, the behavior of the
|
| 2705 |
+
program is undefined. If multiple such sets of objects would give the
|
| 2706 |
+
program defined behavior, it is unspecified which such set of objects is
|
| 2707 |
created.
|
| 2708 |
|
| 2709 |
+
[*Note 4*: Such operations do not start the lifetimes of subobjects of
|
| 2710 |
such objects that are not themselves of implicit-lifetime
|
| 2711 |
types. — *end note*]
|
| 2712 |
|
| 2713 |
Further, after implicitly creating objects within a specified region of
|
| 2714 |
storage, some operations are described as producing a pointer to a
|
|
|
|
| 2739 |
}
|
| 2740 |
```
|
| 2741 |
|
| 2742 |
— *end example*]
|
| 2743 |
|
| 2744 |
+
An operation that begins the lifetime of an array of `unsigned char` or
|
| 2745 |
+
`std::byte` implicitly creates objects within the region of storage
|
| 2746 |
+
occupied by the array.
|
| 2747 |
|
| 2748 |
+
[*Note 5*: The array object provides storage for these
|
| 2749 |
objects. — *end note*]
|
| 2750 |
|
| 2751 |
Any implicit or explicit invocation of a function named `operator new`
|
| 2752 |
or `operator new[]` implicitly creates objects in the returned region of
|
| 2753 |
storage and returns a pointer to a suitable created object.
|
| 2754 |
|
| 2755 |
+
[*Note 6*: Some functions in the C++ standard library implicitly create
|
| 2756 |
+
objects
|
| 2757 |
+
[[obj.lifetime]], [[allocator.traits.members]], [[c.malloc]], [[cstring.syn]], [[bit.cast]]. — *end note*]
|
| 2758 |
|
| 2759 |
### Lifetime <a id="basic.life">[[basic.life]]</a>
|
| 2760 |
|
| 2761 |
The *lifetime* of an object or reference is a runtime property of the
|
| 2762 |
object or reference. A variable is said to have *vacuous initialization*
|
| 2763 |
if it is default-initialized and, if it is of class type or a (possibly
|
| 2764 |
+
multidimensional) array thereof, that class type has a trivial default
|
| 2765 |
constructor. The lifetime of an object of type `T` begins when:
|
| 2766 |
|
| 2767 |
- storage with the proper alignment and size for type `T` is obtained,
|
| 2768 |
and
|
| 2769 |
- its initialization (if any) is complete (including vacuous
|
| 2770 |
initialization) [[dcl.init]],
|
| 2771 |
|
| 2772 |
except that if the object is a union member or subobject thereof, its
|
| 2773 |
lifetime only begins if that union member is the initialized member in
|
| 2774 |
+
the union [[dcl.init.aggr]], [[class.base.init]], or as described in
|
| 2775 |
+
[[class.union]], [[class.copy.ctor]], and [[class.copy.assign]], and
|
| 2776 |
+
except as described in [[allocator.members]]. The lifetime of an object
|
| 2777 |
+
*o* of type `T` ends when:
|
| 2778 |
|
| 2779 |
- if `T` is a non-class type, the object is destroyed, or
|
| 2780 |
- if `T` is a class type, the destructor call starts, or
|
| 2781 |
- the storage which the object occupies is released, or is reused by an
|
| 2782 |
object that is not nested within *o* [[intro.object]].
|
|
|
|
| 2791 |
The properties ascribed to objects and references throughout this
|
| 2792 |
document apply for a given object or reference only during its lifetime.
|
| 2793 |
|
| 2794 |
[*Note 2*: In particular, before the lifetime of an object starts and
|
| 2795 |
after its lifetime ends there are significant restrictions on the use of
|
| 2796 |
+
the object, as described below, in [[class.base.init]], and in
|
| 2797 |
[[class.cdtor]]. Also, the behavior of an object under construction and
|
| 2798 |
+
destruction can differ from the behavior of an object whose lifetime has
|
| 2799 |
+
started and not ended. [[class.base.init]] and [[class.cdtor]] describe
|
| 2800 |
+
the behavior of an object during its periods of construction and
|
| 2801 |
+
destruction. — *end note*]
|
| 2802 |
+
|
| 2803 |
+
A program may end the lifetime of an object of class type without
|
| 2804 |
+
invoking the destructor, by reusing or releasing the storage as
|
| 2805 |
+
described above.
|
| 2806 |
+
|
| 2807 |
+
[*Note 3*: A *delete-expression* [[expr.delete]] invokes the destructor
|
| 2808 |
+
prior to releasing the storage. — *end note*]
|
| 2809 |
+
|
| 2810 |
+
In this case, the destructor is not implicitly invoked.
|
| 2811 |
+
|
| 2812 |
+
[*Note 4*: The correct behavior of a program often depends on the
|
| 2813 |
+
destructor being invoked for each object of class type. — *end note*]
|
| 2814 |
|
| 2815 |
Before the lifetime of an object has started but after the storage which
|
| 2816 |
+
the object will occupy has been allocated[^8]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2817 |
|
| 2818 |
+
or, after the lifetime of an object has ended and before the storage
|
| 2819 |
+
which the object occupied is reused or released, any pointer that
|
| 2820 |
+
represents the address of the storage location where the object will be
|
| 2821 |
+
or was located may be used but only in limited ways. For an object under
|
| 2822 |
+
construction or destruction, see [[class.cdtor]]. Otherwise, such a
|
| 2823 |
+
pointer refers to allocated storage [[basic.stc.dynamic.allocation]],
|
| 2824 |
+
and using the pointer as if the pointer were of type `void*` is
|
| 2825 |
+
well-defined. Indirection through such a pointer is permitted but the
|
| 2826 |
+
resulting lvalue may only be used in limited ways, as described below.
|
| 2827 |
+
The program has undefined behavior if:
|
| 2828 |
+
|
| 2829 |
+
- the pointer is used as the operand of a *delete-expression*,
|
| 2830 |
- the pointer is used to access a non-static data member or call a
|
| 2831 |
non-static member function of the object, or
|
| 2832 |
- the pointer is implicitly converted [[conv.ptr]] to a pointer to a
|
| 2833 |
virtual base class, or
|
| 2834 |
- the pointer is used as the operand of a `static_cast`
|
|
|
|
| 2860 |
|
| 2861 |
void g() {
|
| 2862 |
void* p = std::malloc(sizeof(D1) + sizeof(D2));
|
| 2863 |
B* pb = new (p) D1;
|
| 2864 |
pb->mutate();
|
| 2865 |
+
*pb; // OK, pb points to valid memory
|
| 2866 |
+
void* q = pb; // OK, pb points to valid memory
|
| 2867 |
pb->f(); // undefined behavior: lifetime of *pb has ended
|
| 2868 |
}
|
| 2869 |
```
|
| 2870 |
|
| 2871 |
— *end example*]
|
|
|
|
| 2900 |
|
| 2901 |
- the storage that o₂ occupies exactly overlays the storage that o₁
|
| 2902 |
occupied, and
|
| 2903 |
- o₁ and o₂ are of the same type (ignoring the top-level cv-qualifiers),
|
| 2904 |
and
|
| 2905 |
+
- o₁ is not a const, complete object, and
|
| 2906 |
- neither o₁ nor o₂ is a potentially-overlapping subobject
|
| 2907 |
[[intro.object]], and
|
| 2908 |
- either o₁ and o₂ are both complete objects, or o₁ and o₂ are direct
|
| 2909 |
subobjects of objects p₁ and p₂, respectively, and p₁ is transparently
|
| 2910 |
replaceable by p₂.
|
|
|
|
| 2933 |
c1.f(); // well-defined; c1 refers to a new object of type C
|
| 2934 |
```
|
| 2935 |
|
| 2936 |
— *end example*]
|
| 2937 |
|
| 2938 |
+
[*Note 5*: If these conditions are not met, a pointer to the new object
|
| 2939 |
can be obtained from a pointer that represents the address of its
|
| 2940 |
storage by calling `std::launder` [[ptr.launder]]. — *end note*]
|
| 2941 |
|
| 2942 |
If a program ends the lifetime of an object of type `T` with static
|
| 2943 |
[[basic.stc.static]], thread [[basic.stc.thread]], or automatic
|
| 2944 |
[[basic.stc.auto]] storage duration and if `T` has a non-trivial
|
| 2945 |
+
destructor,[^9]
|
| 2946 |
+
|
| 2947 |
+
and another object of the original type does not occupy that same
|
| 2948 |
+
storage location when the implicit destructor call takes place, the
|
| 2949 |
+
behavior of the program is undefined. This is true even if the block is
|
| 2950 |
+
exited with an exception.
|
| 2951 |
|
| 2952 |
[*Example 3*:
|
| 2953 |
|
| 2954 |
``` cpp
|
| 2955 |
class T { };
|
|
|
|
| 2963 |
} // undefined behavior at block exit
|
| 2964 |
```
|
| 2965 |
|
| 2966 |
— *end example*]
|
| 2967 |
|
| 2968 |
+
Creating a new object within the storage that a const, complete object
|
| 2969 |
with static, thread, or automatic storage duration occupies, or within
|
| 2970 |
the storage that such a const object used to occupy before its lifetime
|
| 2971 |
ended, results in undefined behavior.
|
| 2972 |
|
| 2973 |
[*Example 4*:
|
|
|
|
| 2989 |
— *end example*]
|
| 2990 |
|
| 2991 |
In this subclause, “before” and “after” refer to the “happens before”
|
| 2992 |
relation [[intro.multithread]].
|
| 2993 |
|
| 2994 |
+
[*Note 6*: Therefore, undefined behavior results if an object that is
|
| 2995 |
being constructed in one thread is referenced from another thread
|
| 2996 |
without adequate synchronization. — *end note*]
|
| 2997 |
|
| 2998 |
### Indeterminate values <a id="basic.indet">[[basic.indet]]</a>
|
| 2999 |
|
|
|
|
| 3012 |
[[basic.fundamental]] or `std::byte` type [[cstddef.syn]] is produced
|
| 3013 |
by the evaluation of:
|
| 3014 |
- the second or third operand of a conditional expression
|
| 3015 |
[[expr.cond]],
|
| 3016 |
- the right operand of a comma expression [[expr.comma]],
|
| 3017 |
+
- the operand of a cast or conversion
|
| 3018 |
+
[[conv.integral]], [[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]
|
| 3019 |
+
to an unsigned ordinary character type or `std::byte` type
|
| 3020 |
[[cstddef.syn]], or
|
| 3021 |
- a discarded-value expression [[expr.context]],
|
| 3022 |
|
| 3023 |
then the result of the operation is an indeterminate value.
|
| 3024 |
- If an indeterminate value of unsigned ordinary character type or
|
|
|
|
| 3049 |
|
| 3050 |
— *end example*]
|
| 3051 |
|
| 3052 |
### Storage duration <a id="basic.stc">[[basic.stc]]</a>
|
| 3053 |
|
| 3054 |
+
#### General <a id="basic.stc.general">[[basic.stc.general]]</a>
|
| 3055 |
+
|
| 3056 |
The *storage duration* is the property of an object that defines the
|
| 3057 |
minimum potential lifetime of the storage containing the object. The
|
| 3058 |
storage duration is determined by the construct used to create the
|
| 3059 |
object and is one of the following:
|
| 3060 |
|
|
|
|
| 3074 |
values of all pointers representing the address of any part of that
|
| 3075 |
region of storage become invalid pointer values [[basic.compound]].
|
| 3076 |
Indirection through an invalid pointer value and passing an invalid
|
| 3077 |
pointer value to a deallocation function have undefined behavior. Any
|
| 3078 |
other use of an invalid pointer value has *implementation-defined*
|
| 3079 |
+
behavior.[^10]
|
| 3080 |
|
| 3081 |
#### Static storage duration <a id="basic.stc.static">[[basic.stc.static]]</a>
|
| 3082 |
|
| 3083 |
+
All variables which
|
| 3084 |
+
|
| 3085 |
+
- do not have thread storage duration and
|
| 3086 |
+
- belong to a namespace scope [[basic.scope.namespace]] or are first
|
| 3087 |
+
declared with the `static` or `extern` keywords [[dcl.stc]]
|
| 3088 |
+
|
| 3089 |
+
have *static storage duration*. The storage for these entities lasts for
|
| 3090 |
+
the duration of the program
|
| 3091 |
+
[[basic.start.static]], [[basic.start.term]].
|
| 3092 |
|
| 3093 |
If a variable with static storage duration has initialization or a
|
| 3094 |
destructor with side effects, it shall not be eliminated even if it
|
| 3095 |
appears to be unused, except that a class object or its copy/move may be
|
| 3096 |
eliminated as specified in [[class.copy.elision]].
|
| 3097 |
|
| 3098 |
+
[*Note 1*: The keyword `static` can be used to declare a block
|
| 3099 |
+
variable [[basic.scope.block]] with static storage duration;
|
| 3100 |
+
[[stmt.dcl]] and [[basic.start.term]] describe the initialization and
|
| 3101 |
+
destruction of such variables. The keyword `static` applied to a class
|
| 3102 |
+
data member in a class definition gives the data member static storage
|
| 3103 |
+
duration [[class.static.data]]. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 3104 |
|
| 3105 |
#### Thread storage duration <a id="basic.stc.thread">[[basic.stc.thread]]</a>
|
| 3106 |
|
| 3107 |
All variables declared with the `thread_local` keyword have
|
| 3108 |
*thread storage duration*. The storage for these entities lasts for the
|
|
|
|
| 3115 |
[[stmt.dcl]] and, if constructed, is destroyed on thread exit
|
| 3116 |
[[basic.start.term]]. — *end note*]
|
| 3117 |
|
| 3118 |
#### Automatic storage duration <a id="basic.stc.auto">[[basic.stc.auto]]</a>
|
| 3119 |
|
| 3120 |
+
Variables that belong to a block or parameter scope and are not
|
| 3121 |
+
explicitly declared `static`, `thread_local`, or `extern` have
|
| 3122 |
+
*automatic storage duration*. The storage for these entities lasts until
|
| 3123 |
+
the block in which they are created exits.
|
| 3124 |
|
| 3125 |
[*Note 1*: These variables are initialized and destroyed as described
|
| 3126 |
in [[stmt.dcl]]. — *end note*]
|
| 3127 |
|
| 3128 |
If a variable with automatic storage duration has initialization or a
|
|
|
|
| 3131 |
it appears to be unused, except that a class object or its copy/move may
|
| 3132 |
be eliminated as specified in [[class.copy.elision]].
|
| 3133 |
|
| 3134 |
#### Dynamic storage duration <a id="basic.stc.dynamic">[[basic.stc.dynamic]]</a>
|
| 3135 |
|
| 3136 |
+
##### General <a id="basic.stc.dynamic.general">[[basic.stc.dynamic.general]]</a>
|
| 3137 |
+
|
| 3138 |
Objects can be created dynamically during program execution
|
| 3139 |
[[intro.execution]], using *new-expression*s [[expr.new]], and destroyed
|
| 3140 |
using *delete-expression*s [[expr.delete]]. A C++ implementation
|
| 3141 |
provides access to, and management of, dynamic storage via the global
|
| 3142 |
+
*allocation functions* `operator new` and `operator new[]` and the
|
| 3143 |
+
global *deallocation functions* `operator delete` and
|
| 3144 |
+
`operator delete[]`.
|
| 3145 |
|
| 3146 |
[*Note 1*: The non-allocating forms described in
|
| 3147 |
[[new.delete.placement]] do not perform allocation or
|
| 3148 |
deallocation. — *end note*]
|
| 3149 |
|
| 3150 |
The library provides default definitions for the global allocation and
|
| 3151 |
deallocation functions. Some global allocation and deallocation
|
| 3152 |
+
functions are replaceable [[new.delete]]; these are attached to the
|
| 3153 |
+
global module [[module.unit]]. A C++ program shall provide at most one
|
| 3154 |
+
definition of a replaceable allocation or deallocation function. Any
|
| 3155 |
+
such function definition replaces the default version provided in the
|
| 3156 |
+
library [[replacement.functions]]. The following allocation and
|
| 3157 |
+
deallocation functions [[support.dynamic]] are implicitly declared in
|
| 3158 |
+
global scope in each translation unit of a program.
|
| 3159 |
|
| 3160 |
``` cpp
|
| 3161 |
[[nodiscard]] void* operator new(std::size_t);
|
| 3162 |
[[nodiscard]] void* operator new(std::size_t, std::align_val_t);
|
| 3163 |
|
|
|
|
| 3173 |
void operator delete[](void*, std::size_t) noexcept;
|
| 3174 |
void operator delete[](void*, std::align_val_t) noexcept;
|
| 3175 |
void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
|
| 3176 |
```
|
| 3177 |
|
| 3178 |
+
These implicit declarations introduce only the function names
|
| 3179 |
+
`operator new`, `operator new[]`, `operator delete`, and
|
| 3180 |
+
`operator delete[]`.
|
| 3181 |
|
| 3182 |
[*Note 2*: The implicit declarations do not introduce the names `std`,
|
| 3183 |
`std::size_t`, `std::align_val_t`, or any other names that the library
|
| 3184 |
uses to declare these names. Thus, a *new-expression*,
|
| 3185 |
*delete-expression*, or function call that refers to one of these
|
| 3186 |
+
functions without importing or including the header `<new>` or importing
|
| 3187 |
+
a C++ library module [[std.modules]] is well-formed. However, referring
|
| 3188 |
+
to `std` or `std::size_t` or `std::align_val_t` is ill-formed unless a
|
| 3189 |
+
standard library declaration
|
| 3190 |
+
[[cstddef.syn]], [[new.syn]], [[std.modules]] of that name precedes
|
| 3191 |
+
[[basic.lookup.general]] the use of that name. — *end note*]
|
| 3192 |
|
| 3193 |
Allocation and/or deallocation functions may also be declared and
|
| 3194 |
defined for any class [[class.free]].
|
| 3195 |
|
| 3196 |
If the behavior of an allocation or deallocation function does not
|
|
|
|
| 3198 |
[[basic.stc.dynamic.allocation]] and
|
| 3199 |
[[basic.stc.dynamic.deallocation]], the behavior is undefined.
|
| 3200 |
|
| 3201 |
##### Allocation functions <a id="basic.stc.dynamic.allocation">[[basic.stc.dynamic.allocation]]</a>
|
| 3202 |
|
| 3203 |
+
An allocation function that is not a class member function shall belong
|
| 3204 |
+
to the global scope and not have a name with internal linkage. The
|
| 3205 |
+
return type shall be `void*`. The first parameter shall have type
|
| 3206 |
+
`std::size_t` [[support.types]]. The first parameter shall not have an
|
| 3207 |
+
associated default argument [[dcl.fct.default]]. The value of the first
|
| 3208 |
+
parameter is interpreted as the requested size of the allocation. An
|
| 3209 |
+
allocation function can be a function template. Such a template shall
|
| 3210 |
+
declare its return type and first parameter as specified above (that is,
|
| 3211 |
+
template parameter types shall not be used in the return type and first
|
| 3212 |
+
parameter type). Allocation function templates shall have two or more
|
| 3213 |
+
parameters.
|
|
|
|
| 3214 |
|
| 3215 |
An allocation function attempts to allocate the requested amount of
|
| 3216 |
storage. If it is successful, it returns the address of the start of a
|
| 3217 |
block of storage whose length in bytes is at least as large as the
|
| 3218 |
requested size. The order, contiguity, and initial value of storage
|
|
|
|
| 3224 |
subsequently passed to a replaceable deallocation function. Furthermore,
|
| 3225 |
for the library allocation functions in [[new.delete.single]] and
|
| 3226 |
[[new.delete.array]], `p0` represents the address of a block of storage
|
| 3227 |
disjoint from the storage for any other object accessible to the caller.
|
| 3228 |
The effect of indirecting through a pointer returned from a request for
|
| 3229 |
+
zero size is undefined.[^11]
|
| 3230 |
|
| 3231 |
For an allocation function other than a reserved placement allocation
|
| 3232 |
function [[new.delete.placement]], the pointer returned on a successful
|
| 3233 |
call shall represent the address of storage that is aligned as follows:
|
| 3234 |
|
|
|
|
| 3268 |
[[expr.typeid]], or for an exception object
|
| 3269 |
[[except.throw]]. — *end note*]
|
| 3270 |
|
| 3271 |
##### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
|
| 3272 |
|
| 3273 |
+
A deallocation function that is not a class member function shall belong
|
| 3274 |
+
to the global scope and not have a name with internal linkage.
|
|
|
|
|
|
|
| 3275 |
|
| 3276 |
A deallocation function is a *destroying operator delete* if it has at
|
| 3277 |
least two parameters and its second parameter is of type
|
| 3278 |
`std::destroying_delete_t`. A destroying operator delete shall be a
|
| 3279 |
class member function named `operator delete`.
|
|
|
|
| 3287 |
parameter shall be `void*`. A deallocation function may have more than
|
| 3288 |
one parameter. A *usual deallocation function* is a deallocation
|
| 3289 |
function whose parameters after the first are
|
| 3290 |
|
| 3291 |
- optionally, a parameter of type `std::destroying_delete_t`, then
|
| 3292 |
+
- optionally, a parameter of type `std::size_t`,[^12] then
|
| 3293 |
- optionally, a parameter of type `std::align_val_t`.
|
| 3294 |
|
| 3295 |
A destroying operator delete shall be a usual deallocation function. A
|
| 3296 |
deallocation function may be an instance of a function template. Neither
|
| 3297 |
the first parameter nor the return type shall depend on a template
|
|
|
|
| 3308 |
If the argument given to a deallocation function in the standard library
|
| 3309 |
is a pointer that is not the null pointer value [[basic.compound]], the
|
| 3310 |
deallocation function shall deallocate the storage referenced by the
|
| 3311 |
pointer, ending the duration of the region of storage.
|
| 3312 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3313 |
#### Duration of subobjects <a id="basic.stc.inherit">[[basic.stc.inherit]]</a>
|
| 3314 |
|
| 3315 |
The storage duration of subobjects and reference members is that of
|
| 3316 |
their complete object [[intro.object]].
|
| 3317 |
|
| 3318 |
### Alignment <a id="basic.align">[[basic.align]]</a>
|
| 3319 |
|
| 3320 |
+
Object types have *alignment requirements*
|
| 3321 |
+
[[basic.fundamental]], [[basic.compound]] which place restrictions on
|
| 3322 |
+
the addresses at which an object of that type may be allocated. An
|
| 3323 |
+
*alignment* is an *implementation-defined* integer value representing
|
| 3324 |
+
the number of bytes between successive addresses at which a given object
|
| 3325 |
+
can be allocated. An object type imposes an alignment requirement on
|
| 3326 |
+
every object of that type; stricter alignment can be requested using the
|
| 3327 |
+
alignment specifier [[dcl.align]].
|
| 3328 |
|
| 3329 |
A *fundamental alignment* is represented by an alignment less than or
|
| 3330 |
equal to the greatest alignment supported by the implementation in all
|
| 3331 |
contexts, which is equal to `alignof(std::max_align_t)`
|
| 3332 |
+
[[support.types]]. The alignment required for a type may be different
|
| 3333 |
when it is used as the type of a complete object and when it is used as
|
| 3334 |
the type of a subobject.
|
| 3335 |
|
| 3336 |
[*Example 1*:
|
| 3337 |
|
|
|
|
| 3389 |
- Two alignments are different when their numeric values are not equal.
|
| 3390 |
- When an alignment is larger than another it represents a stricter
|
| 3391 |
alignment.
|
| 3392 |
|
| 3393 |
[*Note 3*: The runtime pointer alignment function [[ptr.align]] can be
|
| 3394 |
+
used to obtain an aligned pointer within a buffer; an
|
| 3395 |
+
*alignment-specifier* [[dcl.align]] can be used to align storage
|
| 3396 |
+
explicitly. — *end note*]
|
| 3397 |
|
| 3398 |
If a request for a specific extended alignment in a specific context is
|
| 3399 |
not supported by an implementation, the program is ill-formed.
|
| 3400 |
|
| 3401 |
### Temporary objects <a id="class.temporary">[[class.temporary]]</a>
|
| 3402 |
|
| 3403 |
Temporary objects are created
|
| 3404 |
|
| 3405 |
- when a prvalue is converted to an xvalue [[conv.rval]],
|
| 3406 |
- when needed by the implementation to pass or return an object of
|
| 3407 |
+
trivially copyable type (see below), and
|
| 3408 |
- when throwing an exception [[except.throw]]. \[*Note 1*: The lifetime
|
| 3409 |
of exception objects is described in [[except.throw]]. — *end note*]
|
| 3410 |
|
| 3411 |
Even when the creation of the temporary object is unevaluated
|
| 3412 |
+
[[expr.context]], all the semantic restrictions shall be respected as if
|
| 3413 |
the temporary object had been created and later destroyed.
|
| 3414 |
|
| 3415 |
[*Note 2*: This includes accessibility [[class.access]] and whether it
|
| 3416 |
is deleted, for the constructor selected and for the destructor.
|
| 3417 |
However, in the special case of the operand of a *decltype-specifier*
|
| 3418 |
+
[[dcl.type.decltype]], no temporary is introduced, so the foregoing does
|
| 3419 |
+
not apply to such a prvalue. — *end note*]
|
| 3420 |
|
| 3421 |
The materialization of a temporary object is generally delayed as long
|
| 3422 |
as possible in order to avoid creating unnecessary temporary objects.
|
| 3423 |
|
| 3424 |
[*Note 3*:
|
| 3425 |
|
| 3426 |
Temporary objects are materialized:
|
| 3427 |
|
| 3428 |
+
- when binding a reference to a prvalue
|
| 3429 |
+
[[dcl.init.ref]], [[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]], [[expr.const.cast]], [[expr.cast]],
|
| 3430 |
+
- when performing member access on a class prvalue
|
| 3431 |
+
[[expr.ref]], [[expr.mptr.oper]],
|
|
|
|
| 3432 |
- when performing an array-to-pointer conversion or subscripting on an
|
| 3433 |
+
array prvalue [[conv.array]], [[expr.sub]],
|
| 3434 |
- when initializing an object of type `std::initializer_list<T>` from a
|
| 3435 |
*braced-init-list* [[dcl.init.list]],
|
| 3436 |
+
- for certain unevaluated operands [[expr.typeid]], [[expr.sizeof]], and
|
|
|
|
| 3437 |
- when a prvalue that has type other than cv `void` appears as a
|
| 3438 |
+
discarded-value expression [[expr.context]].
|
| 3439 |
|
| 3440 |
— *end note*]
|
| 3441 |
|
| 3442 |
[*Example 1*:
|
| 3443 |
|
|
|
|
| 3494 |
|
| 3495 |
[*Note 4*: This latitude is granted to allow objects of class type to
|
| 3496 |
be passed to or returned from functions in registers. — *end note*]
|
| 3497 |
|
| 3498 |
When an implementation introduces a temporary object of a class that has
|
| 3499 |
+
a non-trivial constructor [[class.default.ctor]], [[class.copy.ctor]],
|
| 3500 |
+
it shall ensure that a constructor is called for the temporary object.
|
| 3501 |
+
Similarly, the destructor shall be called for a temporary with a
|
| 3502 |
+
non-trivial destructor [[class.dtor]]. Temporary objects are destroyed
|
| 3503 |
+
as the last step in evaluating the full-expression [[intro.execution]]
|
| 3504 |
+
that (lexically) contains the point where they were created. This is
|
| 3505 |
+
true even if that evaluation ends in throwing an exception. The value
|
| 3506 |
+
computations and side effects of destroying a temporary object are
|
| 3507 |
+
associated only with the full-expression, not with any specific
|
| 3508 |
+
subexpression.
|
| 3509 |
|
| 3510 |
+
There are four contexts in which temporaries are destroyed at a
|
| 3511 |
different point than the end of the full-expression. The first context
|
| 3512 |
is when a default constructor is called to initialize an element of an
|
| 3513 |
array with no corresponding initializer [[dcl.init]]. The second context
|
| 3514 |
is when a copy constructor is called to copy an element of an array
|
| 3515 |
+
while the entire array is copied
|
| 3516 |
+
[[expr.prim.lambda.capture]], [[class.copy.ctor]]. In either case, if
|
| 3517 |
+
the constructor has one or more default arguments, the destruction of
|
| 3518 |
+
every temporary created in a default argument is sequenced before the
|
| 3519 |
+
construction of the next array element, if any.
|
| 3520 |
|
| 3521 |
+
The third context is when a reference binds to a temporary object.[^13]
|
| 3522 |
+
|
| 3523 |
+
The temporary object to which the reference is bound or the temporary
|
| 3524 |
+
object that is the complete object of a subobject to which the reference
|
| 3525 |
+
is bound persists for the lifetime of the reference if the glvalue to
|
| 3526 |
+
which the reference is bound was obtained through one of the following:
|
| 3527 |
|
| 3528 |
- a temporary materialization conversion [[conv.rval]],
|
| 3529 |
- `(` *expression* `)`, where *expression* is one of these expressions,
|
| 3530 |
- subscripting [[expr.sub]] of an array operand, where that operand is
|
| 3531 |
one of these expressions,
|
|
|
|
| 3564 |
|
| 3565 |
— *end example*]
|
| 3566 |
|
| 3567 |
[*Note 5*:
|
| 3568 |
|
| 3569 |
+
An explicit type conversion [[expr.type.conv]], [[expr.cast]] is
|
| 3570 |
interpreted as a sequence of elementary casts, covered above.
|
| 3571 |
|
| 3572 |
[*Example 3*:
|
| 3573 |
|
| 3574 |
``` cpp
|
|
|
|
| 3611 |
`return` statement [[stmt.return]] is not extended; the temporary is
|
| 3612 |
destroyed at the end of the full-expression in the `return` statement.
|
| 3613 |
- A temporary bound to a reference in a *new-initializer* [[expr.new]]
|
| 3614 |
persists until the completion of the full-expression containing the
|
| 3615 |
*new-initializer*.
|
| 3616 |
+
\[*Note 7*: This might introduce a dangling reference. — *end note*]
|
| 3617 |
\[*Example 5*:
|
| 3618 |
``` cpp
|
| 3619 |
struct S { int mi; const std::pair<int,int>& mp; };
|
| 3620 |
S a { 1, {2,3} };
|
| 3621 |
S* p = new S{ 1, {2,3} }; // creates dangling reference
|
| 3622 |
```
|
| 3623 |
|
| 3624 |
— *end example*]
|
| 3625 |
|
| 3626 |
+
The fourth context is when a temporary object other than a function
|
| 3627 |
+
parameter object is created in the *for-range-initializer* of a
|
| 3628 |
+
range-based `for` statement. If such a temporary object would otherwise
|
| 3629 |
+
be destroyed at the end of the *for-range-initializer* full-expression,
|
| 3630 |
+
the object persists for the lifetime of the reference initialized by the
|
| 3631 |
+
*for-range-initializer*.
|
| 3632 |
+
|
| 3633 |
+
The destruction of a temporary whose lifetime is not extended beyond the
|
| 3634 |
+
full-expression in which it was created is sequenced before the
|
| 3635 |
+
destruction of every temporary which is constructed earlier in the same
|
| 3636 |
+
full-expression. If the lifetime of two or more temporaries with
|
| 3637 |
+
lifetimes extending beyond the full-expressions in which they were
|
| 3638 |
+
created ends at the same point, these temporaries are destroyed at that
|
| 3639 |
+
point in the reverse order of the completion of their construction. In
|
| 3640 |
+
addition, the destruction of such temporaries shall take into account
|
| 3641 |
+
the ordering of destruction of objects with static, thread, or automatic
|
| 3642 |
+
storage duration
|
| 3643 |
+
[[basic.stc.static]], [[basic.stc.thread]], [[basic.stc.auto]]; that is,
|
| 3644 |
+
if `obj1` is an object with the same storage duration as the temporary
|
| 3645 |
+
and created before the temporary is created the temporary shall be
|
| 3646 |
+
destroyed before `obj1` is destroyed; if `obj2` is an object with the
|
| 3647 |
+
same storage duration as the temporary and created after the temporary
|
| 3648 |
+
is created the temporary shall be destroyed after `obj2` is destroyed.
|
| 3649 |
|
| 3650 |
[*Example 6*:
|
| 3651 |
|
| 3652 |
``` cpp
|
| 3653 |
struct S {
|
|
|
|
| 3681 |
|
| 3682 |
— *end example*]
|
| 3683 |
|
| 3684 |
## Types <a id="basic.types">[[basic.types]]</a>
|
| 3685 |
|
| 3686 |
+
### General <a id="basic.types.general">[[basic.types.general]]</a>
|
| 3687 |
+
|
| 3688 |
[*Note 1*: [[basic.types]] and the subclauses thereof impose
|
| 3689 |
requirements on implementations regarding the representation of types.
|
| 3690 |
There are two kinds of types: fundamental types and compound types.
|
| 3691 |
Types describe objects [[intro.object]], references [[dcl.ref]], or
|
| 3692 |
functions [[dcl.fct]]. — *end note*]
|
| 3693 |
|
| 3694 |
For any object (other than a potentially-overlapping subobject) of
|
| 3695 |
trivially copyable type `T`, whether or not the object holds a valid
|
| 3696 |
value of type `T`, the underlying bytes [[intro.memory]] making up the
|
| 3697 |
object can be copied into an array of `char`, `unsigned char`, or
|
| 3698 |
+
`std::byte` [[cstddef.syn]].[^14]
|
| 3699 |
+
|
| 3700 |
+
If the content of that array is copied back into the object, the object
|
| 3701 |
+
shall subsequently hold its original value.
|
| 3702 |
|
| 3703 |
[*Example 1*:
|
| 3704 |
|
| 3705 |
``` cpp
|
| 3706 |
constexpr std::size_t N = sizeof(T);
|
|
|
|
| 3710 |
std::memcpy(&obj, buf, N); // at this point, each subobject of obj of scalar type holds its original value
|
| 3711 |
```
|
| 3712 |
|
| 3713 |
— *end example*]
|
| 3714 |
|
| 3715 |
+
For two distinct objects `obj1` and `obj2` of trivially copyable type
|
| 3716 |
+
`T`, where neither `obj1` nor `obj2` is a potentially-overlapping
|
| 3717 |
+
subobject, if the underlying bytes [[intro.memory]] making up `obj1` are
|
| 3718 |
+
copied into `obj2`,[^15]
|
| 3719 |
+
|
| 3720 |
+
`obj2` shall subsequently hold the same value as `obj1`.
|
| 3721 |
|
| 3722 |
[*Example 2*:
|
| 3723 |
|
| 3724 |
``` cpp
|
| 3725 |
T* t1p;
|
|
|
|
| 3738 |
`T` is the set of bits that participate in representing a value of type
|
| 3739 |
`T`. Bits in the object representation that are not part of the value
|
| 3740 |
representation are *padding bits*. For trivially copyable types, the
|
| 3741 |
value representation is a set of bits in the object representation that
|
| 3742 |
determines a *value*, which is one discrete element of an
|
| 3743 |
+
*implementation-defined* set of values.[^16]
|
| 3744 |
|
| 3745 |
A class that has been declared but not defined, an enumeration type in
|
| 3746 |
certain contexts [[dcl.enum]], or an array of unknown bound or of
|
| 3747 |
+
incomplete element type, is an *incompletely-defined object type*.[^17]
|
| 3748 |
+
|
| 3749 |
Incompletely-defined object types and cv `void` are *incomplete types*
|
| 3750 |
+
[[basic.fundamental]].
|
|
|
|
| 3751 |
|
| 3752 |
+
[*Note 2*: Objects cannot be defined to have an incomplete type
|
| 3753 |
+
[[basic.def]]. — *end note*]
|
| 3754 |
+
|
| 3755 |
+
A class type (such as “`class X`”) can be incomplete at one point in a
|
| 3756 |
translation unit and complete later on; the type “`class X`” is the same
|
| 3757 |
+
type at both points. The declared type of an array object can be an
|
| 3758 |
array of incomplete class type and therefore incomplete; if the class
|
| 3759 |
type is completed later on in the translation unit, the array type
|
| 3760 |
becomes complete; the array type at those two points is the same type.
|
| 3761 |
+
The declared type of an array object can be an array of unknown bound
|
| 3762 |
and therefore be incomplete at one point in a translation unit and
|
| 3763 |
complete later on; the array types at those two points (“array of
|
| 3764 |
+
unknown bound of `T`” and “array of `N` `T`”) are different types.
|
| 3765 |
+
|
| 3766 |
+
[*Note 3*: The type of a pointer or reference to array of unknown bound
|
| 3767 |
+
permanently points to or refers to an incomplete type. An array of
|
| 3768 |
+
unknown bound named by a `typedef` declaration permanently refers to an
|
| 3769 |
+
incomplete type. In either case, the array type cannot be
|
| 3770 |
+
completed. — *end note*]
|
| 3771 |
|
| 3772 |
[*Example 3*:
|
| 3773 |
|
| 3774 |
``` cpp
|
| 3775 |
class X; // X is an incomplete type
|
|
|
|
| 3780 |
UNKA** arrpp;
|
| 3781 |
|
| 3782 |
void foo() {
|
| 3783 |
xp++; // error: X is incomplete
|
| 3784 |
arrp++; // error: incomplete type
|
| 3785 |
+
arrpp++; // OK, sizeof UNKA* is known
|
| 3786 |
}
|
| 3787 |
|
| 3788 |
struct X { int i; }; // now X is a complete type
|
| 3789 |
int arr[10]; // now the type of arr is complete
|
| 3790 |
|
| 3791 |
X x;
|
| 3792 |
void bar() {
|
| 3793 |
xp = &x; // OK; type is ``pointer to X''
|
| 3794 |
+
arrp = &arr; // OK; qualification conversion[conv.qual]
|
| 3795 |
+
xp++; // OK, X is complete
|
| 3796 |
arrp++; // error: UNKA can't be completed
|
| 3797 |
}
|
| 3798 |
```
|
| 3799 |
|
| 3800 |
— *end example*]
|
| 3801 |
|
| 3802 |
+
[*Note 4*: The rules for declarations and expressions describe in which
|
| 3803 |
contexts incomplete types are prohibited. — *end note*]
|
| 3804 |
|
| 3805 |
An *object type* is a (possibly cv-qualified) type that is not a
|
| 3806 |
function type, not a reference type, and not cv `void`.
|
| 3807 |
|
|
|
|
| 3827 |
- a reference type; or
|
| 3828 |
- an array of literal type; or
|
| 3829 |
- a possibly cv-qualified class type [[class]] that has all of the
|
| 3830 |
following properties:
|
| 3831 |
- it has a constexpr destructor [[dcl.constexpr]],
|
| 3832 |
+
- all of its non-static non-variant data members and base classes are
|
| 3833 |
+
of non-volatile literal types, and
|
| 3834 |
+
- it
|
| 3835 |
+
- is a closure type [[expr.prim.lambda.closure]],
|
| 3836 |
+
- is an aggregate union type that has either no variant members or
|
| 3837 |
+
at least one variant member of non-volatile literal type,
|
| 3838 |
+
- is a non-union aggregate type for which each of its anonymous
|
| 3839 |
+
union members satisfies the above requirements for an aggregate
|
| 3840 |
+
union type, or
|
| 3841 |
+
- has at least one constexpr constructor or constructor template
|
| 3842 |
+
(possibly inherited [[namespace.udecl]] from a base class) that is
|
| 3843 |
+
not a copy or move constructor.
|
| 3844 |
|
| 3845 |
+
[*Note 5*: A literal type is one for which it might be possible to
|
| 3846 |
create an object within a constant expression. It is not a guarantee
|
| 3847 |
that it is possible to create such an object, nor is it a guarantee that
|
| 3848 |
any object of that type will be usable in a constant
|
| 3849 |
expression. — *end note*]
|
| 3850 |
|
| 3851 |
+
Two types *cv1* `T1` and *cv2* `T2` are *layout-compatible types* if
|
| 3852 |
`T1` and `T2` are the same type, layout-compatible enumerations
|
| 3853 |
[[dcl.enum]], or layout-compatible standard-layout class types
|
| 3854 |
[[class.mem]].
|
| 3855 |
|
| 3856 |
### Fundamental types <a id="basic.fundamental">[[basic.fundamental]]</a>
|
|
|
|
| 3886 |
An unsigned integer type has the same object representation, value
|
| 3887 |
representation, and alignment requirements [[basic.align]] as the
|
| 3888 |
corresponding signed integer type. For each value x of a signed integer
|
| 3889 |
type, the value of the corresponding unsigned integer type congruent to
|
| 3890 |
x modulo 2ᴺ has the same value of corresponding bits in its value
|
| 3891 |
+
representation.[^18]
|
| 3892 |
|
| 3893 |
[*Example 1*: The value -1 of a signed integer type has the same
|
| 3894 |
representation as the largest value of the corresponding unsigned
|
| 3895 |
type. — *end example*]
|
| 3896 |
|
| 3897 |
**Table: Minimum width** <a id="basic.fundamental.width">[basic.fundamental.width]</a>
|
| 3898 |
|
| 3899 |
| Type | Minimum width $N$ |
|
| 3900 |
+
| --------------- | ----------------- |
|
| 3901 |
| `signed char` | 8 |
|
| 3902 |
+
| `short int` | 16 |
|
| 3903 |
| `int` | 16 |
|
| 3904 |
+
| `long int` | 32 |
|
| 3905 |
+
| `long long int` | 64 |
|
| 3906 |
|
| 3907 |
|
| 3908 |
The width of each signed integer type shall not be less than the values
|
| 3909 |
specified in [[basic.fundamental.width]]. The value representation of a
|
| 3910 |
signed or unsigned integer type comprises N bits, where N is the
|
| 3911 |
respective width. Each set of values for any padding bits
|
| 3912 |
+
[[basic.types.general]] in the object representation are alternative
|
| 3913 |
representations of the value specified by the value representation.
|
| 3914 |
|
| 3915 |
[*Note 3*: Padding bits have unspecified value, but cannot cause traps.
|
| 3916 |
In contrast, see ISO C 6.2.6.2. — *end note*]
|
| 3917 |
|
|
|
|
| 3938 |
representable values as the underlying type. Further, each value has the
|
| 3939 |
same representation in both types.
|
| 3940 |
|
| 3941 |
Type `char` is a distinct type that has an *implementation-defined*
|
| 3942 |
choice of “`signed char`” or “`unsigned char`” as its underlying type.
|
| 3943 |
+
The three types `char`, `signed char`, and `unsigned char` are
|
| 3944 |
+
collectively called *ordinary character types*. The ordinary character
|
| 3945 |
+
types and `char8_t` are collectively called *narrow character types*.
|
| 3946 |
+
For narrow character types, each possible bit pattern of the object
|
| 3947 |
+
representation represents a distinct value.
|
|
|
|
|
|
|
| 3948 |
|
| 3949 |
[*Note 5*: This requirement does not hold for other
|
| 3950 |
types. — *end note*]
|
| 3951 |
|
| 3952 |
[*Note 6*: A bit-field of narrow character type whose width is larger
|
| 3953 |
than the width of that type has padding bits; see
|
| 3954 |
+
[[basic.types.general]]. — *end note*]
|
| 3955 |
|
| 3956 |
Type `wchar_t` is a distinct type that has an *implementation-defined*
|
| 3957 |
+
signed or unsigned integer type as its underlying type.
|
|
|
|
|
|
|
|
|
|
| 3958 |
|
| 3959 |
Type `char8_t` denotes a distinct type whose underlying type is
|
| 3960 |
`unsigned char`. Types `char16_t` and `char32_t` denote distinct types
|
| 3961 |
whose underlying types are `uint_least16_t` and `uint_least32_t`,
|
| 3962 |
respectively, in `<cstdint>`.
|
|
|
|
| 3967 |
`bool` are `true` and `false`.
|
| 3968 |
|
| 3969 |
[*Note 7*: There are no `signed`, `unsigned`, `short`, or `long bool`
|
| 3970 |
types or values. — *end note*]
|
| 3971 |
|
| 3972 |
+
The types `char`, `wchar_t`, `char8_t`, `char16_t`, and `char32_t` are
|
| 3973 |
+
collectively called *character types*. The character types, `bool`, the
|
| 3974 |
+
signed and unsigned integer types, and cv-qualified versions
|
| 3975 |
+
[[basic.type.qualifier]] thereof, are collectively termed *integral
|
| 3976 |
types*. A synonym for integral type is *integer type*.
|
| 3977 |
|
| 3978 |
[*Note 8*: Enumerations [[dcl.enum]] are not integral; however,
|
| 3979 |
unscoped enumerations can be promoted to integral types as specified in
|
| 3980 |
[[conv.prom]]. — *end note*]
|
| 3981 |
|
| 3982 |
+
The three distinct types `float`, `double`, and `long double` can
|
| 3983 |
+
represent floating-point numbers. The type `double` provides at least as
|
| 3984 |
+
much precision as `float`, and the type `long double` provides at least
|
| 3985 |
+
as much precision as `double`. The set of values of the type `float` is
|
| 3986 |
+
a subset of the set of values of the type `double`; the set of values of
|
| 3987 |
+
the type `double` is a subset of the set of values of the type
|
| 3988 |
+
`long double`. The types `float`, `double`, and `long double`, and
|
| 3989 |
+
cv-qualified versions [[basic.type.qualifier]] thereof, are collectively
|
| 3990 |
+
termed *standard floating-point types*. An implementation may also
|
| 3991 |
+
provide additional types that represent floating-point values and define
|
| 3992 |
+
them (and cv-qualified versions thereof) to be *extended floating-point
|
| 3993 |
+
types*. The standard and extended floating-point types are collectively
|
| 3994 |
+
termed *floating-point types*.
|
| 3995 |
+
|
| 3996 |
+
[*Note 9*: Any additional implementation-specific types representing
|
| 3997 |
+
floating-point values that are not defined by the implementation to be
|
| 3998 |
+
extended floating-point types are not considered to be floating-point
|
| 3999 |
+
types, and this document imposes no requirements on them or their
|
| 4000 |
+
interactions with floating-point types. — *end note*]
|
| 4001 |
+
|
| 4002 |
+
Except as specified in [[basic.extended.fp]], the object and value
|
| 4003 |
+
representations and accuracy of operations of floating-point types are
|
| 4004 |
*implementation-defined*.
|
| 4005 |
|
| 4006 |
+
Integral and floating-point types are collectively termed *arithmetic
|
| 4007 |
+
types*.
|
| 4008 |
|
| 4009 |
+
[*Note 10*: Properties of the arithmetic types, such as their minimum
|
| 4010 |
+
and maximum representable value, can be queried using the facilities in
|
| 4011 |
+
the standard library headers `<limits>`, `<climits>`, and
|
| 4012 |
+
`<cfloat>`. — *end note*]
|
| 4013 |
|
| 4014 |
A type cv `void` is an incomplete type that cannot be completed; such a
|
| 4015 |
type has an empty set of values. It is used as the return type for
|
| 4016 |
functions that do not return a value. Any expression can be explicitly
|
| 4017 |
+
converted to type cv `void`
|
| 4018 |
+
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]. An expression
|
| 4019 |
+
of type cv `void` shall be used only as an expression statement
|
| 4020 |
+
[[stmt.expr]], as an operand of a comma expression [[expr.comma]], as a
|
| 4021 |
+
second or third operand of `?:` [[expr.cond]], as the operand of
|
| 4022 |
+
`typeid`, `noexcept`, or `decltype`, as the expression in a `return`
|
| 4023 |
+
statement [[stmt.return]] for a function with the return type cv `void`,
|
| 4024 |
+
or as the operand of an explicit conversion to type cv `void`.
|
| 4025 |
|
| 4026 |
A value of type `std::nullptr_t` is a null pointer constant
|
| 4027 |
[[conv.ptr]]. Such values participate in the pointer and the
|
| 4028 |
+
pointer-to-member conversions [[conv.ptr]], [[conv.mem]].
|
| 4029 |
`sizeof(std::nullptr_t)` shall be equal to `sizeof(void*)`.
|
| 4030 |
|
| 4031 |
The types described in this subclause are called *fundamental types*.
|
| 4032 |
|
| 4033 |
+
[*Note 11*: Even if the implementation defines two or more fundamental
|
| 4034 |
types to have the same value representation, they are nevertheless
|
| 4035 |
different types. — *end note*]
|
| 4036 |
|
| 4037 |
+
### Optional extended floating-point types <a id="basic.extended.fp">[[basic.extended.fp]]</a>
|
| 4038 |
+
|
| 4039 |
+
If the implementation supports an extended floating-point type
|
| 4040 |
+
[[basic.fundamental]] whose properties are specified by the ISO/IEC/IEEE
|
| 4041 |
+
60559 floating-point interchange format binary16, then the
|
| 4042 |
+
*typedef-name* `std::float16_t` is defined in the header `<stdfloat>`
|
| 4043 |
+
and names such a type, the macro `__STDCPP_FLOAT16_T__` is defined
|
| 4044 |
+
[[cpp.predefined]], and the floating-point literal suffixes `f16` and
|
| 4045 |
+
`F16` are supported [[lex.fcon]].
|
| 4046 |
+
|
| 4047 |
+
If the implementation supports an extended floating-point type whose
|
| 4048 |
+
properties are specified by the ISO/IEC/IEEE 60559 floating-point
|
| 4049 |
+
interchange format binary32, then the *typedef-name* `std::float32_t` is
|
| 4050 |
+
defined in the header `<stdfloat>` and names such a type, the macro
|
| 4051 |
+
`__STDCPP_FLOAT32_T__` is defined, and the floating-point literal
|
| 4052 |
+
suffixes `f32` and `F32` are supported.
|
| 4053 |
+
|
| 4054 |
+
If the implementation supports an extended floating-point type whose
|
| 4055 |
+
properties are specified by the ISO/IEC/IEEE 60559 floating-point
|
| 4056 |
+
interchange format binary64, then the *typedef-name* `std::float64_t` is
|
| 4057 |
+
defined in the header `<stdfloat>` and names such a type, the macro
|
| 4058 |
+
`__STDCPP_FLOAT64_T__` is defined, and the floating-point literal
|
| 4059 |
+
suffixes `f64` and `F64` are supported.
|
| 4060 |
+
|
| 4061 |
+
If the implementation supports an extended floating-point type whose
|
| 4062 |
+
properties are specified by the ISO/IEC/IEEE 60559 floating-point
|
| 4063 |
+
interchange format binary128, then the *typedef-name* `std::float128_t`
|
| 4064 |
+
is defined in the header `<stdfloat>` and names such a type, the macro
|
| 4065 |
+
`__STDCPP_FLOAT128_T__` is defined, and the floating-point literal
|
| 4066 |
+
suffixes `f128` and `F128` are supported.
|
| 4067 |
+
|
| 4068 |
+
If the implementation supports an extended floating-point type with the
|
| 4069 |
+
properties, as specified by ISO/IEC/IEEE 60559, of radix (b) of 2,
|
| 4070 |
+
storage width in bits (k) of 16, precision in bits (p) of 8, maximum
|
| 4071 |
+
exponent (emax) of 127, and exponent field width in bits (w) of 8, then
|
| 4072 |
+
the *typedef-name* `std::bfloat16_t` is defined in the header
|
| 4073 |
+
`<stdfloat>` and names such a type, the macro `__STDCPP_BFLOAT16_T__` is
|
| 4074 |
+
defined, and the floating-point literal suffixes `bf16` and `BF16` are
|
| 4075 |
+
supported.
|
| 4076 |
+
|
| 4077 |
+
[*Note 1*: A summary of the parameters for each type is given in
|
| 4078 |
+
[[basic.extended.fp]]. The precision p includes the implicit 1 bit at
|
| 4079 |
+
the beginning of the mantissa, so the storage used for the mantissa is
|
| 4080 |
+
p-1 bits. ISO/IEC/IEEE 60559 does not assign a name for a type having
|
| 4081 |
+
the parameters specified for `std::bfloat16_t`. — *end note*]
|
| 4082 |
+
|
| 4083 |
+
**Table: Properties of named extended floating-point types** <a id="basic.extended.fp">[basic.extended.fp]</a>
|
| 4084 |
+
|
| 4085 |
+
| Parameter | `float16_t` | `float32_t` | `float64_t` | `float128_t` | `bfloat16_t` |
|
| 4086 |
+
| --------------------------------- | ----------- | ----------- | ----------- | ------------ | ------------ |
|
| 4087 |
+
| ISO/IEC/IEEE 60559 name | binary16 | binary32 | binary64 | binary128 | |
|
| 4088 |
+
| $k$, storage width in bits | 16 | 32 | 64 | 128 | 16 |
|
| 4089 |
+
| $p$, precision in bits | 11 | 24 | 53 | 113 | 8 |
|
| 4090 |
+
| $emax$, maximum exponent | 15 | 127 | 1023 | 16383 | 127 |
|
| 4091 |
+
| $w$, exponent field width in bits | 5 | 8 | 11 | 15 | 8 |
|
| 4092 |
+
|
| 4093 |
+
|
| 4094 |
+
*Recommended practice:* Any names that the implementation provides for
|
| 4095 |
+
the extended floating-point types described in this subsection that are
|
| 4096 |
+
in addition to the names defined in the `<stdfloat>` header should be
|
| 4097 |
+
chosen to increase compatibility and interoperability with the
|
| 4098 |
+
interchange types `_Float16`, `_Float32`, `_Float64`, and `_Float128`
|
| 4099 |
+
defined in ISO/IEC TS 18661-3 and with future versions of the C
|
| 4100 |
+
standard.
|
| 4101 |
+
|
| 4102 |
### Compound types <a id="basic.compound">[[basic.compound]]</a>
|
| 4103 |
|
| 4104 |
Compound types can be constructed in the following ways:
|
| 4105 |
|
| 4106 |
- *arrays* of objects of a given type, [[dcl.array]];
|
|
|
|
| 4116 |
a set of types, enumerations and functions for manipulating these
|
| 4117 |
objects [[class.mfct]], and a set of restrictions on the access to
|
| 4118 |
these entities [[class.access]];
|
| 4119 |
- *unions*, which are classes capable of containing objects of different
|
| 4120 |
types at different times, [[class.union]];
|
| 4121 |
+
- *enumerations*, which comprise a set of named constant values,
|
|
|
|
| 4122 |
[[dcl.enum]];
|
| 4123 |
+
- *pointers to non-static class members*,[^19] which identify members of
|
| 4124 |
+
a given type within objects of a given class, [[dcl.mptr]]. Pointers
|
| 4125 |
+
to data members and pointers to member functions are collectively
|
| 4126 |
+
called *pointer-to-member* types.
|
| 4127 |
|
| 4128 |
These methods of constructing types can be applied recursively;
|
| 4129 |
restrictions are mentioned in [[dcl.meaning]]. Constructing a type such
|
| 4130 |
that the number of bytes in its object representation exceeds the
|
| 4131 |
maximum value representable in the type `std::size_t` [[support.types]]
|
|
|
|
| 4156 |
- the *null pointer value* for that type, or
|
| 4157 |
- an *invalid pointer value*.
|
| 4158 |
|
| 4159 |
A value of a pointer type that is a pointer to or past the end of an
|
| 4160 |
object *represents the address* of the first byte in memory
|
| 4161 |
+
[[intro.memory]] occupied by the object[^20]
|
| 4162 |
+
|
| 4163 |
+
or the first byte in memory after the end of the storage occupied by the
|
| 4164 |
+
object, respectively.
|
| 4165 |
|
| 4166 |
[*Note 2*: A pointer past the end of an object [[expr.add]] is not
|
| 4167 |
+
considered to point to an unrelated object of the object’s type, even if
|
| 4168 |
+
the unrelated object is located at that address. A pointer value becomes
|
| 4169 |
+
invalid when the storage it denotes reaches the end of its storage
|
| 4170 |
+
duration; see [[basic.stc]]. — *end note*]
|
| 4171 |
|
| 4172 |
+
For purposes of pointer arithmetic [[expr.add]] and comparison
|
| 4173 |
+
[[expr.rel]], [[expr.eq]], a pointer past the end of the last element of
|
| 4174 |
+
an array `x` of n elements is considered to be equivalent to a pointer
|
| 4175 |
+
to a hypothetical array element n of `x` and an object of type `T` that
|
| 4176 |
+
is not an array element is considered to belong to an array with one
|
| 4177 |
+
element of type `T`. The value representation of pointer types is
|
| 4178 |
+
*implementation-defined*. Pointers to layout-compatible types shall have
|
| 4179 |
+
the same value representation and alignment requirements
|
| 4180 |
[[basic.align]].
|
| 4181 |
|
| 4182 |
[*Note 3*: Pointers to over-aligned types [[basic.align]] have no
|
| 4183 |
special representation, but their range of valid values is restricted by
|
| 4184 |
the extended alignment requirement. — *end note*]
|
|
|
|
| 4187 |
|
| 4188 |
- they are the same object, or
|
| 4189 |
- one is a union object and the other is a non-static data member of
|
| 4190 |
that object [[class.union]], or
|
| 4191 |
- one is a standard-layout class object and the other is the first
|
| 4192 |
+
non-static data member of that object or any base class subobject of
|
| 4193 |
+
that object [[class.mem]], or
|
|
|
|
| 4194 |
- there exists an object *c* such that *a* and *c* are
|
| 4195 |
pointer-interconvertible, and *c* and *b* are
|
| 4196 |
pointer-interconvertible.
|
| 4197 |
|
| 4198 |
If two objects are pointer-interconvertible, then they have the same
|
|
|
|
| 4201 |
|
| 4202 |
[*Note 4*: An array object and its first element are not
|
| 4203 |
pointer-interconvertible, even though they have the same
|
| 4204 |
address. — *end note*]
|
| 4205 |
|
| 4206 |
+
A byte of storage *b* is *reachable through* a pointer value that points
|
| 4207 |
+
to an object *x* if there is an object *y*, pointer-interconvertible
|
| 4208 |
+
with *x*, such that *b* is within the storage occupied by *y*, or the
|
| 4209 |
+
immediately-enclosing array object if *y* is an array element.
|
| 4210 |
+
|
| 4211 |
A pointer to cv `void` can be used to point to objects of unknown type.
|
| 4212 |
Such a pointer shall be able to hold any object pointer. An object of
|
| 4213 |
+
type “pointer to cv `void`” shall have the same representation and
|
| 4214 |
+
alignment requirements as an object of type “pointer to cv `char`”.
|
| 4215 |
|
| 4216 |
### CV-qualifiers <a id="basic.type.qualifier">[[basic.type.qualifier]]</a>
|
| 4217 |
|
| 4218 |
+
Each type other than a function or reference type is part of a group of
|
| 4219 |
+
four distinct, but related, types: a *cv-unqualified* version, a
|
| 4220 |
+
*const-qualified* version, a *volatile-qualified* version, and a
|
| 4221 |
+
*const-volatile-qualified* version. The types in each such group shall
|
| 4222 |
+
have the same representation and alignment requirements
|
| 4223 |
+
[[basic.align]].[^21]
|
| 4224 |
+
|
| 4225 |
+
A function or reference type is always cv-unqualified.
|
|
|
|
| 4226 |
|
| 4227 |
- A *const object* is an object of type `const T` or a non-mutable
|
| 4228 |
subobject of a const object.
|
| 4229 |
- A *volatile object* is an object of type `volatile T` or a subobject
|
| 4230 |
of a volatile object.
|
| 4231 |
- A *const volatile object* is an object of type `const volatile T`, a
|
| 4232 |
non-mutable subobject of a const volatile object, a const subobject of
|
| 4233 |
a volatile object, or a non-mutable volatile subobject of a const
|
| 4234 |
object.
|
| 4235 |
|
| 4236 |
+
[*Note 1*: The type of an object [[intro.object]] includes the
|
| 4237 |
+
*cv-qualifier*s specified in the *decl-specifier-seq* [[dcl.spec]],
|
| 4238 |
+
*declarator* [[dcl.decl]], *type-id* [[dcl.name]], or *new-type-id*
|
| 4239 |
+
[[expr.new]] when the object is created. — *end note*]
|
| 4240 |
|
| 4241 |
Except for array types, a compound type [[basic.compound]] is not
|
| 4242 |
cv-qualified by the cv-qualifiers (if any) of the types from which it is
|
| 4243 |
compounded.
|
| 4244 |
|
| 4245 |
An array type whose elements are cv-qualified is also considered to have
|
| 4246 |
the same cv-qualifications as its elements.
|
| 4247 |
|
| 4248 |
+
[*Note 2*: Cv-qualifiers applied to an array type attach to the
|
| 4249 |
underlying element type, so the notation “cv `T`”, where `T` is an array
|
| 4250 |
type, refers to an array whose elements are so-qualified
|
| 4251 |
[[dcl.array]]. — *end note*]
|
| 4252 |
|
| 4253 |
[*Example 1*:
|
|
|
|
| 4262 |
The type of both `arr1` and `arr2` is “array of 5 `const char`”, and the
|
| 4263 |
array type is considered to be const-qualified.
|
| 4264 |
|
| 4265 |
— *end example*]
|
| 4266 |
|
| 4267 |
+
[*Note 3*: See [[dcl.fct]] and [[over.match.funcs]] regarding
|
| 4268 |
+
function types that have *cv-qualifier*s. — *end note*]
|
| 4269 |
|
| 4270 |
There is a partial ordering on cv-qualifiers, so that a type can be said
|
| 4271 |
to be *more cv-qualified* than another. [[basic.type.qualifier.rel]]
|
| 4272 |
shows the relations that constitute this ordering.
|
| 4273 |
|
|
|
|
| 4293 |
`volatile int * const` has the top-level cv-qualifier `const`. For a
|
| 4294 |
class type `C`, the type corresponding to the *type-id*
|
| 4295 |
`void (C::* volatile)(int) const` has the top-level cv-qualifier
|
| 4296 |
`volatile`. — *end example*]
|
| 4297 |
|
| 4298 |
+
### Conversion ranks <a id="conv.rank">[[conv.rank]]</a>
|
| 4299 |
|
| 4300 |
Every integer type has an *integer conversion rank* defined as follows:
|
| 4301 |
|
| 4302 |
- No two signed integer types other than `char` and `signed
|
| 4303 |
+
char` (if `char` is signed) have the same rank, even if they have the
|
| 4304 |
+
same representation.
|
| 4305 |
+
- The rank of a signed integer type is greater than the rank of any
|
| 4306 |
+
signed integer type with a smaller width.
|
| 4307 |
+
- The rank of `long long int` is greater than the rank of `long int`,
|
| 4308 |
+
which is greater than the rank of `int`, which is greater than the
|
| 4309 |
+
rank of `short int`, which is greater than the rank of `signed char`.
|
| 4310 |
+
- The rank of any unsigned integer type equals the rank of the
|
|
|
|
| 4311 |
corresponding signed integer type.
|
| 4312 |
+
- The rank of any standard integer type is greater than the rank of any
|
| 4313 |
+
extended integer type with the same width.
|
| 4314 |
+
- The rank of `char` equals the rank of `signed char` and
|
| 4315 |
`unsigned char`.
|
| 4316 |
+
- The rank of `bool` is less than the rank of all standard integer
|
| 4317 |
+
types.
|
| 4318 |
+
- The ranks of `char8_t`, `char16_t`, `char32_t`, and `wchar_t` equal
|
| 4319 |
+
the ranks of their underlying types [[basic.fundamental]].
|
| 4320 |
- The rank of any extended signed integer type relative to another
|
| 4321 |
extended signed integer type with the same width is
|
| 4322 |
*implementation-defined*, but still subject to the other rules for
|
| 4323 |
determining the integer conversion rank.
|
| 4324 |
- For all integer types `T1`, `T2`, and `T3`, if `T1` has greater rank
|
| 4325 |
+
than `T2` and `T2` has greater rank than `T3`, then `T1` has greater
|
| 4326 |
+
rank than `T3`.
|
| 4327 |
|
| 4328 |
[*Note 1*: The integer conversion rank is used in the definition of the
|
| 4329 |
integral promotions [[conv.prom]] and the usual arithmetic conversions
|
| 4330 |
[[expr.arith.conv]]. — *end note*]
|
| 4331 |
|
| 4332 |
+
Every floating-point type has a *floating-point conversion rank* defined
|
| 4333 |
+
as follows:
|
| 4334 |
+
|
| 4335 |
+
- The rank of a floating point type `T` is greater than the rank of any
|
| 4336 |
+
floating-point type whose set of values is a proper subset of the set
|
| 4337 |
+
of values of `T`.
|
| 4338 |
+
- The rank of `long double` is greater than the rank of `double`, which
|
| 4339 |
+
is greater than the rank of `float`.
|
| 4340 |
+
- Two extended floating-point types with the same set of values have
|
| 4341 |
+
equal ranks.
|
| 4342 |
+
- An extended floating-point type with the same set of values as exactly
|
| 4343 |
+
one cv-unqualified standard floating-point type has a rank equal to
|
| 4344 |
+
the rank of that standard floating-point type.
|
| 4345 |
+
- An extended floating-point type with the same set of values as more
|
| 4346 |
+
than one cv-unqualified standard floating-point type has a rank equal
|
| 4347 |
+
to the rank of `double`.
|
| 4348 |
+
|
| 4349 |
+
[*Note 2*: The conversion ranks of floating-point types `T1` and `T2`
|
| 4350 |
+
are unordered if the set of values of `T1` is neither a subset nor a
|
| 4351 |
+
superset of the set of values of `T2`. This can happen when one type has
|
| 4352 |
+
both a larger range and a lower precision than the other. — *end note*]
|
| 4353 |
+
|
| 4354 |
+
Floating-point types that have equal floating-point conversion ranks are
|
| 4355 |
+
ordered by floating-point conversion subrank. The subrank forms a total
|
| 4356 |
+
order among types with equal ranks. The types `std::float16_t`,
|
| 4357 |
+
`std::float32_t`, `std::float64_t`, and `std::float128_t`
|
| 4358 |
+
[[stdfloat.syn]] have a greater conversion subrank than any standard
|
| 4359 |
+
floating-point type with equal conversion rank. Otherwise, the
|
| 4360 |
+
conversion subrank order is *implementation-defined*.
|
| 4361 |
+
|
| 4362 |
+
[*Note 3*: The floating-point conversion rank and subrank are used in
|
| 4363 |
+
the definition of the usual arithmetic conversions
|
| 4364 |
+
[[expr.arith.conv]]. — *end note*]
|
| 4365 |
+
|
| 4366 |
## Program execution <a id="basic.exec">[[basic.exec]]</a>
|
| 4367 |
|
| 4368 |
### Sequential execution <a id="intro.execution">[[intro.execution]]</a>
|
| 4369 |
|
| 4370 |
An instance of each object with automatic storage duration
|
|
|
|
| 4374 |
suspension of a coroutine [[expr.await]], or receipt of a signal).
|
| 4375 |
|
| 4376 |
A *constituent expression* is defined as follows:
|
| 4377 |
|
| 4378 |
- The constituent expression of an expression is that expression.
|
| 4379 |
+
- The constituent expression of a conversion is the corresponding
|
| 4380 |
+
implicit function call, if any, or the converted expression otherwise.
|
| 4381 |
- The constituent expressions of a *braced-init-list* or of a (possibly
|
| 4382 |
parenthesized) *expression-list* are the constituent expressions of
|
| 4383 |
the elements of the respective list.
|
| 4384 |
- The constituent expressions of a *brace-or-equal-initializer* of the
|
| 4385 |
form `=` *initializer-clause* are the constituent expressions of the
|
|
|
|
| 4417 |
|
| 4418 |
[*Note 1*: Expressions appearing in the *compound-statement* of a
|
| 4419 |
*lambda-expression* are not subexpressions of the
|
| 4420 |
*lambda-expression*. — *end note*]
|
| 4421 |
|
| 4422 |
+
The *potentially-evaluated subexpressions* of an expression, conversion,
|
| 4423 |
+
or *initializer* E are
|
| 4424 |
+
|
| 4425 |
+
- the constituent expressions of E and
|
| 4426 |
+
- the subexpressions thereof that are not subexpressions of a nested
|
| 4427 |
+
unevaluated operand [[term.unevaluated.operand]].
|
| 4428 |
+
|
| 4429 |
A *full-expression* is
|
| 4430 |
|
| 4431 |
+
- an unevaluated operand [[expr.context]],
|
| 4432 |
- a *constant-expression* [[expr.const]],
|
| 4433 |
- an immediate invocation [[expr.const]],
|
| 4434 |
- an *init-declarator* [[dcl.decl]] or a *mem-initializer*
|
| 4435 |
[[class.base.init]], including the constituent expressions of the
|
| 4436 |
initializer,
|
|
|
|
| 4514 |
Evaluations *A* and *B* are *indeterminately sequenced* when either *A*
|
| 4515 |
is sequenced before *B* or *B* is sequenced before *A*, but it is
|
| 4516 |
unspecified which.
|
| 4517 |
|
| 4518 |
[*Note 4*: Indeterminately sequenced evaluations cannot overlap, but
|
| 4519 |
+
either can be executed first. — *end note*]
|
| 4520 |
|
| 4521 |
An expression *X* is said to be sequenced before an expression *Y* if
|
| 4522 |
every value computation and every side effect associated with the
|
| 4523 |
expression *X* is sequenced before every value computation and every
|
| 4524 |
side effect associated with the expression *Y*.
|
| 4525 |
|
| 4526 |
Every value computation and side effect associated with a
|
| 4527 |
full-expression is sequenced before every value computation and side
|
| 4528 |
+
effect associated with the next full-expression to be evaluated.[^22]
|
| 4529 |
|
| 4530 |
Except where noted, evaluations of operands of individual operators and
|
| 4531 |
of subexpressions of individual expressions are unsequenced.
|
| 4532 |
|
| 4533 |
[*Note 5*: In an expression that is evaluated more than once during the
|
|
|
|
| 4558 |
}
|
| 4559 |
```
|
| 4560 |
|
| 4561 |
— *end example*]
|
| 4562 |
|
| 4563 |
+
When invoking a function (whether or not the function is inline), every
|
| 4564 |
+
argument expression and the postfix expression designating the called
|
| 4565 |
+
function are sequenced before every expression or statement in the body
|
| 4566 |
+
of the called function. For each function invocation or evaluation of an
|
| 4567 |
+
*await-expression* *F*, each evaluation that does not occur within *F*
|
| 4568 |
+
but is evaluated on the same thread and as part of the same signal
|
| 4569 |
+
handler (if any) is either sequenced before all evaluations that occur
|
| 4570 |
+
within *F* or sequenced after all evaluations that occur within
|
| 4571 |
+
*F*;[^23]
|
| 4572 |
|
| 4573 |
+
if *F* invokes or resumes a coroutine [[expr.await]], only evaluations
|
| 4574 |
+
subsequent to the previous suspension (if any) and prior to the next
|
| 4575 |
+
suspension (if any) are considered to occur within *F*.
|
| 4576 |
|
| 4577 |
Several contexts in C++ cause evaluation of a function call, even though
|
| 4578 |
no corresponding function call syntax appears in the translation unit.
|
| 4579 |
|
| 4580 |
[*Example 4*: Evaluation of a *new-expression* invokes one or more
|
|
|
|
| 4583 |
arise in contexts in which no function call syntax
|
| 4584 |
appears. — *end example*]
|
| 4585 |
|
| 4586 |
The sequencing constraints on the execution of the called function (as
|
| 4587 |
described above) are features of the function calls as evaluated,
|
| 4588 |
+
regardless of the syntax of the expression that calls the function.
|
| 4589 |
|
| 4590 |
If a signal handler is executed as a result of a call to the
|
| 4591 |
`std::raise` function, then the execution of the handler is sequenced
|
| 4592 |
after the invocation of the `std::raise` function and before its return.
|
| 4593 |
|
| 4594 |
+
[*Note 7*: When a signal is received for another reason, the execution
|
| 4595 |
of the signal handler is usually unsequenced with respect to the rest of
|
| 4596 |
the program. — *end note*]
|
| 4597 |
|
| 4598 |
### Multi-threaded executions and data races <a id="intro.multithread">[[intro.multithread]]</a>
|
| 4599 |
|
| 4600 |
+
#### General <a id="intro.multithread.general">[[intro.multithread.general]]</a>
|
| 4601 |
+
|
| 4602 |
A *thread of execution* (also known as a *thread*) is a single flow of
|
| 4603 |
control within a program, including the initial invocation of a specific
|
| 4604 |
top-level function, and recursively including every function invocation
|
| 4605 |
subsequently executed by the thread.
|
| 4606 |
|
| 4607 |
[*Note 1*: When one thread creates another, the initial call to the
|
| 4608 |
top-level function of the new thread is executed by the new thread, not
|
| 4609 |
by the creating thread. — *end note*]
|
| 4610 |
|
| 4611 |
Every thread in a program can potentially access every object and
|
| 4612 |
+
function in a program.[^24]
|
| 4613 |
+
|
| 4614 |
+
Under a hosted implementation, a C++ program can have more than one
|
| 4615 |
+
thread running concurrently. The execution of each thread proceeds as
|
| 4616 |
+
defined by the remainder of this document. The execution of the entire
|
| 4617 |
+
program consists of an execution of all of its threads.
|
| 4618 |
|
| 4619 |
[*Note 2*: Usually the execution can be viewed as an interleaving of
|
| 4620 |
all its threads. However, some kinds of atomic operations, for example,
|
| 4621 |
allow executions inconsistent with a simple interleaving, as described
|
| 4622 |
below. — *end note*]
|
|
|
|
| 4633 |
The value of an object visible to a thread T at a particular point is
|
| 4634 |
the initial value of the object, a value assigned to the object by T, or
|
| 4635 |
a value assigned to the object by another thread, according to the rules
|
| 4636 |
below.
|
| 4637 |
|
| 4638 |
+
[*Note 1*: In some cases, there might instead be undefined behavior.
|
| 4639 |
+
Much of this subclause is motivated by the desire to support atomic
|
| 4640 |
operations with explicit and detailed visibility constraints. However,
|
| 4641 |
it also implicitly supports a simpler view for more restricted
|
| 4642 |
programs. — *end note*]
|
| 4643 |
|
| 4644 |
Two expression evaluations *conflict* if one of them modifies a memory
|
|
|
|
| 4671 |
particular total order, called the *modification order* of M.
|
| 4672 |
|
| 4673 |
[*Note 3*: There is a separate order for each atomic object. There is
|
| 4674 |
no requirement that these can be combined into a single total order for
|
| 4675 |
all objects. In general this will be impossible since different threads
|
| 4676 |
+
can observe modifications to different objects in inconsistent
|
| 4677 |
orders. — *end note*]
|
| 4678 |
|
| 4679 |
A *release sequence* headed by a release operation A on an atomic object
|
| 4680 |
M is a maximal contiguous sub-sequence of side effects in the
|
| 4681 |
modification order of M, where the first operation is A, and every
|
|
|
|
| 4884 |
object in that interleaving. This is normally referred to as “sequential
|
| 4885 |
consistency”. However, this applies only to data-race-free programs, and
|
| 4886 |
data-race-free programs cannot observe most program transformations that
|
| 4887 |
do not change single-threaded program semantics. In fact, most
|
| 4888 |
single-threaded program transformations continue to be allowed, since
|
| 4889 |
+
any program that behaves differently as a result has undefined
|
| 4890 |
+
behavior. — *end note*]
|
| 4891 |
|
| 4892 |
Two accesses to the same object of type `volatile std::sig_atomic_t` do
|
| 4893 |
not result in a data race if both occur in the same thread, even if one
|
| 4894 |
or more occurs in a signal handler. For each signal handler invocation,
|
| 4895 |
evaluations performed by the thread invoking a signal handler can be
|
|
|
|
| 4905 |
abstract machine are generally precluded by this document, since such an
|
| 4906 |
assignment might overwrite another assignment by a different thread in
|
| 4907 |
cases in which an abstract machine execution would not have encountered
|
| 4908 |
a data race. This includes implementations of data member assignment
|
| 4909 |
that overwrite adjacent members in separate memory locations. Reordering
|
| 4910 |
+
of atomic loads in cases in which the atomics in question might alias is
|
| 4911 |
+
also generally precluded, since this could violate the coherence
|
| 4912 |
rules. — *end note*]
|
| 4913 |
|
| 4914 |
[*Note 23*: Transformations that introduce a speculative read of a
|
| 4915 |
+
potentially shared memory location might not preserve the semantics of
|
| 4916 |
+
the C++ program as defined in this document, since they potentially
|
| 4917 |
introduce a data race. However, they are typically valid in the context
|
| 4918 |
of an optimizing compiler that targets a specific machine with
|
| 4919 |
well-defined semantics for data races. They would be invalid for a
|
| 4920 |
hypothetical machine that is not tolerant of races or provides hardware
|
| 4921 |
race detection. — *end note*]
|
|
|
|
| 4938 |
[[atomics.flag]] or indicated as lock-free [[atomics.lockfree]] are
|
| 4939 |
*lock-free executions*.
|
| 4940 |
|
| 4941 |
- If there is only one thread that is not blocked [[defns.block]] in a
|
| 4942 |
standard library function, a lock-free execution in that thread shall
|
| 4943 |
+
complete. \[*Note 2*: Concurrently executing threads might prevent
|
| 4944 |
progress of a lock-free execution. For example, this situation can
|
| 4945 |
occur with load-locked store-conditional implementations. This
|
| 4946 |
property is sometimes termed obstruction-free. — *end note*]
|
| 4947 |
- When one or more lock-free executions run concurrently, at least one
|
| 4948 |
should complete. \[*Note 3*: It is difficult for some implementations
|
| 4949 |
to provide absolute guarantees to this effect, since repeated and
|
| 4950 |
+
particularly inopportune interference from other threads could prevent
|
| 4951 |
forward progress, e.g., by repeatedly stealing a cache line for
|
| 4952 |
unrelated purposes between load-locked and store-conditional
|
| 4953 |
+
instructions. For implementations that follow this recommendation and
|
| 4954 |
+
ensure that such effects cannot indefinitely delay progress under
|
| 4955 |
+
expected operating conditions, such anomalies can therefore safely be
|
| 4956 |
+
ignored by programmers. Outside this document, this property is
|
| 4957 |
+
sometimes termed lock-free. — *end note*]
|
| 4958 |
|
| 4959 |
During the execution of a thread of execution, each of the following is
|
| 4960 |
termed an *execution step*:
|
| 4961 |
|
| 4962 |
- termination of the thread of execution,
|
|
|
|
| 4968 |
is considered to continuously execute execution steps while waiting for
|
| 4969 |
the condition that it blocks on to be satisfied.
|
| 4970 |
|
| 4971 |
[*Example 1*: A library I/O function that blocks until the I/O
|
| 4972 |
operation is complete can be considered to continuously check whether
|
| 4973 |
+
the operation is complete. Each such check consists of one or more
|
| 4974 |
execution steps, for example using observable behavior of the abstract
|
| 4975 |
machine. — *end example*]
|
| 4976 |
|
| 4977 |
[*Note 4*: Because of this and the preceding requirement regarding what
|
| 4978 |
threads of execution have to perform eventually, it follows that no
|
|
|
|
| 4987 |
For a thread of execution providing *concurrent forward progress
|
| 4988 |
guarantees*, the implementation ensures that the thread will eventually
|
| 4989 |
make progress for as long as it has not terminated.
|
| 4990 |
|
| 4991 |
[*Note 5*: This is required regardless of whether or not other threads
|
| 4992 |
+
of execution (if any) have been or are making progress. To eventually
|
| 4993 |
fulfill this requirement means that this will happen in an unspecified
|
| 4994 |
but finite amount of time. — *end note*]
|
| 4995 |
|
| 4996 |
It is *implementation-defined* whether the implementation-created thread
|
| 4997 |
of execution that executes `main` [[basic.start.main]] and the threads
|
| 4998 |
of execution created by `std::thread` [[thread.thread.class]] or
|
| 4999 |
`std::jthread` [[thread.jthread.class]] provide concurrent forward
|
| 5000 |
+
progress guarantees. General-purpose implementations should provide
|
| 5001 |
+
these guarantees.
|
|
|
|
|
|
|
| 5002 |
|
| 5003 |
For a thread of execution providing *parallel forward progress
|
| 5004 |
guarantees*, the implementation is not required to ensure that the
|
| 5005 |
thread will eventually make progress if it has not yet executed any
|
| 5006 |
execution step; once this thread has executed a step, it provides
|
| 5007 |
concurrent forward progress guarantees.
|
| 5008 |
|
| 5009 |
+
[*Note 6*: This does not specify a requirement for when to start this
|
| 5010 |
thread of execution, which will typically be specified by the entity
|
| 5011 |
that creates this thread of execution. For example, a thread of
|
| 5012 |
execution that provides concurrent forward progress guarantees and
|
| 5013 |
executes tasks from a set of tasks in an arbitrary order, one after the
|
| 5014 |
other, satisfies the requirements of parallel forward progress for these
|
|
|
|
| 5016 |
|
| 5017 |
For a thread of execution providing *weakly parallel forward progress
|
| 5018 |
guarantees*, the implementation does not ensure that the thread will
|
| 5019 |
eventually make progress.
|
| 5020 |
|
| 5021 |
+
[*Note 7*: Threads of execution providing weakly parallel forward
|
| 5022 |
progress guarantees cannot be expected to make progress regardless of
|
| 5023 |
whether other threads make progress or not; however, blocking with
|
| 5024 |
forward progress guarantee delegation, as defined below, can be used to
|
| 5025 |
ensure that such threads of execution make progress
|
| 5026 |
eventually. — *end note*]
|
| 5027 |
|
| 5028 |
Concurrent forward progress guarantees are stronger than parallel
|
| 5029 |
forward progress guarantees, which in turn are stronger than weakly
|
| 5030 |
parallel forward progress guarantees.
|
| 5031 |
|
| 5032 |
+
[*Note 8*: For example, some kinds of synchronization between threads
|
| 5033 |
+
of execution might only make progress if the respective threads of
|
| 5034 |
execution provide parallel forward progress guarantees, but will fail to
|
| 5035 |
make progress under weakly parallel guarantees. — *end note*]
|
| 5036 |
|
| 5037 |
When a thread of execution P is specified to *block with forward
|
| 5038 |
progress guarantee delegation* on the completion of a set S of threads
|
| 5039 |
of execution, then throughout the whole time of P being blocked on S,
|
| 5040 |
the implementation shall ensure that the forward progress guarantees
|
| 5041 |
provided by at least one thread of execution in S is at least as strong
|
| 5042 |
as P’s forward progress guarantees.
|
| 5043 |
|
| 5044 |
+
[*Note 9*: It is unspecified which thread or threads of execution in S
|
| 5045 |
are chosen and for which number of execution steps. The strengthening is
|
| 5046 |
not permanent and not necessarily in place for the rest of the lifetime
|
| 5047 |
of the affected thread of execution. As long as P is blocked, the
|
| 5048 |
implementation has to eventually select and potentially strengthen a
|
| 5049 |
thread of execution in S. — *end note*]
|
| 5050 |
|
| 5051 |
Once a thread of execution in S terminates, it is removed from S. Once S
|
| 5052 |
is empty, P is unblocked.
|
| 5053 |
|
| 5054 |
+
[*Note 10*: A thread of execution B thus can temporarily provide an
|
| 5055 |
effectively stronger forward progress guarantee for a certain amount of
|
| 5056 |
time, due to a second thread of execution A being blocked on it with
|
| 5057 |
forward progress guarantee delegation. In turn, if B then blocks with
|
| 5058 |
+
forward progress guarantee delegation on C, this can also temporarily
|
| 5059 |
provide a stronger forward progress guarantee to C. — *end note*]
|
| 5060 |
|
| 5061 |
+
[*Note 11*: If all threads of execution in S finish executing (e.g.,
|
| 5062 |
they terminate and do not use blocking synchronization incorrectly),
|
| 5063 |
then P’s execution of the operation that blocks with forward progress
|
| 5064 |
guarantee delegation will not result in P’s progress guarantee being
|
| 5065 |
effectively weakened. — *end note*]
|
| 5066 |
|
| 5067 |
+
[*Note 12*: This does not remove any constraints regarding blocking
|
| 5068 |
synchronization for threads of execution providing parallel or weakly
|
| 5069 |
parallel forward progress guarantees because the implementation is not
|
| 5070 |
required to strengthen a particular thread of execution whose too-weak
|
| 5071 |
progress guarantee is preventing overall progress. — *end note*]
|
| 5072 |
|
|
|
|
| 5076 |
|
| 5077 |
### Start and termination <a id="basic.start">[[basic.start]]</a>
|
| 5078 |
|
| 5079 |
#### `main` function <a id="basic.start.main">[[basic.start.main]]</a>
|
| 5080 |
|
| 5081 |
+
A program shall contain exactly one function called `main` that belongs
|
| 5082 |
+
to the global scope. Executing a program starts a main thread of
|
| 5083 |
+
execution [[intro.multithread]], [[thread.threads]] in which the `main`
|
| 5084 |
+
function is invoked. It is *implementation-defined* whether a program in
|
| 5085 |
+
a freestanding environment is required to define a `main` function.
|
|
|
|
|
|
|
| 5086 |
|
| 5087 |
[*Note 1*: In a freestanding environment, startup and termination is
|
| 5088 |
*implementation-defined*; startup contains the execution of constructors
|
| 5089 |
+
for non-local objects with static storage duration; termination contains
|
| 5090 |
+
the execution of destructors for objects with static storage
|
| 5091 |
duration. — *end note*]
|
| 5092 |
|
| 5093 |
+
An implementation shall not predefine the `main` function. Its type
|
| 5094 |
+
shall have C++ language linkage and it shall have a declared return type
|
| 5095 |
+
of type `int`, but otherwise its type is *implementation-defined*. An
|
| 5096 |
+
implementation shall allow both
|
| 5097 |
|
| 5098 |
- a function of `()` returning `int` and
|
| 5099 |
- a function of `(int`, pointer to pointer to `char)` returning `int`
|
| 5100 |
|
| 5101 |
as the type of `main` [[dcl.fct]]. In the latter form, for purposes of
|
|
|
|
| 5108 |
[[multibyte.strings]] and `argv[0]` shall be the pointer to the initial
|
| 5109 |
character of a NTMBS that represents the name used to invoke the program
|
| 5110 |
or `""`. The value of `argc` shall be non-negative. The value of
|
| 5111 |
`argv[argc]` shall be 0.
|
| 5112 |
|
| 5113 |
+
*Recommended practice:* Any further (optional) parameters should be
|
| 5114 |
+
added after `argv`.
|
| 5115 |
|
| 5116 |
The function `main` shall not be used within a program. The linkage
|
| 5117 |
[[basic.link]] of `main` is *implementation-defined*. A program that
|
| 5118 |
defines `main` as deleted or that declares `main` to be `inline`,
|
| 5119 |
+
`static`, `constexpr`, or `consteval` is ill-formed. The function `main`
|
| 5120 |
+
shall not be a coroutine [[dcl.fct.def.coroutine]]. The `main` function
|
| 5121 |
+
shall not be declared with a *linkage-specification* [[dcl.link]]. A
|
| 5122 |
+
program that declares
|
| 5123 |
+
|
| 5124 |
+
- a variable `main` that belongs to the global scope, or
|
| 5125 |
+
- a function `main` that belongs to the global scope and is attached to
|
| 5126 |
+
a named module, or
|
| 5127 |
+
- a function template `main` that belongs to the global scope, or
|
| 5128 |
+
- an entity named `main` with C language linkage (in any namespace)
|
| 5129 |
+
|
| 5130 |
+
is ill-formed. The name `main` is not otherwise reserved.
|
| 5131 |
|
| 5132 |
[*Example 1*: Member functions, classes, and enumerations can be called
|
| 5133 |
`main`, as can entities in other namespaces. — *end example*]
|
| 5134 |
|
| 5135 |
Terminating the program without leaving the current block (e.g., by
|
| 5136 |
calling the function `std::exit(int)` [[support.start.term]]) does not
|
| 5137 |
destroy any objects with automatic storage duration [[class.dtor]]. If
|
| 5138 |
+
`std::exit` is invoked during the destruction of an object with static
|
| 5139 |
+
or thread storage duration, the program has undefined behavior.
|
|
|
|
| 5140 |
|
| 5141 |
A `return` statement [[stmt.return]] in `main` has the effect of leaving
|
| 5142 |
the main function (destroying any objects with automatic storage
|
| 5143 |
duration) and calling `std::exit` with the return value as the argument.
|
| 5144 |
If control flows off the end of the *compound-statement* of `main`, the
|
|
|
|
| 5156 |
with static or thread storage duration is constant-initialized
|
| 5157 |
[[expr.const]]. If constant initialization is not performed, a variable
|
| 5158 |
with static storage duration [[basic.stc.static]] or thread storage
|
| 5159 |
duration [[basic.stc.thread]] is zero-initialized [[dcl.init]].
|
| 5160 |
Together, zero-initialization and constant initialization are called
|
| 5161 |
+
*static initialization*; all other initialization is
|
| 5162 |
+
*dynamic initialization*. All static initialization strongly happens
|
| 5163 |
+
before [[intro.races]] any dynamic initialization.
|
| 5164 |
|
| 5165 |
+
[*Note 1*: The dynamic initialization of non-block variables is
|
| 5166 |
+
described in [[basic.start.dynamic]]; that of static block variables is
|
| 5167 |
described in [[stmt.dcl]]. — *end note*]
|
| 5168 |
|
| 5169 |
An implementation is permitted to perform the initialization of a
|
| 5170 |
variable with static or thread storage duration as a static
|
| 5171 |
initialization even if such initialization is not required to be done
|
|
|
|
| 5180 |
statically were initialized dynamically.
|
| 5181 |
|
| 5182 |
[*Note 2*:
|
| 5183 |
|
| 5184 |
As a consequence, if the initialization of an object `obj1` refers to an
|
| 5185 |
+
object `obj2` potentially requiring dynamic initialization and defined
|
| 5186 |
+
later in the same translation unit, it is unspecified whether the value
|
| 5187 |
+
of `obj2` used will be the value of the fully initialized `obj2`
|
| 5188 |
+
(because `obj2` was statically initialized) or will be the value of
|
| 5189 |
+
`obj2` merely zero-initialized. For example,
|
| 5190 |
|
| 5191 |
``` cpp
|
| 5192 |
inline double fd() { return 1.0; }
|
| 5193 |
extern double d1;
|
| 5194 |
double d2 = d1; // unspecified:
|
| 5195 |
+
// either statically initialized to 0.0 or
|
| 5196 |
// dynamically initialized to 0.0 if d1 is
|
| 5197 |
// dynamically initialized, or 1.0 otherwise
|
| 5198 |
+
double d1 = fd(); // either initialized statically or dynamically to 1.0
|
| 5199 |
```
|
| 5200 |
|
| 5201 |
— *end note*]
|
| 5202 |
|
| 5203 |
+
#### Dynamic initialization of non-block variables <a id="basic.start.dynamic">[[basic.start.dynamic]]</a>
|
| 5204 |
|
| 5205 |
+
Dynamic initialization of a non-block variable with static storage
|
| 5206 |
duration is unordered if the variable is an implicitly or explicitly
|
| 5207 |
instantiated specialization, is partially-ordered if the variable is an
|
| 5208 |
inline variable that is not an implicitly or explicitly instantiated
|
| 5209 |
specialization, and otherwise is ordered.
|
| 5210 |
|
| 5211 |
+
[*Note 1*: A non-inline explicit specialization of a templated variable
|
| 5212 |
+
has ordered initialization. — *end note*]
|
|
|
|
| 5213 |
|
| 5214 |
A declaration `D` is *appearance-ordered* before a declaration `E` if
|
| 5215 |
|
| 5216 |
- `D` appears in the same translation unit as `E`, or
|
| 5217 |
- the translation unit containing `E` has an interface dependency on the
|
| 5218 |
translation unit containing `D`,
|
| 5219 |
|
| 5220 |
in either case prior to `E`.
|
| 5221 |
|
| 5222 |
+
Dynamic initialization of non-block variables `V` and `W` with static
|
| 5223 |
storage duration are ordered as follows:
|
| 5224 |
|
| 5225 |
- If `V` and `W` have ordered initialization and the definition of `V`
|
| 5226 |
is appearance-ordered before the definition of `W`, or if `V` has
|
| 5227 |
partially-ordered initialization, `W` does not have unordered
|
|
|
|
| 5243 |
sequenced.
|
| 5244 |
|
| 5245 |
[*Note 2*: This definition permits initialization of a sequence of
|
| 5246 |
ordered variables concurrently with another sequence. — *end note*]
|
| 5247 |
|
| 5248 |
+
A *non-initialization odr-use* is an odr-use [[term.odr.use]] not caused
|
| 5249 |
+
directly or indirectly by the initialization of a non-block static or
|
| 5250 |
+
thread storage duration variable.
|
| 5251 |
|
| 5252 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 5253 |
+
non-block non-inline variable with static storage duration is sequenced
|
| 5254 |
before the first statement of `main` or is deferred. If it is deferred,
|
| 5255 |
it strongly happens before any non-initialization odr-use of any
|
| 5256 |
non-inline function or non-inline variable defined in the same
|
| 5257 |
+
translation unit as the variable to be initialized.[^25]
|
|
|
|
|
|
|
| 5258 |
|
| 5259 |
+
It is *implementation-defined* in which threads and at which points in
|
| 5260 |
+
the program such deferred dynamic initialization occurs.
|
| 5261 |
+
|
| 5262 |
+
*Recommended practice:* An implementation should choose such points in a
|
| 5263 |
+
way that allows the programmer to avoid deadlocks.
|
| 5264 |
|
| 5265 |
[*Example 1*:
|
| 5266 |
|
| 5267 |
``` cpp
|
| 5268 |
// - File 1 -
|
|
|
|
| 5299 |
to its use in `A::A`.
|
| 5300 |
|
| 5301 |
— *end example*]
|
| 5302 |
|
| 5303 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 5304 |
+
non-block inline variable with static storage duration is sequenced
|
| 5305 |
before the first statement of `main` or is deferred. If it is deferred,
|
| 5306 |
it strongly happens before any non-initialization odr-use of that
|
| 5307 |
variable. It is *implementation-defined* in which threads and at which
|
| 5308 |
points in the program such deferred dynamic initialization occurs.
|
| 5309 |
|
| 5310 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 5311 |
+
non-block non-inline variable with thread storage duration is sequenced
|
| 5312 |
before the first statement of the initial function of a thread or is
|
| 5313 |
deferred. If it is deferred, the initialization associated with the
|
| 5314 |
entity for thread *t* is sequenced before the first non-initialization
|
| 5315 |
odr-use by *t* of any non-inline variable with thread storage duration
|
| 5316 |
defined in the same translation unit as the variable to be initialized.
|
| 5317 |
It is *implementation-defined* in which threads and at which points in
|
| 5318 |
the program such deferred dynamic initialization occurs.
|
| 5319 |
|
| 5320 |
+
If the initialization of a non-block variable with static or thread
|
| 5321 |
storage duration exits via an exception, the function `std::terminate`
|
| 5322 |
is called [[except.terminate]].
|
| 5323 |
|
| 5324 |
#### Termination <a id="basic.start.term">[[basic.start.term]]</a>
|
| 5325 |
|
|
|
|
| 5346 |
storage duration is sequenced before that of another, the completion of
|
| 5347 |
the destructor of the second is sequenced before the initiation of the
|
| 5348 |
destructor of the first. If an object is initialized statically, the
|
| 5349 |
object is destroyed in the same order as if the object was dynamically
|
| 5350 |
initialized. For an object of array or class type, all subobjects of
|
| 5351 |
+
that object are destroyed before any block variable with static storage
|
| 5352 |
+
duration initialized during the construction of the subobjects is
|
| 5353 |
+
destroyed. If the destruction of an object with static or thread storage
|
| 5354 |
+
duration exits via an exception, the function `std::terminate` is called
|
| 5355 |
+
[[except.terminate]].
|
| 5356 |
|
| 5357 |
+
If a function contains a block variable of static or thread storage
|
| 5358 |
duration that has been destroyed and the function is called during the
|
| 5359 |
destruction of an object with static or thread storage duration, the
|
| 5360 |
program has undefined behavior if the flow of control passes through the
|
| 5361 |
+
definition of the previously destroyed block variable.
|
| 5362 |
+
|
| 5363 |
+
[*Note 2*: Likewise, the behavior is undefined if the block variable is
|
| 5364 |
+
used indirectly (e.g., through a pointer) after its
|
| 5365 |
+
destruction. — *end note*]
|
| 5366 |
|
| 5367 |
If the completion of the initialization of an object with static storage
|
| 5368 |
duration strongly happens before a call to `std::atexit` (see
|
| 5369 |
`<cstdlib>`, [[support.start.term]]), the call to the function passed to
|
| 5370 |
`std::atexit` is sequenced before the call to the destructor for the
|
|
|
|
| 5381 |
within signal handlers [[support.runtime]] that does not happen before
|
| 5382 |
[[intro.multithread]] completion of destruction of objects with static
|
| 5383 |
storage duration and execution of `std::atexit` registered functions
|
| 5384 |
[[support.start.term]], the program has undefined behavior.
|
| 5385 |
|
| 5386 |
+
[*Note 3*: If there is a use of an object with static storage duration
|
| 5387 |
that does not happen before the object’s destruction, the program has
|
| 5388 |
undefined behavior. Terminating every thread before a call to
|
| 5389 |
`std::exit` or the exit from `main` is sufficient, but not necessary, to
|
| 5390 |
satisfy these requirements. These requirements permit thread managers as
|
| 5391 |
static-storage-duration objects. — *end note*]
|
|
|
|
| 5393 |
Calling the function `std::abort()` declared in `<cstdlib>` terminates
|
| 5394 |
the program without executing any destructors and without calling the
|
| 5395 |
functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
| 5396 |
|
| 5397 |
<!-- Link reference definitions -->
|
| 5398 |
+
[allocator.members]: mem.md#allocator.members
|
| 5399 |
+
[allocator.traits.members]: mem.md#allocator.traits.members
|
| 5400 |
+
[atomics]: thread.md#atomics
|
| 5401 |
+
[atomics.flag]: thread.md#atomics.flag
|
| 5402 |
+
[atomics.lockfree]: thread.md#atomics.lockfree
|
| 5403 |
+
[atomics.order]: thread.md#atomics.order
|
| 5404 |
[bad.alloc]: support.md#bad.alloc
|
| 5405 |
[basic]: #basic
|
| 5406 |
[basic.align]: #basic.align
|
| 5407 |
[basic.compound]: #basic.compound
|
| 5408 |
[basic.def]: #basic.def
|
| 5409 |
[basic.def.odr]: #basic.def.odr
|
| 5410 |
[basic.exec]: #basic.exec
|
| 5411 |
+
[basic.extended.fp]: #basic.extended.fp
|
| 5412 |
[basic.fundamental]: #basic.fundamental
|
| 5413 |
[basic.fundamental.width]: #basic.fundamental.width
|
|
|
|
| 5414 |
[basic.indet]: #basic.indet
|
| 5415 |
[basic.life]: #basic.life
|
| 5416 |
[basic.link]: #basic.link
|
| 5417 |
[basic.lookup]: #basic.lookup
|
| 5418 |
[basic.lookup.argdep]: #basic.lookup.argdep
|
|
|
|
| 5419 |
[basic.lookup.elab]: #basic.lookup.elab
|
| 5420 |
+
[basic.lookup.general]: #basic.lookup.general
|
| 5421 |
[basic.lookup.qual]: #basic.lookup.qual
|
| 5422 |
+
[basic.lookup.qual.general]: #basic.lookup.qual.general
|
| 5423 |
[basic.lookup.udir]: #basic.lookup.udir
|
| 5424 |
[basic.lookup.unqual]: #basic.lookup.unqual
|
| 5425 |
[basic.lval]: expr.md#basic.lval
|
| 5426 |
[basic.memobj]: #basic.memobj
|
| 5427 |
[basic.namespace]: dcl.md#basic.namespace
|
| 5428 |
[basic.pre]: #basic.pre
|
| 5429 |
[basic.scope]: #basic.scope
|
| 5430 |
[basic.scope.block]: #basic.scope.block
|
| 5431 |
[basic.scope.class]: #basic.scope.class
|
|
|
|
| 5432 |
[basic.scope.enum]: #basic.scope.enum
|
| 5433 |
+
[basic.scope.lambda]: #basic.scope.lambda
|
| 5434 |
[basic.scope.namespace]: #basic.scope.namespace
|
| 5435 |
[basic.scope.param]: #basic.scope.param
|
| 5436 |
[basic.scope.pdecl]: #basic.scope.pdecl
|
| 5437 |
+
[basic.scope.scope]: #basic.scope.scope
|
| 5438 |
[basic.scope.temp]: #basic.scope.temp
|
| 5439 |
[basic.start]: #basic.start
|
| 5440 |
[basic.start.dynamic]: #basic.start.dynamic
|
| 5441 |
[basic.start.main]: #basic.start.main
|
| 5442 |
[basic.start.static]: #basic.start.static
|
|
|
|
| 5444 |
[basic.stc]: #basic.stc
|
| 5445 |
[basic.stc.auto]: #basic.stc.auto
|
| 5446 |
[basic.stc.dynamic]: #basic.stc.dynamic
|
| 5447 |
[basic.stc.dynamic.allocation]: #basic.stc.dynamic.allocation
|
| 5448 |
[basic.stc.dynamic.deallocation]: #basic.stc.dynamic.deallocation
|
| 5449 |
+
[basic.stc.dynamic.general]: #basic.stc.dynamic.general
|
| 5450 |
+
[basic.stc.general]: #basic.stc.general
|
| 5451 |
[basic.stc.inherit]: #basic.stc.inherit
|
| 5452 |
[basic.stc.static]: #basic.stc.static
|
| 5453 |
[basic.stc.thread]: #basic.stc.thread
|
| 5454 |
[basic.type.qualifier]: #basic.type.qualifier
|
| 5455 |
[basic.type.qualifier.rel]: #basic.type.qualifier.rel
|
| 5456 |
[basic.types]: #basic.types
|
| 5457 |
+
[basic.types.general]: #basic.types.general
|
| 5458 |
+
[bit.cast]: utilities.md#bit.cast
|
| 5459 |
+
[c.malloc]: mem.md#c.malloc
|
| 5460 |
[class]: class.md#class
|
| 5461 |
[class.abstract]: class.md#class.abstract
|
| 5462 |
[class.access]: class.md#class.access
|
| 5463 |
+
[class.access.base]: class.md#class.access.base
|
| 5464 |
[class.base.init]: class.md#class.base.init
|
| 5465 |
[class.bit]: class.md#class.bit
|
| 5466 |
[class.cdtor]: class.md#class.cdtor
|
| 5467 |
[class.conv.fct]: class.md#class.conv.fct
|
| 5468 |
[class.copy.assign]: class.md#class.copy.assign
|
|
|
|
| 5471 |
[class.default.ctor]: class.md#class.default.ctor
|
| 5472 |
[class.derived]: class.md#class.derived
|
| 5473 |
[class.dtor]: class.md#class.dtor
|
| 5474 |
[class.free]: class.md#class.free
|
| 5475 |
[class.friend]: class.md#class.friend
|
|
|
|
| 5476 |
[class.mem]: class.md#class.mem
|
| 5477 |
+
[class.member.lookup]: #class.member.lookup
|
| 5478 |
[class.mfct]: class.md#class.mfct
|
| 5479 |
+
[class.mfct.non.static]: class.md#class.mfct.non.static
|
| 5480 |
[class.name]: class.md#class.name
|
|
|
|
| 5481 |
[class.pre]: class.md#class.pre
|
| 5482 |
[class.prop]: class.md#class.prop
|
| 5483 |
[class.qual]: #class.qual
|
| 5484 |
[class.spaceship]: class.md#class.spaceship
|
| 5485 |
[class.static]: class.md#class.static
|
| 5486 |
[class.static.data]: class.md#class.static.data
|
| 5487 |
[class.temporary]: #class.temporary
|
|
|
|
| 5488 |
[class.union]: class.md#class.union
|
| 5489 |
+
[class.union.anon]: class.md#class.union.anon
|
| 5490 |
[class.virtual]: class.md#class.virtual
|
| 5491 |
[conv]: expr.md#conv
|
| 5492 |
[conv.array]: expr.md#conv.array
|
| 5493 |
[conv.func]: expr.md#conv.func
|
| 5494 |
[conv.integral]: expr.md#conv.integral
|
|
|
|
| 5516 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 5517 |
[dcl.init]: dcl.md#dcl.init
|
| 5518 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 5519 |
[dcl.init.list]: dcl.md#dcl.init.list
|
| 5520 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
|
|
|
| 5521 |
[dcl.link]: dcl.md#dcl.link
|
| 5522 |
[dcl.meaning]: dcl.md#dcl.meaning
|
| 5523 |
[dcl.mptr]: dcl.md#dcl.mptr
|
| 5524 |
[dcl.name]: dcl.md#dcl.name
|
| 5525 |
[dcl.pre]: dcl.md#dcl.pre
|
|
|
|
| 5527 |
[dcl.ref]: dcl.md#dcl.ref
|
| 5528 |
[dcl.spec]: dcl.md#dcl.spec
|
| 5529 |
[dcl.spec.auto]: dcl.md#dcl.spec.auto
|
| 5530 |
[dcl.stc]: dcl.md#dcl.stc
|
| 5531 |
[dcl.struct.bind]: dcl.md#dcl.struct.bind
|
| 5532 |
+
[dcl.type.decltype]: dcl.md#dcl.type.decltype
|
| 5533 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 5534 |
[dcl.typedef]: dcl.md#dcl.typedef
|
| 5535 |
[defns.block]: intro.md#defns.block
|
|
|
|
|
|
|
| 5536 |
[depr.local]: future.md#depr.local
|
| 5537 |
[depr.static.constexpr]: future.md#depr.static.constexpr
|
| 5538 |
[diff.cpp11.basic]: compatibility.md#diff.cpp11.basic
|
| 5539 |
[enum.udecl]: dcl.md#enum.udecl
|
| 5540 |
[except.handle]: except.md#except.handle
|
| 5541 |
[except.pre]: except.md#except.pre
|
| 5542 |
[except.spec]: except.md#except.spec
|
| 5543 |
[except.terminate]: except.md#except.terminate
|
| 5544 |
[except.throw]: except.md#except.throw
|
|
|
|
| 5545 |
[expr.add]: expr.md#expr.add
|
| 5546 |
[expr.alignof]: expr.md#expr.alignof
|
| 5547 |
[expr.arith.conv]: expr.md#expr.arith.conv
|
| 5548 |
[expr.ass]: expr.md#expr.ass
|
| 5549 |
[expr.await]: expr.md#expr.await
|
| 5550 |
[expr.call]: expr.md#expr.call
|
| 5551 |
[expr.cast]: expr.md#expr.cast
|
| 5552 |
[expr.comma]: expr.md#expr.comma
|
|
|
|
| 5553 |
[expr.cond]: expr.md#expr.cond
|
| 5554 |
[expr.const]: expr.md#expr.const
|
| 5555 |
[expr.const.cast]: expr.md#expr.const.cast
|
| 5556 |
[expr.context]: expr.md#expr.context
|
| 5557 |
[expr.delete]: expr.md#expr.delete
|
|
|
|
| 5561 |
[expr.log.or]: expr.md#expr.log.or
|
| 5562 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 5563 |
[expr.new]: expr.md#expr.new
|
| 5564 |
[expr.pre]: expr.md#expr.pre
|
| 5565 |
[expr.prim.id]: expr.md#expr.prim.id
|
|
|
|
| 5566 |
[expr.prim.id.qual]: expr.md#expr.prim.id.qual
|
| 5567 |
+
[expr.prim.id.unqual]: expr.md#expr.prim.id.unqual
|
| 5568 |
[expr.prim.lambda]: expr.md#expr.prim.lambda
|
| 5569 |
[expr.prim.lambda.capture]: expr.md#expr.prim.lambda.capture
|
| 5570 |
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 5571 |
[expr.prim.this]: expr.md#expr.prim.this
|
| 5572 |
[expr.prop]: expr.md#expr.prop
|
|
|
|
| 5582 |
[get.new.handler]: support.md#get.new.handler
|
| 5583 |
[headers]: library.md#headers
|
| 5584 |
[intro.execution]: #intro.execution
|
| 5585 |
[intro.memory]: #intro.memory
|
| 5586 |
[intro.multithread]: #intro.multithread
|
| 5587 |
+
[intro.multithread.general]: #intro.multithread.general
|
| 5588 |
[intro.object]: #intro.object
|
| 5589 |
[intro.progress]: #intro.progress
|
| 5590 |
[intro.races]: #intro.races
|
| 5591 |
[lex.charset]: lex.md#lex.charset
|
| 5592 |
+
[lex.fcon]: lex.md#lex.fcon
|
| 5593 |
[lex.name]: lex.md#lex.name
|
| 5594 |
[lex.separate]: lex.md#lex.separate
|
|
|
|
|
|
|
| 5595 |
[module.context]: module.md#module.context
|
| 5596 |
[module.global.frag]: module.md#module.global.frag
|
|
|
|
| 5597 |
[module.interface]: module.md#module.interface
|
| 5598 |
[module.reach]: module.md#module.reach
|
| 5599 |
[module.unit]: module.md#module.unit
|
| 5600 |
[multibyte.strings]: library.md#multibyte.strings
|
| 5601 |
[namespace.def]: dcl.md#namespace.def
|
|
|
|
| 5602 |
[namespace.qual]: #namespace.qual
|
| 5603 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 5604 |
[namespace.udir]: dcl.md#namespace.udir
|
| 5605 |
+
[namespace.unnamed]: dcl.md#namespace.unnamed
|
| 5606 |
[new.delete]: support.md#new.delete
|
| 5607 |
[new.delete.array]: support.md#new.delete.array
|
| 5608 |
[new.delete.placement]: support.md#new.delete.placement
|
| 5609 |
[new.delete.single]: support.md#new.delete.single
|
| 5610 |
[new.handler]: support.md#new.handler
|
| 5611 |
+
[new.syn]: support.md#new.syn
|
| 5612 |
+
[obj.lifetime]: mem.md#obj.lifetime
|
| 5613 |
[over]: over.md#over
|
| 5614 |
[over.literal]: over.md#over.literal
|
| 5615 |
[over.match]: over.md#over.match
|
| 5616 |
+
[over.match.funcs]: over.md#over.match.funcs
|
| 5617 |
[over.oper]: over.md#over.oper
|
| 5618 |
[over.over]: over.md#over.over
|
| 5619 |
+
[ptr.align]: mem.md#ptr.align
|
| 5620 |
[ptr.launder]: support.md#ptr.launder
|
| 5621 |
[replacement.functions]: library.md#replacement.functions
|
| 5622 |
[special]: class.md#special
|
| 5623 |
+
[std.modules]: library.md#std.modules
|
| 5624 |
+
[stdfloat.syn]: support.md#stdfloat.syn
|
| 5625 |
[stmt.block]: stmt.md#stmt.block
|
| 5626 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 5627 |
[stmt.expr]: stmt.md#stmt.expr
|
|
|
|
| 5628 |
[stmt.if]: stmt.md#stmt.if
|
| 5629 |
+
[stmt.iter]: stmt.md#stmt.iter
|
| 5630 |
+
[stmt.pre]: stmt.md#stmt.pre
|
| 5631 |
[stmt.ranged]: stmt.md#stmt.ranged
|
| 5632 |
[stmt.return]: stmt.md#stmt.return
|
| 5633 |
+
[stmt.select]: stmt.md#stmt.select
|
| 5634 |
[support.dynamic]: support.md#support.dynamic
|
|
|
|
| 5635 |
[support.runtime]: support.md#support.runtime
|
| 5636 |
[support.start.term]: support.md#support.start.term
|
| 5637 |
[support.types]: support.md#support.types
|
| 5638 |
+
[temp.concept]: temp.md#temp.concept
|
| 5639 |
[temp.deduct.guide]: temp.md#temp.deduct.guide
|
| 5640 |
[temp.dep]: temp.md#temp.dep
|
| 5641 |
[temp.dep.candidate]: temp.md#temp.dep.candidate
|
| 5642 |
+
[temp.dep.constexpr]: temp.md#temp.dep.constexpr
|
| 5643 |
+
[temp.dep.type]: temp.md#temp.dep.type
|
| 5644 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 5645 |
[temp.explicit]: temp.md#temp.explicit
|
| 5646 |
+
[temp.friend]: temp.md#temp.friend
|
| 5647 |
[temp.local]: temp.md#temp.local
|
| 5648 |
[temp.names]: temp.md#temp.names
|
|
|
|
| 5649 |
[temp.over]: temp.md#temp.over
|
| 5650 |
+
[temp.over.link]: temp.md#temp.over.link
|
| 5651 |
[temp.param]: temp.md#temp.param
|
| 5652 |
[temp.point]: temp.md#temp.point
|
| 5653 |
[temp.pre]: temp.md#temp.pre
|
| 5654 |
[temp.res]: temp.md#temp.res
|
| 5655 |
[temp.spec]: temp.md#temp.spec
|
| 5656 |
+
[temp.spec.partial]: temp.md#temp.spec.partial
|
| 5657 |
[temp.type]: temp.md#temp.type
|
| 5658 |
+
[term.incomplete.type]: #term.incomplete.type
|
| 5659 |
+
[term.odr.use]: #term.odr.use
|
| 5660 |
+
[term.unevaluated.operand]: expr.md#term.unevaluated.operand
|
| 5661 |
[thread]: thread.md#thread
|
| 5662 |
[thread.jthread.class]: thread.md#thread.jthread.class
|
| 5663 |
[thread.thread.class]: thread.md#thread.thread.class
|
| 5664 |
[thread.threads]: thread.md#thread.threads
|
|
|
|
| 5665 |
|
| 5666 |
[^1]: Appearing inside the brace-enclosed *declaration-seq* in a
|
| 5667 |
*linkage-specification* does not affect whether a declaration is a
|
| 5668 |
definition.
|
| 5669 |
|
| 5670 |
[^2]: An implementation is not required to call allocation and
|
| 5671 |
deallocation functions from constructors or destructors; however,
|
| 5672 |
this is a permissible implementation technique.
|
| 5673 |
|
| 5674 |
+
[^3]: An implicit object parameter [[over.match.funcs]] is not part of
|
| 5675 |
+
the parameter-type-list.
|
|
|
|
| 5676 |
|
| 5677 |
+
[^4]: Lookups in which function names are ignored include names
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5678 |
appearing in a *nested-name-specifier*, an
|
| 5679 |
*elaborated-type-specifier*, or a *base-specifier*.
|
| 5680 |
|
| 5681 |
+
[^5]: Unicode® is a registered trademark of Unicode, Inc. This
|
| 5682 |
+
information is given for the convenience of users of this document
|
| 5683 |
+
and does not constitute an endorsement by ISO or IEC of this
|
| 5684 |
+
product.
|
| 5685 |
+
|
| 5686 |
+
[^6]: The number of bits in a byte is reported by the macro `CHAR_BIT`
|
| 5687 |
in the header `<climits>`.
|
| 5688 |
|
| 5689 |
+
[^7]: Under the “as-if” rule an implementation is allowed to store two
|
| 5690 |
objects at the same machine address or not store an object at all if
|
| 5691 |
the program cannot observe the difference [[intro.execution]].
|
| 5692 |
|
| 5693 |
+
[^8]: For example, before the dynamic initialization of an object with
|
| 5694 |
+
static storage duration [[basic.start.dynamic]].
|
| 5695 |
|
| 5696 |
+
[^9]: That is, an object for which a destructor will be called
|
| 5697 |
implicitly—upon exit from the block for an object with automatic
|
| 5698 |
storage duration, upon exit from the thread for an object with
|
| 5699 |
thread storage duration, or upon exit from the program for an object
|
| 5700 |
with static storage duration.
|
| 5701 |
|
| 5702 |
+
[^10]: Some implementations might define that copying an invalid pointer
|
| 5703 |
value causes a system-generated runtime fault.
|
| 5704 |
|
| 5705 |
+
[^11]: The intent is to have `operator new()` implementable by calling
|
| 5706 |
`std::malloc()` or `std::calloc()`, so the rules are substantially
|
| 5707 |
the same. C++ differs from C in requiring a zero request to return a
|
| 5708 |
non-null pointer.
|
| 5709 |
|
| 5710 |
+
[^12]: The global `operator delete(void*, std::size_t)` precludes use of
|
| 5711 |
an allocation function `void operator new(std::size_t, std::size_t)`
|
| 5712 |
+
as a placement allocation function [[diff.cpp11.basic]].
|
| 5713 |
|
| 5714 |
+
[^13]: The same rules apply to initialization of an `initializer_list`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5715 |
object [[dcl.init.list]] with its underlying temporary array.
|
| 5716 |
|
| 5717 |
+
[^14]: By using, for example, the library functions [[headers]]
|
| 5718 |
`std::memcpy` or `std::memmove`.
|
| 5719 |
|
| 5720 |
+
[^15]: By using, for example, the library functions [[headers]]
|
| 5721 |
`std::memcpy` or `std::memmove`.
|
| 5722 |
|
| 5723 |
+
[^16]: The intent is that the memory model of C++ is compatible with
|
| 5724 |
that of ISO/IEC 9899 Programming Language C.
|
| 5725 |
|
| 5726 |
+
[^17]: The size and layout of an instance of an incompletely-defined
|
| 5727 |
object type is unknown.
|
| 5728 |
|
| 5729 |
+
[^18]: This is also known as two’s complement representation.
|
| 5730 |
|
| 5731 |
+
[^19]: Static class members are objects or functions, and pointers to
|
| 5732 |
them are ordinary pointers to objects or functions.
|
| 5733 |
|
| 5734 |
+
[^20]: For an object that is not within its lifetime, this is the first
|
| 5735 |
byte in memory that it will occupy or used to occupy.
|
| 5736 |
|
| 5737 |
+
[^21]: The same representation and alignment requirements are meant to
|
| 5738 |
imply interchangeability as arguments to functions, return values
|
| 5739 |
from functions, and non-static data members of unions.
|
| 5740 |
|
| 5741 |
+
[^22]: As specified in [[class.temporary]], after a full-expression is
|
| 5742 |
evaluated, a sequence of zero or more invocations of destructor
|
| 5743 |
functions for temporary objects takes place, usually in reverse
|
| 5744 |
order of the construction of each temporary object.
|
| 5745 |
|
| 5746 |
+
[^23]: In other words, function executions do not interleave with each
|
| 5747 |
other.
|
| 5748 |
|
| 5749 |
+
[^24]: An object with automatic or thread storage duration [[basic.stc]]
|
| 5750 |
is associated with one specific thread, and can be accessed by a
|
| 5751 |
different thread only indirectly through a pointer or reference
|
| 5752 |
[[basic.compound]].
|
| 5753 |
|
| 5754 |
+
[^25]: A non-block variable with static storage duration having
|
| 5755 |
initialization with side effects is initialized in this case, even
|
| 5756 |
+
if it is not itself odr-used [[term.odr.use]], [[basic.stc.static]].
|
|
|