- tmp/tmp5vh7hxhl/{from.md → to.md} +3486 -1738
tmp/tmp5vh7hxhl/{from.md → to.md}
RENAMED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
-
#
|
|
|
|
|
|
|
| 2 |
|
| 3 |
[*Note 1*: This Clause presents the basic concepts of the C++ language.
|
| 4 |
It explains the difference between an object and a name and how they
|
| 5 |
relate to the value categories for expressions. It introduces the
|
| 6 |
concepts of a declaration and a definition and presents C++’s notion of
|
|
@@ -11,92 +13,105 @@ compound types from these. — *end note*]
|
|
| 11 |
|
| 12 |
[*Note 2*: This Clause does not cover concepts that affect only a
|
| 13 |
single part of the language. Such concepts are discussed in the relevant
|
| 14 |
Clauses. — *end note*]
|
| 15 |
|
| 16 |
-
An *entity* is a value, object, reference,
|
| 17 |
-
class member, bit-field, template, template
|
| 18 |
-
|
| 19 |
|
| 20 |
-
A *name* is a use of an *identifier*
|
| 21 |
-
*operator-function-id*
|
| 22 |
-
[[over.literal]]
|
| 23 |
-
*template-id*
|
| 24 |
[[stmt.goto]], [[stmt.label]]).
|
| 25 |
|
| 26 |
Every name that denotes an entity is introduced by a *declaration*.
|
| 27 |
Every name that denotes a label is introduced either by a `goto`
|
| 28 |
-
statement
|
| 29 |
|
| 30 |
A *variable* is introduced by the declaration of a reference other than
|
| 31 |
a non-static data member or of an object. The variable’s name, if any,
|
| 32 |
denotes the reference or object.
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
Some names denote types or templates. In general, whenever a name is
|
| 35 |
encountered it is necessary to determine whether that name denotes one
|
| 36 |
of these entities before continuing to parse the program that contains
|
| 37 |
-
it. The process that determines this is called *name lookup*
|
| 38 |
-
[[basic.lookup]]
|
| 39 |
|
| 40 |
Two names are *the same* if
|
| 41 |
|
| 42 |
- they are *identifier*s composed of the same character sequence, or
|
| 43 |
- they are *operator-function-id*s formed with the same operator, or
|
| 44 |
- they are *conversion-function-id*s formed with the same type, or
|
| 45 |
- they are *template-id*s that refer to the same class, function, or
|
| 46 |
-
variable
|
| 47 |
-
- they are
|
| 48 |
-
|
| 49 |
|
| 50 |
A name used in more than one translation unit can potentially refer to
|
| 51 |
-
the same entity in these translation units depending on the linkage
|
| 52 |
-
[[basic.link]]
|
| 53 |
|
| 54 |
## Declarations and definitions <a id="basic.def">[[basic.def]]</a>
|
| 55 |
|
| 56 |
-
A declaration
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
including:
|
| 61 |
|
| 62 |
-
- a static assertion
|
| 63 |
-
- controlling template instantiation
|
| 64 |
-
- guiding template argument deduction for constructors
|
| 65 |
-
[[temp.deduct.guide]]
|
| 66 |
-
- use of attributes
|
| 67 |
- nothing (in the case of an *empty-declaration*).
|
| 68 |
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
- it declares a function without specifying the function’s body
|
| 72 |
-
[[dcl.fct.def]]
|
| 73 |
-
- it contains the `extern` specifier
|
| 74 |
-
*linkage-specification*[^1]
|
| 75 |
-
|
| 76 |
-
- it declares a non-inline static data member in a class definition
|
| 77 |
-
[[class.mem]],
|
| 78 |
- it declares a static data member outside a class definition and the
|
| 79 |
variable was defined within the class with the `constexpr` specifier
|
| 80 |
-
(this usage is deprecated; see [[depr.
|
| 81 |
-
- it is
|
| 82 |
-
- it is an *opaque-enum-declaration*
|
| 83 |
-
- it is a *template-parameter*
|
| 84 |
-
- it is a *parameter-declaration*
|
| 85 |
-
|
| 86 |
-
- it is a `typedef` declaration
|
| 87 |
-
- it is an *alias-declaration*
|
| 88 |
-
- it is a *using-declaration*
|
| 89 |
-
- it is a *deduction-guide*
|
| 90 |
-
- it is a *static_assert-declaration*
|
| 91 |
-
- it is an *attribute-declaration*
|
| 92 |
-
- it is an *empty-declaration*
|
| 93 |
-
- it is a *using-directive*
|
| 94 |
-
- it is
|
| 95 |
-
- it is
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
*declaration* is not a definition.
|
| 97 |
|
|
|
|
|
|
|
|
|
|
| 98 |
[*Example 1*:
|
| 99 |
|
| 100 |
All but one of the following are definitions:
|
| 101 |
|
| 102 |
``` cpp
|
|
@@ -128,25 +143,25 @@ extern X anotherX; // declares anotherX
|
|
| 128 |
using N::d; // declares d
|
| 129 |
```
|
| 130 |
|
| 131 |
— *end example*]
|
| 132 |
|
| 133 |
-
[*Note 1*: In some circumstances, C++implementations implicitly
|
| 134 |
-
the default constructor
|
| 135 |
-
|
| 136 |
-
operator
|
| 137 |
-
|
| 138 |
|
| 139 |
[*Example 2*:
|
| 140 |
|
| 141 |
Given
|
| 142 |
|
| 143 |
``` cpp
|
| 144 |
#include <string>
|
| 145 |
|
| 146 |
struct C {
|
| 147 |
-
std::string s;
|
| 148 |
};
|
| 149 |
|
| 150 |
int main() {
|
| 151 |
C a;
|
| 152 |
C b = a;
|
|
@@ -172,46 +187,52 @@ struct C {
|
|
| 172 |
```
|
| 173 |
|
| 174 |
— *end example*]
|
| 175 |
|
| 176 |
[*Note 2*: A class name can also be implicitly declared by an
|
| 177 |
-
*elaborated-type-specifier*
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
|
|
|
| 181 |
|
| 182 |
## One-definition rule <a id="basic.def.odr">[[basic.def.odr]]</a>
|
| 183 |
|
| 184 |
No translation unit shall contain more than one definition of any
|
| 185 |
-
variable, function, class type, enumeration type,
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
An expression is *potentially evaluated* unless it is an
|
| 188 |
-
operand
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
- If
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
contains the potential results of
|
| 197 |
-
- If
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
| 204 |
operands.
|
| 205 |
-
- If
|
| 206 |
potential results of the right operand.
|
| 207 |
- Otherwise, the set is empty.
|
| 208 |
|
| 209 |
[*Note 1*:
|
| 210 |
|
| 211 |
This set is a (possibly-empty) set of *id-expression*s, each of which is
|
| 212 |
-
either
|
| 213 |
|
| 214 |
[*Example 1*:
|
| 215 |
|
| 216 |
In the following example, the set of potential results of the
|
| 217 |
initializer of `n` contains the first `S::x` subexpression, but not the
|
|
@@ -226,67 +247,145 @@ int n = b ? (1, S::x) // S::x is not odr-used here
|
|
| 226 |
|
| 227 |
— *end example*]
|
| 228 |
|
| 229 |
— *end note*]
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
A variable `x` whose name appears as a potentially-evaluated expression
|
| 232 |
-
|
| 233 |
-
conversion ([[conv.lval]]) to `x` yields a constant expression (
|
| 234 |
-
[[expr.const]]) that does not invoke any non-trivial functions and, if
|
| 235 |
-
`x` is an object, `ex` is an element of the set of potential results of
|
| 236 |
-
an expression `e`, where either the lvalue-to-rvalue conversion (
|
| 237 |
-
[[conv.lval]]) is applied to `e`, or `e` is a discarded-value
|
| 238 |
-
expression (Clause [[expr]]). `this` is odr-used if it appears as a
|
| 239 |
-
potentially-evaluated expression (including as the result of the
|
| 240 |
-
implicit transformation in the body of a non-static member function (
|
| 241 |
-
[[class.mfct.non-static]])). A virtual member function is odr-used if it
|
| 242 |
-
is not pure. A function whose name appears as a potentially-evaluated
|
| 243 |
-
expression is odr-used if it is the unique lookup result or the selected
|
| 244 |
-
member of a set of overloaded functions ([[basic.lookup]],
|
| 245 |
-
[[over.match]], [[over.over]]), unless it is a pure virtual function and
|
| 246 |
-
either its name is not explicitly qualified or the expression forms a
|
| 247 |
-
pointer to member ([[expr.unary.op]]).
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
non-placement deallocation function for a class is odr-used by the
|
| 265 |
definition of the destructor of that class, or by being selected by the
|
| 266 |
-
lookup at the point of definition of a virtual destructor
|
| 267 |
-
[[class.dtor]]
|
| 268 |
-
|
| 269 |
-
function
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
[[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
Every program shall contain exactly one definition of every non-inline
|
| 275 |
function or variable that is odr-used in that program outside of a
|
| 276 |
-
discarded statement
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
[[class.copy]]).
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
The following complete translation unit is well-formed, even though it
|
| 290 |
never defines `X`:
|
| 291 |
|
| 292 |
``` cpp
|
|
@@ -300,92 +399,97 @@ X* x2; // use X in pointer formation
|
|
| 300 |
[*Note 3*:
|
| 301 |
|
| 302 |
The rules for declarations and expressions describe in which contexts
|
| 303 |
complete class types are required. A class type `T` must be complete if:
|
| 304 |
|
| 305 |
-
- an object of type `T` is defined
|
| 306 |
-
- a non-static class data member of type `T` is declared
|
| 307 |
-
|
| 308 |
- `T` is used as the allocated type or array element type in a
|
| 309 |
-
*new-expression*
|
| 310 |
- an lvalue-to-rvalue conversion is applied to a glvalue referring to an
|
| 311 |
-
object of type `T`
|
| 312 |
- an expression is converted (either implicitly or explicitly) to type
|
| 313 |
-
`T` (
|
| 314 |
[[expr.static.cast]], [[expr.cast]]), or
|
| 315 |
- an expression that is not a null pointer constant, and has type other
|
| 316 |
than cv `void*`, is converted to the type pointer to `T` or reference
|
| 317 |
-
to `T` using a standard conversion
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
- the type `T` is the subject of an `alignof` expression (
|
| 330 |
-
[[expr.alignof]]), or
|
| 331 |
- an *exception-declaration* has type `T`, reference to `T`, or pointer
|
| 332 |
-
to `T`
|
| 333 |
|
| 334 |
— *end note*]
|
| 335 |
|
| 336 |
-
There can be more than one definition of a
|
| 337 |
-
[[class]]), enumeration type ([[dcl.enum]]), inline function with
|
| 338 |
-
external linkage ([[dcl.inline]]), inline variable with external
|
| 339 |
-
linkage ([[dcl.inline]]), class template (Clause [[temp]]), non-static
|
| 340 |
-
function template ([[temp.fct]]), static data member of a class
|
| 341 |
-
template ([[temp.static]]), member function of a class template (
|
| 342 |
-
[[temp.mem.func]]), or template specialization for which some template
|
| 343 |
-
parameters are not specified ([[temp.spec]], [[temp.class.spec]]) in a
|
| 344 |
-
program provided that each definition appears in a different translation
|
| 345 |
-
unit, and provided the definitions satisfy the following requirements.
|
| 346 |
-
Given such an entity named `D` defined in more than one translation
|
| 347 |
-
unit, then
|
| 348 |
|
| 349 |
-
-
|
| 350 |
-
|
| 351 |
-
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
object
|
| 358 |
- has the same literal type in all definitions of `D`,
|
| 359 |
-
- is initialized with a constant expression
|
| 360 |
- is not odr-used in any definition of `D`, and
|
| 361 |
- has the same value in all definitions of `D`,
|
| 362 |
|
| 363 |
or
|
| 364 |
- a reference with internal or no linkage initialized with a constant
|
| 365 |
expression such that the reference refers to the same entity in all
|
| 366 |
-
definitions of `D`
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
|
|
|
| 372 |
implicit calls to conversion functions, constructors, operator new
|
| 373 |
functions and operator delete functions, shall refer to the same
|
| 374 |
-
function
|
| 375 |
-
-
|
| 376 |
-
explicit) function call
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
-
|
| 382 |
-
[[class.ctor]]), it is as if the
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
for a subobject of `D`.
|
| 386 |
-
\[*Example
|
| 387 |
``` cpp
|
| 388 |
// translation unit 1:
|
| 389 |
struct X {
|
| 390 |
X(int, int);
|
| 391 |
X(int, int, int);
|
|
@@ -408,27 +512,82 @@ unit, then
|
|
| 408 |
D d2; // X(int, int, int) called by D();
|
| 409 |
// D()'s implicit definition violates the ODR
|
| 410 |
```
|
| 411 |
|
| 412 |
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
If `D` is a template and is defined in more than one translation unit,
|
| 415 |
then the preceding requirements shall apply both to names from the
|
| 416 |
-
template’s enclosing scope used in the template definition
|
| 417 |
-
[[temp.nondep]]
|
| 418 |
-
instantiation
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
|
| 423 |
## Scope <a id="basic.scope">[[basic.scope]]</a>
|
| 424 |
|
| 425 |
### Declarative regions and scopes <a id="basic.scope.declarative">[[basic.scope.declarative]]</a>
|
| 426 |
|
| 427 |
Every name is introduced in some portion of program text called a
|
| 428 |
*declarative region*, which is the largest part of the program in which
|
| 429 |
-
that name is
|
| 430 |
unqualified name to refer to the same entity. In general, each
|
| 431 |
particular name is valid only within some possibly discontiguous portion
|
| 432 |
of program text called its *scope*. To determine the scope of a
|
| 433 |
declaration, it is sometimes convenient to refer to the *potential
|
| 434 |
scope* of a declaration. The scope of a declaration is the same as its
|
|
@@ -461,62 +620,68 @@ same as its potential scope.
|
|
| 461 |
|
| 462 |
— *end example*]
|
| 463 |
|
| 464 |
The names declared by a declaration are introduced into the scope in
|
| 465 |
which the declaration occurs, except that the presence of a `friend`
|
| 466 |
-
specifier
|
| 467 |
-
*elaborated-type-specifier*
|
| 468 |
-
|
| 469 |
|
| 470 |
Given a set of declarations in a single declarative region, each of
|
| 471 |
which specifies the same unqualified name,
|
| 472 |
|
| 473 |
- they shall all refer to the same entity, or all refer to functions and
|
| 474 |
function templates; or
|
| 475 |
- exactly one declaration shall declare a class name or enumeration name
|
| 476 |
that is not a typedef name and the other declarations shall all refer
|
| 477 |
to the same variable, non-static data member, or enumerator, or all
|
| 478 |
refer to functions and function templates; in this case the class name
|
| 479 |
-
or enumeration name is hidden
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
|
| 484 |
[*Note 2*: These restrictions apply to the declarative region into
|
| 485 |
which a name is introduced, which is not necessarily the same as the
|
| 486 |
region in which the declaration occurs. In particular,
|
| 487 |
-
*elaborated-type-specifier*s
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
|
| 495 |
[*Note 3*: The name lookup rules are summarized in
|
| 496 |
[[basic.lookup]]. — *end note*]
|
| 497 |
|
| 498 |
### Point of declaration <a id="basic.scope.pdecl">[[basic.scope.pdecl]]</a>
|
| 499 |
|
| 500 |
The *point of declaration* for a name is immediately after its complete
|
| 501 |
-
declarator
|
| 502 |
-
|
| 503 |
|
| 504 |
[*Example 1*:
|
| 505 |
|
| 506 |
``` cpp
|
| 507 |
unsigned char x = 12;
|
| 508 |
{ unsigned char x = x; }
|
| 509 |
```
|
| 510 |
|
| 511 |
-
Here the second `x`
|
|
|
|
|
|
|
| 512 |
|
| 513 |
— *end example*]
|
| 514 |
|
| 515 |
[*Note 1*:
|
| 516 |
|
| 517 |
-
|
| 518 |
declaration of the name that hides it.
|
| 519 |
|
| 520 |
[*Example 2*:
|
| 521 |
|
| 522 |
``` cpp
|
|
@@ -530,20 +695,20 @@ declares a block-scope array of two integers.
|
|
| 530 |
|
| 531 |
— *end note*]
|
| 532 |
|
| 533 |
The point of declaration for a class or class template first declared by
|
| 534 |
a *class-specifier* is immediately after the *identifier* or
|
| 535 |
-
*simple-template-id* (if any) in its *class-head*
|
| 536 |
-
|
| 537 |
-
*identifier* (if any) in either its *enum-specifier*
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
|
| 542 |
The point of declaration of a *using-declarator* that does not name a
|
| 543 |
-
constructor is immediately after the *using-declarator*
|
| 544 |
-
[[namespace.udecl]]
|
| 545 |
|
| 546 |
The point of declaration for an enumerator is immediately after its
|
| 547 |
*enumerator-definition*.
|
| 548 |
|
| 549 |
[*Example 3*:
|
|
@@ -598,17 +763,24 @@ The point of declaration of a class first declared in an
|
|
| 598 |
\[*Note 3*: These rules also apply within templates. — *end note*]
|
| 599 |
\[*Note 4*: Other forms of *elaborated-type-specifier* do not declare
|
| 600 |
a new name, and therefore must refer to an existing *type-name*. See
|
| 601 |
[[basic.lookup.elab]] and [[dcl.type.elab]]. — *end note*]
|
| 602 |
|
| 603 |
-
The point of declaration for an
|
| 604 |
-
|
| 605 |
-
definition.
|
| 606 |
|
| 607 |
-
The point of declaration for a function-local predefined variable
|
| 608 |
-
[[dcl.fct.def]]
|
| 609 |
-
definition.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
|
| 611 |
The point of declaration for a template parameter is immediately after
|
| 612 |
its complete *template-parameter*.
|
| 613 |
|
| 614 |
[*Example 4*:
|
|
@@ -623,72 +795,81 @@ template<class T
|
|
| 623 |
|
| 624 |
— *end example*]
|
| 625 |
|
| 626 |
[*Note 5*: Friend declarations refer to functions or classes that are
|
| 627 |
members of the nearest enclosing namespace, but they do not introduce
|
| 628 |
-
new names into that namespace
|
| 629 |
declarations at block scope and variable declarations with the `extern`
|
| 630 |
specifier at block scope refer to declarations that are members of an
|
| 631 |
enclosing namespace, but they do not introduce new names into that
|
| 632 |
scope. — *end note*]
|
| 633 |
|
| 634 |
[*Note 6*: For point of instantiation of a template, see
|
| 635 |
[[temp.point]]. — *end note*]
|
| 636 |
|
| 637 |
### Block scope <a id="basic.scope.block">[[basic.scope.block]]</a>
|
| 638 |
|
| 639 |
-
A name declared in a block
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
The potential scope of a function parameter name (including one
|
| 645 |
-
appearing in a *lambda-declarator*) or of a function-local predefined
|
| 646 |
-
variable in a function definition ([[dcl.fct.def]]) begins at its point
|
| 647 |
-
of declaration. If the function has a *function-try-block* the potential
|
| 648 |
-
scope of a parameter or of a function-local predefined variable ends at
|
| 649 |
-
the end of the last associated handler, otherwise it ends at the end of
|
| 650 |
-
the outermost block of the function definition. A parameter name shall
|
| 651 |
-
not be redeclared in the outermost block of the function definition nor
|
| 652 |
-
in the outermost block of any handler associated with a
|
| 653 |
-
*function-try-block*.
|
| 654 |
|
| 655 |
The name declared in an *exception-declaration* is local to the
|
| 656 |
*handler* and shall not be redeclared in the outermost block of the
|
| 657 |
*handler*.
|
| 658 |
|
| 659 |
Names declared in the *init-statement*, the *for-range-declaration*, and
|
| 660 |
in the *condition* of `if`, `while`, `for`, and `switch` statements are
|
| 661 |
local to the `if`, `while`, `for`, or `switch` statement (including the
|
| 662 |
controlled statement), and shall not be redeclared in a subsequent
|
| 663 |
condition of that statement nor in the outermost block (or, for the `if`
|
| 664 |
-
statement, any of the outermost blocks) of the controlled statement
|
| 665 |
-
see [[stmt.select]].
|
| 666 |
|
| 667 |
-
|
| 668 |
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 673 |
|
| 674 |
### Function scope <a id="basic.funscope">[[basic.funscope]]</a>
|
| 675 |
|
| 676 |
-
Labels
|
| 677 |
-
|
| 678 |
scope.
|
| 679 |
|
| 680 |
### Namespace scope <a id="basic.scope.namespace">[[basic.scope.namespace]]</a>
|
| 681 |
|
| 682 |
The declarative region of a *namespace-definition* is its
|
| 683 |
*namespace-body*. Entities declared in a *namespace-body* are said to be
|
| 684 |
*members* of the namespace, and names introduced by these declarations
|
| 685 |
into the declarative region of the namespace are said to be *member
|
| 686 |
names* of the namespace. A namespace member name has namespace scope.
|
| 687 |
Its potential scope includes its namespace from the name’s point of
|
| 688 |
-
declaration
|
| 689 |
-
*using-directive*
|
| 690 |
namespace, the member’s potential scope includes that portion of the
|
| 691 |
potential scope of the *using-directive* that follows the member’s point
|
| 692 |
of declaration.
|
| 693 |
|
| 694 |
[*Example 1*:
|
|
@@ -718,47 +899,90 @@ namespace N {
|
|
| 718 |
}
|
| 719 |
```
|
| 720 |
|
| 721 |
— *end example*]
|
| 722 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
A namespace member can also be referred to after the `::` scope
|
| 724 |
-
resolution operator
|
| 725 |
namespace or the name of a namespace which nominates the member’s
|
| 726 |
namespace in a *using-directive*; see [[namespace.qual]].
|
| 727 |
|
| 728 |
The outermost declarative region of a translation unit is also a
|
| 729 |
namespace, called the *global namespace*. A name declared in the global
|
| 730 |
namespace has *global namespace scope* (also called *global scope*). The
|
| 731 |
-
potential scope of such a name begins at its point of declaration
|
| 732 |
-
[[basic.scope.pdecl]]
|
| 733 |
is its declarative region. A name with global namespace scope is said to
|
| 734 |
be a *global name*.
|
| 735 |
|
| 736 |
### Class scope <a id="basic.scope.class">[[basic.scope.class]]</a>
|
| 737 |
|
| 738 |
The potential scope of a name declared in a class consists not only of
|
| 739 |
the declarative region following the name’s point of declaration, but
|
| 740 |
-
also of all
|
| 741 |
-
and *brace-or-equal-initializer*s of non-static data members in that
|
| 742 |
-
class (including such things in nested classes).
|
| 743 |
|
| 744 |
A name `N` used in a class `S` shall refer to the same declaration in
|
| 745 |
its context and when re-evaluated in the completed scope of `S`. No
|
| 746 |
diagnostic is required for a violation of this rule.
|
| 747 |
|
| 748 |
A name declared within a member function hides a declaration of the same
|
| 749 |
name whose scope extends to or past the end of the member function’s
|
| 750 |
class.
|
| 751 |
|
| 752 |
-
The potential scope of a declaration that extends to or past
|
| 753 |
-
a class definition also extends to the regions defined by its
|
| 754 |
-
definitions, even if the members are defined lexically outside
|
| 755 |
-
(this includes static data member definitions, nested class
|
| 756 |
-
and member function definitions, including the member
|
| 757 |
-
any portion of the declarator part of such definitions
|
| 758 |
-
*declarator-id*, including a
|
| 759 |
-
default arguments
|
|
|
|
| 760 |
|
| 761 |
[*Example 1*:
|
| 762 |
|
| 763 |
``` cpp
|
| 764 |
typedef int c;
|
|
@@ -787,21 +1011,21 @@ class D {
|
|
| 787 |
— *end example*]
|
| 788 |
|
| 789 |
The name of a class member shall only be used as follows:
|
| 790 |
|
| 791 |
- in the scope of its class (as described above) or a class derived
|
| 792 |
-
|
| 793 |
- after the `.` operator applied to an expression of the type of its
|
| 794 |
-
class
|
| 795 |
-
- after the `->` operator applied to a pointer to an object of its
|
| 796 |
-
|
| 797 |
-
- after the `::` scope resolution operator
|
| 798 |
-
the name of its class or a class derived from its class.
|
| 799 |
|
| 800 |
### Enumeration scope <a id="basic.scope.enum">[[basic.scope.enum]]</a>
|
| 801 |
|
| 802 |
-
The name of a scoped enumerator
|
| 803 |
Its potential scope begins at its point of declaration and terminates at
|
| 804 |
the end of the *enum-specifier*.
|
| 805 |
|
| 806 |
### Template parameter scope <a id="basic.scope.temp">[[basic.scope.temp]]</a>
|
| 807 |
|
|
@@ -837,12 +1061,12 @@ to belong to this declarative region in spite of its being hidden during
|
|
| 837 |
qualified and unqualified name lookup.)
|
| 838 |
|
| 839 |
— *end example*]
|
| 840 |
|
| 841 |
The potential scope of a template parameter name begins at its point of
|
| 842 |
-
declaration
|
| 843 |
-
|
| 844 |
|
| 845 |
[*Note 1*:
|
| 846 |
|
| 847 |
This implies that a *template-parameter* can be used in the declaration
|
| 848 |
of subsequent *template-parameter*s and their default arguments but
|
|
@@ -872,11 +1096,11 @@ The declarative region of the name of a template parameter is nested
|
|
| 872 |
within the immediately-enclosing declarative region.
|
| 873 |
|
| 874 |
[*Note 2*:
|
| 875 |
|
| 876 |
As a result, a *template-parameter* hides any entity with the same name
|
| 877 |
-
in an enclosing scope
|
| 878 |
|
| 879 |
[*Example 2*:
|
| 880 |
|
| 881 |
``` cpp
|
| 882 |
typedef int N;
|
|
@@ -890,33 +1114,34 @@ parameter of `A`.
|
|
| 890 |
— *end example*]
|
| 891 |
|
| 892 |
— *end note*]
|
| 893 |
|
| 894 |
[*Note 3*: Because the name of a template parameter cannot be
|
| 895 |
-
redeclared within its potential scope
|
| 896 |
parameter’s scope is often its potential scope. However, it is still
|
| 897 |
possible for a template parameter name to be hidden; see
|
| 898 |
[[temp.local]]. — *end note*]
|
| 899 |
|
| 900 |
### Name hiding <a id="basic.scope.hiding">[[basic.scope.hiding]]</a>
|
| 901 |
|
| 902 |
-
A
|
| 903 |
-
|
|
|
|
| 904 |
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
|
| 913 |
In a member function definition, the declaration of a name at block
|
| 914 |
scope hides the declaration of a member of the class with the same name;
|
| 915 |
see [[basic.scope.class]]. The declaration of a member in a derived
|
| 916 |
-
class
|
| 917 |
-
|
| 918 |
|
| 919 |
During the lookup of a name qualified by a namespace name, declarations
|
| 920 |
that would otherwise be made visible by a *using-directive* can be
|
| 921 |
hidden by declarations with the same name in the namespace containing
|
| 922 |
the *using-directive*; see [[namespace.qual]].
|
|
@@ -924,31 +1149,31 @@ the *using-directive*; see [[namespace.qual]].
|
|
| 924 |
If a name is in scope and is not hidden it is said to be *visible*.
|
| 925 |
|
| 926 |
## Name lookup <a id="basic.lookup">[[basic.lookup]]</a>
|
| 927 |
|
| 928 |
The name lookup rules apply uniformly to all names (including
|
| 929 |
-
*typedef-name*s
|
| 930 |
-
|
| 931 |
-
|
| 932 |
-
|
| 933 |
-
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
place after name lookup has succeeded. The access
|
| 938 |
-
[[class.access]]
|
| 939 |
overload resolution (if applicable) have succeeded. Only after name
|
| 940 |
lookup, function overload resolution (if applicable) and access checking
|
| 941 |
-
have succeeded are the
|
| 942 |
-
|
|
|
|
| 943 |
|
| 944 |
-
A name “looked up in the context of an expression” is looked up
|
| 945 |
-
|
| 946 |
|
| 947 |
-
The injected-class-name of a class
|
| 948 |
-
|
| 949 |
-
and lookup.
|
| 950 |
|
| 951 |
[*Note 1*: [[basic.link]] discusses linkage issues. The notions of
|
| 952 |
scope, point of declaration and name hiding are discussed in
|
| 953 |
[[basic.scope]]. — *end note*]
|
| 954 |
|
|
@@ -971,12 +1196,31 @@ function call is described in [[basic.lookup.argdep]].
|
|
| 971 |
|
| 972 |
[*Note 1*:
|
| 973 |
|
| 974 |
For purposes of determining (during parsing) whether an expression is a
|
| 975 |
*postfix-expression* for a function call, the usual name lookup rules
|
| 976 |
-
apply.
|
| 977 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 978 |
|
| 979 |
``` cpp
|
| 980 |
typedef int f;
|
| 981 |
namespace N {
|
| 982 |
struct A {
|
|
@@ -988,11 +1232,11 @@ namespace N {
|
|
| 988 |
};
|
| 989 |
}
|
| 990 |
```
|
| 991 |
|
| 992 |
Because the expression is not a function call, the argument-dependent
|
| 993 |
-
name lookup
|
| 994 |
function `f` is not found.
|
| 995 |
|
| 996 |
— *end note*]
|
| 997 |
|
| 998 |
A name used in global scope, outside of any function, class or
|
|
@@ -1002,15 +1246,15 @@ scope.
|
|
| 1002 |
A name used in a user-declared namespace outside of the definition of
|
| 1003 |
any function or class shall be declared before its use in that namespace
|
| 1004 |
or before its use in a namespace enclosing its namespace.
|
| 1005 |
|
| 1006 |
In the definition of a function that is a member of namespace `N`, a
|
| 1007 |
-
name used after the function’s *declarator-id*[^
|
| 1008 |
before its use in the block in which it is used or in one of its
|
| 1009 |
-
enclosing blocks
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
|
| 1013 |
[*Example 1*:
|
| 1014 |
|
| 1015 |
``` cpp
|
| 1016 |
namespace A {
|
|
@@ -1028,22 +1272,21 @@ void A::N::f() {
|
|
| 1028 |
}
|
| 1029 |
```
|
| 1030 |
|
| 1031 |
— *end example*]
|
| 1032 |
|
| 1033 |
-
A name used in the definition of a class `X` outside of a
|
| 1034 |
-
|
| 1035 |
-
|
| 1036 |
-
class definition[^5] shall be declared in one of the following ways:
|
| 1037 |
|
| 1038 |
-
- before its use in class `X` or be a member of a base class of `X`
|
| 1039 |
-
[[class.member.lookup]]
|
| 1040 |
-
- if `X` is a nested class of class `Y`
|
| 1041 |
definition of `X` in `Y`, or shall be a member of a base class of `Y`
|
| 1042 |
(this lookup applies in turn to `Y`’s enclosing classes, starting with
|
| 1043 |
-
the innermost enclosing class),[^
|
| 1044 |
-
- if `X` is a local class
|
| 1045 |
local class, before the definition of class `X` in a block enclosing
|
| 1046 |
the definition of class `X`, or
|
| 1047 |
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 1048 |
that is a member of `N`, or is a local class or a nested class within
|
| 1049 |
a local class of a function that is a member of `N`, before the
|
|
@@ -1076,36 +1319,34 @@ namespace N {
|
|
| 1076 |
```
|
| 1077 |
|
| 1078 |
— *end example*]
|
| 1079 |
|
| 1080 |
[*Note 2*: When looking for a prior declaration of a class or function
|
| 1081 |
-
introduced by a
|
| 1082 |
enclosing namespace scope are not considered; see
|
| 1083 |
[[namespace.memdef]]. — *end note*]
|
| 1084 |
|
| 1085 |
[*Note 3*: [[basic.scope.class]] further describes the restrictions on
|
| 1086 |
the use of names in a class definition. [[class.nest]] further describes
|
| 1087 |
the restrictions on the use of names in nested class definitions.
|
| 1088 |
[[class.local]] further describes the restrictions on the use of names
|
| 1089 |
in local class definitions. — *end note*]
|
| 1090 |
|
| 1091 |
-
For the members of a class `X`, a name used in a
|
| 1092 |
-
|
| 1093 |
-
|
| 1094 |
-
|
| 1095 |
-
definition of `X`, following the member’s *declarator-id*[^7], shall be
|
| 1096 |
-
declared in one of the following ways:
|
| 1097 |
|
| 1098 |
- before its use in the block in which it is used or in an enclosing
|
| 1099 |
-
block
|
| 1100 |
-
- shall be a member of class `X` or be a member of a base class of `X`
|
| 1101 |
-
[[class.member.lookup]]
|
| 1102 |
-
- if `X` is a nested class of class `Y`
|
| 1103 |
member of `Y`, or shall be a member of a base class of `Y` (this
|
| 1104 |
lookup applies in turn to `Y`’s enclosing classes, starting with the
|
| 1105 |
-
innermost enclosing class),[^
|
| 1106 |
-
- if `X` is a local class
|
| 1107 |
local class, before the definition of class `X` in a block enclosing
|
| 1108 |
the definition of class `X`, or
|
| 1109 |
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 1110 |
that is a member of `N`, or is a local class or a nested class within
|
| 1111 |
a local class of a function that is a member of `N`, before the use of
|
|
@@ -1142,21 +1383,21 @@ restrictions on the use of names in member function definitions.
|
|
| 1142 |
[[class.nest]] further describes the restrictions on the use of names in
|
| 1143 |
the scope of nested classes. [[class.local]] further describes the
|
| 1144 |
restrictions on the use of names in local class
|
| 1145 |
definitions. — *end note*]
|
| 1146 |
|
| 1147 |
-
Name lookup for a name used in the definition of a
|
| 1148 |
-
[[class.friend]]
|
| 1149 |
proceed as described for lookup in member function definitions. If the
|
| 1150 |
-
|
| 1151 |
-
lookup in the
|
| 1152 |
-
|
| 1153 |
|
| 1154 |
-
In a
|
| 1155 |
function declarator and not part of a *template-argument* in the
|
| 1156 |
*declarator-id* is first looked up in the scope of the member function’s
|
| 1157 |
-
class
|
| 1158 |
part of a *template-argument* in the *declarator-id*, the look up is as
|
| 1159 |
described for unqualified names in the definition of the class granting
|
| 1160 |
friendship.
|
| 1161 |
|
| 1162 |
[*Example 4*:
|
|
@@ -1177,14 +1418,14 @@ struct B {
|
|
| 1177 |
};
|
| 1178 |
```
|
| 1179 |
|
| 1180 |
— *end example*]
|
| 1181 |
|
| 1182 |
-
During the lookup for a name used as a default argument
|
| 1183 |
-
[[dcl.fct.default]]
|
| 1184 |
-
|
| 1185 |
-
[[class.base.init]]
|
| 1186 |
the names of entities declared in the block, class or namespace scopes
|
| 1187 |
containing the function declaration.
|
| 1188 |
|
| 1189 |
[*Note 5*: [[dcl.fct.default]] further describes the restrictions on
|
| 1190 |
the use of names in default arguments. [[class.base.init]] further
|
|
@@ -1194,13 +1435,13 @@ describes the restrictions on the use of names in a
|
|
| 1194 |
During the lookup of a name used in the *constant-expression* of an
|
| 1195 |
*enumerator-definition*, previously declared *enumerator*s of the
|
| 1196 |
enumeration are visible and hide the names of entities declared in the
|
| 1197 |
block, class, or namespace scopes containing the *enum-specifier*.
|
| 1198 |
|
| 1199 |
-
A name used in the definition of a `static` data member of class `X`
|
| 1200 |
-
[[class.static.data]]
|
| 1201 |
-
|
| 1202 |
|
| 1203 |
[*Note 6*: [[class.static.data]] further describes the restrictions on
|
| 1204 |
the use of names in the definition of a `static` data
|
| 1205 |
member. — *end note*]
|
| 1206 |
|
|
@@ -1222,32 +1463,32 @@ int i = 2;
|
|
| 1222 |
int N::j = i; // N::j == 4
|
| 1223 |
```
|
| 1224 |
|
| 1225 |
— *end example*]
|
| 1226 |
|
| 1227 |
-
A name used in the handler for a *function-try-block*
|
| 1228 |
-
|
| 1229 |
-
|
| 1230 |
-
|
| 1231 |
-
|
| 1232 |
-
|
| 1233 |
-
|
| 1234 |
|
| 1235 |
[*Note 7*: But function parameter names are found. — *end note*]
|
| 1236 |
|
| 1237 |
[*Note 8*: The rules for name lookup in template definitions are
|
| 1238 |
described in [[temp.res]]. — *end note*]
|
| 1239 |
|
| 1240 |
### Argument-dependent name lookup <a id="basic.lookup.argdep">[[basic.lookup.argdep]]</a>
|
| 1241 |
|
| 1242 |
-
When the *postfix-expression* in a function call
|
| 1243 |
*unqualified-id*, other namespaces not considered during the usual
|
| 1244 |
-
unqualified lookup
|
| 1245 |
-
|
| 1246 |
-
declarations
|
| 1247 |
-
|
| 1248 |
-
|
| 1249 |
argument).
|
| 1250 |
|
| 1251 |
[*Example 1*:
|
| 1252 |
|
| 1253 |
``` cpp
|
|
@@ -1265,62 +1506,62 @@ void g() {
|
|
| 1265 |
|
| 1266 |
— *end example*]
|
| 1267 |
|
| 1268 |
For each argument type `T` in the function call, there is a set of zero
|
| 1269 |
or more *associated namespaces* and a set of zero or more *associated
|
| 1270 |
-
|
| 1271 |
-
determined entirely by the types of the
|
| 1272 |
-
namespace of any template template
|
| 1273 |
-
*using-declaration*s used to specify the
|
| 1274 |
-
set. The sets of namespaces and
|
| 1275 |
-
way:
|
| 1276 |
|
| 1277 |
- If `T` is a fundamental type, its associated sets of namespaces and
|
| 1278 |
-
|
| 1279 |
-
- If `T` is a class type (including unions), its associated
|
| 1280 |
-
the class itself; the class of which it is a member, if any; and
|
| 1281 |
-
direct and indirect base classes. Its associated namespaces are
|
| 1282 |
-
innermost enclosing namespaces of its associated
|
| 1283 |
-
if `T` is a class template specialization, its associated
|
| 1284 |
-
and
|
| 1285 |
-
the types of the template arguments provided for
|
| 1286 |
-
parameters (excluding template template parameters); the
|
| 1287 |
-
|
| 1288 |
-
|
| 1289 |
-
|
| 1290 |
-
|
|
|
|
| 1291 |
- If `T` is an enumeration type, its associated namespace is the
|
| 1292 |
-
innermost enclosing namespace of its declaration
|
| 1293 |
-
|
| 1294 |
-
associated class.
|
| 1295 |
- If `T` is a pointer to `U` or an array of `U`, its associated
|
| 1296 |
-
namespaces and
|
| 1297 |
-
- If `T` is a function type, its associated namespaces and
|
| 1298 |
those associated with the function parameter types and those
|
| 1299 |
associated with the return type.
|
| 1300 |
- If `T` is a pointer to a member function of a class `X`, its
|
| 1301 |
-
associated namespaces and
|
| 1302 |
function parameter types and return type, together with those
|
| 1303 |
associated with `X`.
|
| 1304 |
- If `T` is a pointer to a data member of class `X`, its associated
|
| 1305 |
-
namespaces and
|
| 1306 |
together with those associated with `X`.
|
| 1307 |
|
| 1308 |
-
If an associated namespace is an inline namespace
|
| 1309 |
-
|
| 1310 |
namespace directly contains inline namespaces, those inline namespaces
|
| 1311 |
are also included in the set. In addition, if the argument is the name
|
| 1312 |
-
or address of
|
| 1313 |
-
|
| 1314 |
-
|
| 1315 |
-
|
| 1316 |
-
|
| 1317 |
-
*template-
|
| 1318 |
-
|
| 1319 |
|
| 1320 |
-
Let *X* be the lookup set produced by unqualified lookup
|
| 1321 |
-
[[basic.lookup.unqual]]
|
| 1322 |
argument dependent lookup (defined as follows). If *X* contains
|
| 1323 |
|
| 1324 |
- a declaration of a class member, or
|
| 1325 |
- a block-scope function declaration that is not a *using-declaration*,
|
| 1326 |
or
|
|
@@ -1329,12 +1570,12 @@ argument dependent lookup (defined as follows). If *X* contains
|
|
| 1329 |
then *Y* is empty. Otherwise *Y* is the set of declarations found in the
|
| 1330 |
namespaces associated with the argument types as described below. The
|
| 1331 |
set of declarations found by the lookup of the name is the union of *X*
|
| 1332 |
and *Y*.
|
| 1333 |
|
| 1334 |
-
[*Note 2*: The namespaces and
|
| 1335 |
-
types can include namespaces and
|
| 1336 |
ordinary unqualified lookup. — *end note*]
|
| 1337 |
|
| 1338 |
[*Example 2*:
|
| 1339 |
|
| 1340 |
``` cpp
|
|
@@ -1352,27 +1593,88 @@ int main() {
|
|
| 1352 |
}
|
| 1353 |
```
|
| 1354 |
|
| 1355 |
— *end example*]
|
| 1356 |
|
| 1357 |
-
When considering an associated namespace, the lookup is the same as
|
| 1358 |
-
lookup performed when
|
| 1359 |
-
|
| 1360 |
|
| 1361 |
-
- Any *using-directive*s in
|
| 1362 |
-
- Any namespace-scope friend functions or friend function templates
|
| 1363 |
-
declared in associated classes are visible within their respective
|
| 1364 |
-
namespaces even if they are not visible during an ordinary lookup (
|
| 1365 |
-
[[class.friend]]).
|
| 1366 |
- All names except those of (possibly overloaded) functions and function
|
| 1367 |
templates are ignored.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1368 |
|
| 1369 |
### Qualified name lookup <a id="basic.lookup.qual">[[basic.lookup.qual]]</a>
|
| 1370 |
|
| 1371 |
The name of a class or namespace member or enumerator can be referred to
|
| 1372 |
-
after the `::` scope resolution operator
|
| 1373 |
-
*nested-name-specifier* that denotes its class, namespace, or
|
| 1374 |
enumeration. If a `::` scope resolution operator in a
|
| 1375 |
*nested-name-specifier* is not preceded by a *decltype-specifier*,
|
| 1376 |
lookup of the name preceding that `::` considers only namespaces, types,
|
| 1377 |
and templates whose specializations are types. If the name found does
|
| 1378 |
not designate a namespace or a class, enumeration, or dependent type,
|
|
@@ -1386,19 +1688,19 @@ public:
|
|
| 1386 |
static int n;
|
| 1387 |
};
|
| 1388 |
int main() {
|
| 1389 |
int A;
|
| 1390 |
A::n = 42; // OK
|
| 1391 |
-
A b; //
|
| 1392 |
}
|
| 1393 |
```
|
| 1394 |
|
| 1395 |
— *end example*]
|
| 1396 |
|
| 1397 |
[*Note 1*: Multiply qualified names, such as `N1::N2::N3::n`, can be
|
| 1398 |
-
used to refer to members of nested classes
|
| 1399 |
-
|
| 1400 |
|
| 1401 |
In a declaration in which the *declarator-id* is a *qualified-id*, names
|
| 1402 |
used before the *qualified-id* being declared are looked up in the
|
| 1403 |
defining namespace scope; names following the *qualified-id* are looked
|
| 1404 |
up in the scope of the member’s class or namespace.
|
|
@@ -1410,38 +1712,35 @@ class X { };
|
|
| 1410 |
class C {
|
| 1411 |
class X { };
|
| 1412 |
static const int number = 50;
|
| 1413 |
static X arr[number];
|
| 1414 |
};
|
| 1415 |
-
X C::arr[number]; //
|
| 1416 |
// equivalent to ::X C::arr[C::number];
|
| 1417 |
// and not to C::X C::arr[C::number];
|
| 1418 |
```
|
| 1419 |
|
| 1420 |
— *end example*]
|
| 1421 |
|
| 1422 |
-
A name prefixed by the unary scope operator `::`
|
| 1423 |
-
looked up in global scope, in the translation unit where it is used.
|
| 1424 |
-
name shall be declared in global namespace scope or shall be a name
|
| 1425 |
whose declaration is visible in global scope because of a
|
| 1426 |
-
*using-directive*
|
| 1427 |
-
name to be referred to even if its identifier has been hidden
|
| 1428 |
-
[[basic.scope.hiding]]
|
| 1429 |
|
| 1430 |
A name prefixed by a *nested-name-specifier* that nominates an
|
| 1431 |
enumeration type shall represent an *enumerator* of that enumeration.
|
| 1432 |
|
| 1433 |
-
|
| 1434 |
-
*nested-name-specifier*, the *type-name*s are looked up as types in the
|
| 1435 |
-
scope designated by the *nested-name-specifier*. Similarly, in a
|
| 1436 |
-
*qualified-id* of the form:
|
| 1437 |
|
| 1438 |
``` bnf
|
| 1439 |
-
nested-name-specifierₒₚₜ
|
| 1440 |
```
|
| 1441 |
|
| 1442 |
-
the second *
|
| 1443 |
|
| 1444 |
[*Example 3*:
|
| 1445 |
|
| 1446 |
``` cpp
|
| 1447 |
struct C {
|
|
@@ -1470,40 +1769,39 @@ proceeds after the `.` and `->` operators. — *end note*]
|
|
| 1470 |
|
| 1471 |
#### Class members <a id="class.qual">[[class.qual]]</a>
|
| 1472 |
|
| 1473 |
If the *nested-name-specifier* of a *qualified-id* nominates a class,
|
| 1474 |
the name specified after the *nested-name-specifier* is looked up in the
|
| 1475 |
-
scope of the class
|
| 1476 |
-
|
| 1477 |
-
|
| 1478 |
|
| 1479 |
[*Note 1*: A class member can be referred to using a *qualified-id* at
|
| 1480 |
-
any point in its potential scope
|
| 1481 |
-
[[basic.scope.class]]). — *end note*]
|
| 1482 |
|
| 1483 |
The exceptions to the name lookup rule above are the following:
|
| 1484 |
|
| 1485 |
- the lookup for a destructor is as specified in [[basic.lookup.qual]];
|
| 1486 |
- a *conversion-type-id* of a *conversion-function-id* is looked up in
|
| 1487 |
the same manner as a *conversion-type-id* in a class member access
|
| 1488 |
(see [[basic.lookup.classref]]);
|
| 1489 |
- the names in a *template-argument* of a *template-id* are looked up in
|
| 1490 |
-
the context in which the entire *postfix-expression* occurs
|
| 1491 |
-
- the lookup for a name specified in a *using-declaration*
|
| 1492 |
-
[[namespace.udecl]]
|
| 1493 |
-
within the same scope
|
| 1494 |
|
| 1495 |
-
In a lookup in which function names are not ignored[^
|
| 1496 |
*nested-name-specifier* nominates a class `C`:
|
| 1497 |
|
| 1498 |
- if the name specified after the *nested-name-specifier*, when looked
|
| 1499 |
-
up in `C`, is the injected-class-name of `C`
|
| 1500 |
-
- in a *using-declarator* of a *using-declaration*
|
| 1501 |
-
|
| 1502 |
-
|
| 1503 |
-
*
|
| 1504 |
-
|
| 1505 |
|
| 1506 |
the name is instead considered to name the constructor of class `C`.
|
| 1507 |
|
| 1508 |
[*Note 2*: For example, the constructor is not an acceptable lookup
|
| 1509 |
result in an *elaborated-type-specifier* so the constructor would not be
|
|
@@ -1520,11 +1818,11 @@ struct B: public A { B(); };
|
|
| 1520 |
|
| 1521 |
A::A() { }
|
| 1522 |
B::B() { }
|
| 1523 |
|
| 1524 |
B::A ba; // object of type A
|
| 1525 |
-
A::A a; // error
|
| 1526 |
struct A::A a2; // object of type A
|
| 1527 |
```
|
| 1528 |
|
| 1529 |
— *end example*]
|
| 1530 |
|
|
@@ -1541,22 +1839,24 @@ nominating the global namespace), the name specified after the
|
|
| 1541 |
names in a *template-argument* of a *template-id* are looked up in the
|
| 1542 |
context in which the entire *postfix-expression* occurs.
|
| 1543 |
|
| 1544 |
For a namespace `X` and name `m`, the namespace-qualified lookup set
|
| 1545 |
S(X, m) is defined as follows: Let S'(X, m) be the set of all
|
| 1546 |
-
declarations of `m` in `X` and the inline namespace set of `X`
|
| 1547 |
-
[[namespace.def]]
|
|
|
|
|
|
|
| 1548 |
otherwise, S(X, m) is the union of S(Nᵢ, m) for all namespaces Nᵢ
|
| 1549 |
nominated by *using-directive*s in `X` and its inline namespace set.
|
| 1550 |
|
| 1551 |
Given `X::m` (where `X` is a user-declared namespace), or given `::m`
|
| 1552 |
(where X is the global namespace), if S(X, m) is the empty set, the
|
| 1553 |
program is ill-formed. Otherwise, if S(X, m) has exactly one member, or
|
| 1554 |
-
if the context of the reference is a *using-declaration*
|
| 1555 |
-
[[namespace.udecl]]
|
| 1556 |
-
|
| 1557 |
-
|
| 1558 |
|
| 1559 |
[*Example 1*:
|
| 1560 |
|
| 1561 |
``` cpp
|
| 1562 |
int x;
|
|
@@ -1692,11 +1992,11 @@ void f()
|
|
| 1692 |
— *end example*]
|
| 1693 |
|
| 1694 |
During the lookup of a qualified namespace member name, if the lookup
|
| 1695 |
finds more than one declaration of the member, and if one declaration
|
| 1696 |
introduces a class name or enumeration name and the other declarations
|
| 1697 |
-
|
| 1698 |
functions, the non-type name hides the class or enumeration name if and
|
| 1699 |
only if the declarations are from the same namespace; otherwise (the
|
| 1700 |
declarations are from different namespaces), the program is ill-formed.
|
| 1701 |
|
| 1702 |
[*Example 4*:
|
|
@@ -1729,23 +2029,23 @@ has the form
|
|
| 1729 |
``` bnf
|
| 1730 |
nested-name-specifier unqualified-id
|
| 1731 |
```
|
| 1732 |
|
| 1733 |
the *unqualified-id* shall name a member of the namespace designated by
|
| 1734 |
-
the *nested-name-specifier* or of an element of the inline namespace
|
| 1735 |
-
|
| 1736 |
|
| 1737 |
[*Example 5*:
|
| 1738 |
|
| 1739 |
``` cpp
|
| 1740 |
namespace A {
|
| 1741 |
namespace B {
|
| 1742 |
void f1(int);
|
| 1743 |
}
|
| 1744 |
using namespace B;
|
| 1745 |
}
|
| 1746 |
-
void A::f1(int){ } //
|
| 1747 |
```
|
| 1748 |
|
| 1749 |
— *end example*]
|
| 1750 |
|
| 1751 |
However, in such namespace member declarations, the
|
|
@@ -1774,14 +2074,13 @@ void B::f1(int){ } // OK, defines A::B::f1(int)
|
|
| 1774 |
|
| 1775 |
— *end example*]
|
| 1776 |
|
| 1777 |
### Elaborated type specifiers <a id="basic.lookup.elab">[[basic.lookup.elab]]</a>
|
| 1778 |
|
| 1779 |
-
An *elaborated-type-specifier*
|
| 1780 |
-
|
| 1781 |
-
|
| 1782 |
-
[[basic.scope.hiding]]).
|
| 1783 |
|
| 1784 |
If the *elaborated-type-specifier* has no *nested-name-specifier*, and
|
| 1785 |
unless the *elaborated-type-specifier* appears in a declaration with the
|
| 1786 |
following form:
|
| 1787 |
|
|
@@ -1813,18 +2112,17 @@ declared. If the name lookup does not find a previously declared
|
|
| 1813 |
|
| 1814 |
[*Example 1*:
|
| 1815 |
|
| 1816 |
``` cpp
|
| 1817 |
struct Node {
|
| 1818 |
-
struct Node* Next; // OK: Refers to
|
| 1819 |
-
struct Data* Data; // OK: Declares type Data
|
| 1820 |
-
// at global scope and member Data
|
| 1821 |
};
|
| 1822 |
|
| 1823 |
struct Data {
|
| 1824 |
struct Node* Node; // OK: Refers to Node at global scope
|
| 1825 |
-
friend struct ::Glob; // error: Glob is not declared, cannot introduce a qualified type
|
| 1826 |
friend struct Glob; // OK: Refers to (as yet) undeclared Glob at global scope.
|
| 1827 |
...
|
| 1828 |
};
|
| 1829 |
|
| 1830 |
struct Base {
|
|
@@ -1835,34 +2133,33 @@ struct Base {
|
|
| 1835 |
friend class Data; // OK: nested Data is a friend
|
| 1836 |
struct Data { ... }; // Defines nested Data
|
| 1837 |
};
|
| 1838 |
|
| 1839 |
struct Data; // OK: Redeclares Data at global scope
|
| 1840 |
-
struct ::Data; // error: cannot introduce a qualified type
|
| 1841 |
-
struct Base::Data; // error: cannot introduce a qualified type
|
| 1842 |
struct Base::Datum; // error: Datum undefined
|
| 1843 |
struct Base::Data* pBase; // OK: refers to nested Data
|
| 1844 |
```
|
| 1845 |
|
| 1846 |
— *end example*]
|
| 1847 |
|
| 1848 |
### Class member access <a id="basic.lookup.classref">[[basic.lookup.classref]]</a>
|
| 1849 |
|
| 1850 |
-
In a class member access expression
|
| 1851 |
token is immediately followed by an *identifier* followed by a `<`, the
|
| 1852 |
identifier must be looked up to determine whether the `<` is the
|
| 1853 |
-
beginning of a template argument list
|
| 1854 |
operator. The identifier is first looked up in the class of the object
|
| 1855 |
-
expression. If the identifier is not found, it
|
| 1856 |
-
context of the entire *postfix-expression* and
|
| 1857 |
-
template.
|
| 1858 |
|
| 1859 |
-
If the *id-expression* in a class member access
|
| 1860 |
*unqualified-id*, and the type of the object expression is of a class
|
| 1861 |
-
type `C`, the *unqualified-id* is looked up in the scope of class `C`
|
| 1862 |
-
|
| 1863 |
-
looked up in the context of the complete *postfix-expression*.
|
| 1864 |
|
| 1865 |
If the *unqualified-id* is `~`*type-name*, the *type-name* is looked up
|
| 1866 |
in the context of the entire *postfix-expression*. If the type `T` of
|
| 1867 |
the object expression is of a class type `C`, the *type-name* is also
|
| 1868 |
looked up in the scope of class `C`. At least one of the lookups shall
|
|
@@ -1890,14 +2187,14 @@ the form
|
|
| 1890 |
|
| 1891 |
``` cpp
|
| 1892 |
class-name-or-namespace-name::...
|
| 1893 |
```
|
| 1894 |
|
| 1895 |
-
the
|
| 1896 |
-
first looked up in the class of the object expression
|
| 1897 |
-
found, is used. Otherwise it is
|
| 1898 |
-
*postfix-expression*.
|
| 1899 |
|
| 1900 |
[*Note 1*: See [[basic.lookup.qual]], which describes the lookup of a
|
| 1901 |
name before `::`, which will only find a type or namespace
|
| 1902 |
name. — *end note*]
|
| 1903 |
|
|
@@ -1905,23 +2202,23 @@ If the *qualified-id* has the form
|
|
| 1905 |
|
| 1906 |
``` cpp
|
| 1907 |
::class-name-or-namespace-name::...
|
| 1908 |
```
|
| 1909 |
|
| 1910 |
-
the
|
| 1911 |
*class-name* or *namespace-name*.
|
| 1912 |
|
| 1913 |
-
If the *nested-name-specifier* contains a *simple-template-id*
|
| 1914 |
-
[[temp.names]]
|
| 1915 |
the context in which the entire *postfix-expression* occurs.
|
| 1916 |
|
| 1917 |
If the *id-expression* is a *conversion-function-id*, its
|
| 1918 |
*conversion-type-id* is first looked up in the class of the object
|
| 1919 |
-
expression and the name, if found, is used.
|
| 1920 |
-
|
| 1921 |
-
lookups, only names that denote
|
| 1922 |
-
are types are considered.
|
| 1923 |
|
| 1924 |
[*Example 2*:
|
| 1925 |
|
| 1926 |
``` cpp
|
| 1927 |
struct A { };
|
|
@@ -1946,82 +2243,108 @@ In a *using-directive* or *namespace-alias-definition*, during the
|
|
| 1946 |
lookup for a *namespace-name* or for a name in a *nested-name-specifier*
|
| 1947 |
only namespace names are considered.
|
| 1948 |
|
| 1949 |
## Program and linkage <a id="basic.link">[[basic.link]]</a>
|
| 1950 |
|
| 1951 |
-
A *program* consists of one or more
|
| 1952 |
-
|
| 1953 |
declarations.
|
| 1954 |
|
| 1955 |
``` bnf
|
| 1956 |
translation-unit:
|
| 1957 |
declaration-seqₒₚₜ
|
|
|
|
| 1958 |
```
|
| 1959 |
|
| 1960 |
A name is said to have *linkage* when it might denote the same object,
|
| 1961 |
reference, function, type, template, namespace or value as a name
|
| 1962 |
introduced by a declaration in another scope:
|
| 1963 |
|
| 1964 |
- When a name has *external linkage*, the entity it denotes can be
|
| 1965 |
referred to by names from scopes of other translation units or from
|
| 1966 |
other scopes of the same translation unit.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1967 |
- When a name has *internal linkage*, the entity it denotes can be
|
| 1968 |
referred to by names from other scopes in the same translation unit.
|
| 1969 |
- When a name has *no linkage*, the entity it denotes cannot be referred
|
| 1970 |
to by names from other scopes.
|
| 1971 |
|
| 1972 |
-
A name having namespace scope
|
| 1973 |
linkage if it is the name of
|
| 1974 |
|
| 1975 |
-
- a variable, function or function template that is
|
| 1976 |
-
`static`; or
|
| 1977 |
-
- a non-
|
| 1978 |
-
|
| 1979 |
-
|
|
|
|
|
|
|
| 1980 |
- a data member of an anonymous union.
|
| 1981 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1982 |
An unnamed namespace or a namespace declared directly or indirectly
|
| 1983 |
within an unnamed namespace has internal linkage. All other namespaces
|
| 1984 |
have external linkage. A name having namespace scope that has not been
|
| 1985 |
-
given internal linkage above
|
| 1986 |
-
namespace if it is the name of
|
| 1987 |
|
| 1988 |
- a variable; or
|
| 1989 |
- a function; or
|
| 1990 |
-
- a named class
|
| 1991 |
-
|
| 1992 |
-
|
| 1993 |
-
- a named enumeration
|
| 1994 |
-
|
| 1995 |
-
for linkage purposes
|
| 1996 |
-
- a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1997 |
|
| 1998 |
In addition, a member function, static data member, a named class or
|
| 1999 |
enumeration of class scope, or an unnamed class or enumeration defined
|
| 2000 |
in a class-scope typedef declaration such that the class or enumeration
|
| 2001 |
-
has the typedef name for linkage purposes
|
| 2002 |
-
|
| 2003 |
|
| 2004 |
The name of a function declared in block scope and the name of a
|
| 2005 |
variable declared by a block scope `extern` declaration have linkage. If
|
| 2006 |
-
|
| 2007 |
-
|
| 2008 |
-
|
| 2009 |
-
|
| 2010 |
-
|
|
|
|
|
|
|
|
|
|
| 2011 |
Otherwise, if no matching entity is found, the block scope entity
|
| 2012 |
receives external linkage. If, within a translation unit, the same
|
| 2013 |
entity is declared with both internal and external linkage, the program
|
| 2014 |
is ill-formed.
|
| 2015 |
|
| 2016 |
[*Example 1*:
|
| 2017 |
|
| 2018 |
``` cpp
|
| 2019 |
static void f();
|
|
|
|
| 2020 |
static int i = 0; // #1
|
| 2021 |
void g() {
|
| 2022 |
extern void f(); // internal linkage
|
|
|
|
| 2023 |
int i; // #2: i has no linkage
|
| 2024 |
{
|
| 2025 |
extern void f(); // internal linkage
|
| 2026 |
extern int i; // #3: external linkage, ill-formed
|
| 2027 |
}
|
|
@@ -2060,403 +2383,763 @@ void q() { ... } // some other, unrelated q
|
|
| 2060 |
```
|
| 2061 |
|
| 2062 |
— *end example*]
|
| 2063 |
|
| 2064 |
Names not covered by these rules have no linkage. Moreover, except as
|
| 2065 |
-
noted, a name declared at block scope
|
| 2066 |
-
linkage.
|
| 2067 |
|
| 2068 |
-
|
| 2069 |
-
linkage purposes ([[dcl.typedef]])) and the name has linkage; or
|
| 2070 |
-
- it is an unnamed class or unnamed enumeration that is a member of a
|
| 2071 |
-
class with linkage; or
|
| 2072 |
-
- it is a specialization of a class template (Clause [[temp]])[^10]; or
|
| 2073 |
-
- it is a fundamental type ([[basic.fundamental]]); or
|
| 2074 |
-
- it is a compound type ([[basic.compound]]) other than a class or
|
| 2075 |
-
enumeration, compounded exclusively from types that have linkage; or
|
| 2076 |
-
- it is a cv-qualified ([[basic.type.qualifier]]) version of a type
|
| 2077 |
-
that has linkage.
|
| 2078 |
-
|
| 2079 |
-
A type without linkage shall not be used as the type of a variable or
|
| 2080 |
-
function with external linkage unless
|
| 2081 |
-
|
| 2082 |
-
- the entity has C language linkage ([[dcl.link]]), or
|
| 2083 |
-
- the entity is declared within an unnamed namespace (
|
| 2084 |
-
[[namespace.def]]), or
|
| 2085 |
-
- the entity is not odr-used ([[basic.def.odr]]) or is defined in the
|
| 2086 |
-
same translation unit.
|
| 2087 |
-
|
| 2088 |
-
[*Note 1*: In other words, a type without linkage contains a class or
|
| 2089 |
-
enumeration that cannot be named outside its translation unit. An entity
|
| 2090 |
-
with external linkage declared using such a type could not correspond to
|
| 2091 |
-
any other entity in another translation unit of the program and thus
|
| 2092 |
-
must be defined in the translation unit if it is odr-used. Also note
|
| 2093 |
-
that classes with linkage may contain members whose types do not have
|
| 2094 |
-
linkage, and that typedef names are ignored in the determination of
|
| 2095 |
-
whether a type has linkage. — *end note*]
|
| 2096 |
-
|
| 2097 |
-
[*Example 3*:
|
| 2098 |
-
|
| 2099 |
-
``` cpp
|
| 2100 |
-
template <class T> struct B {
|
| 2101 |
-
void g(T) { }
|
| 2102 |
-
void h(T);
|
| 2103 |
-
friend void i(B, T) { }
|
| 2104 |
-
};
|
| 2105 |
-
|
| 2106 |
-
void f() {
|
| 2107 |
-
struct A { int x; }; // no linkage
|
| 2108 |
-
A a = { 1 };
|
| 2109 |
-
B<A> ba; // declares B<A>::g(A) and B<A>::h(A)
|
| 2110 |
-
ba.g(a); // OK
|
| 2111 |
-
ba.h(a); // error: B<A>::h(A) not defined in the translation unit
|
| 2112 |
-
i(ba, a); // OK
|
| 2113 |
-
}
|
| 2114 |
-
```
|
| 2115 |
-
|
| 2116 |
-
— *end example*]
|
| 2117 |
-
|
| 2118 |
-
Two names that are the same (Clause [[basic]]) and that are declared in
|
| 2119 |
different scopes shall denote the same variable, function, type,
|
| 2120 |
template or namespace if
|
| 2121 |
|
| 2122 |
-
- both names have external
|
| 2123 |
-
|
|
|
|
| 2124 |
- both names refer to members of the same namespace or to members, not
|
| 2125 |
by inheritance, of the same class; and
|
| 2126 |
-
- when both names denote functions
|
| 2127 |
-
|
| 2128 |
-
|
| 2129 |
-
[[temp.over.link]]) are the same.
|
| 2130 |
|
| 2131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2132 |
are replaced by their definitions), the types specified by all
|
| 2133 |
declarations referring to a given variable or function shall be
|
| 2134 |
identical, except that declarations for an array object can specify
|
| 2135 |
array types that differ by the presence or absence of a major array
|
| 2136 |
-
bound
|
| 2137 |
-
|
| 2138 |
-
|
| 2139 |
-
[*Note
|
| 2140 |
-
*linkage-specification*
|
| 2141 |
-
|
| 2142 |
-
|
| 2143 |
-
|
| 2144 |
-
|
| 2145 |
-
|
| 2146 |
-
|
| 2147 |
-
|
| 2148 |
-
|
| 2149 |
-
|
| 2150 |
-
|
| 2151 |
-
|
| 2152 |
-
|
| 2153 |
-
|
| 2154 |
-
|
| 2155 |
-
|
| 2156 |
-
|
| 2157 |
-
|
| 2158 |
-
|
| 2159 |
-
|
| 2160 |
-
|
| 2161 |
-
|
| 2162 |
-
|
| 2163 |
-
|
| 2164 |
-
|
| 2165 |
-
|
| 2166 |
-
|
| 2167 |
-
|
| 2168 |
-
|
| 2169 |
-
|
| 2170 |
-
|
| 2171 |
-
|
| 2172 |
-
|
| 2173 |
-
|
| 2174 |
-
|
| 2175 |
-
|
| 2176 |
-
|
| 2177 |
-
|
| 2178 |
-
|
| 2179 |
-
|
| 2180 |
-
|
| 2181 |
-
|
| 2182 |
-
|
| 2183 |
-
|
| 2184 |
-
|
| 2185 |
-
|
| 2186 |
-
|
| 2187 |
-
|
| 2188 |
-
|
| 2189 |
-
|
| 2190 |
-
|
| 2191 |
-
|
| 2192 |
-
|
| 2193 |
-
|
| 2194 |
-
|
| 2195 |
-
|
| 2196 |
-
|
| 2197 |
-
|
| 2198 |
-
|
| 2199 |
-
|
| 2200 |
-
|
| 2201 |
-
|
| 2202 |
-
|
| 2203 |
-
|
| 2204 |
-
|
| 2205 |
-
|
| 2206 |
-
|
| 2207 |
-
|
| 2208 |
-
|
| 2209 |
-
|
| 2210 |
-
|
| 2211 |
-
|
| 2212 |
-
|
| 2213 |
-
|
| 2214 |
-
|
| 2215 |
-
A *constant initializer* for a variable or temporary object `o` is an
|
| 2216 |
-
initializer whose full-expression is a constant expression, except that
|
| 2217 |
-
if `o` is an object, such an initializer may also invoke constexpr
|
| 2218 |
-
constructors for `o` and its subobjects even if those objects are of
|
| 2219 |
-
non-literal class types.
|
| 2220 |
-
|
| 2221 |
-
[*Note 1*: Such a class may have a non-trivial
|
| 2222 |
-
destructor. — *end note*]
|
| 2223 |
-
|
| 2224 |
-
*Constant initialization* is performed if a variable or temporary object
|
| 2225 |
-
with static or thread storage duration is initialized by a constant
|
| 2226 |
-
initializer for the entity. If constant initialization is not performed,
|
| 2227 |
-
a variable with static storage duration ([[basic.stc.static]]) or
|
| 2228 |
-
thread storage duration ([[basic.stc.thread]]) is zero-initialized (
|
| 2229 |
-
[[dcl.init]]). Together, zero-initialization and constant initialization
|
| 2230 |
-
are called *static initialization*; all other initialization is *dynamic
|
| 2231 |
-
initialization*. All static initialization strongly happens before (
|
| 2232 |
-
[[intro.races]]) any dynamic initialization.
|
| 2233 |
-
|
| 2234 |
-
[*Note 2*: The dynamic initialization of non-local variables is
|
| 2235 |
-
described in [[basic.start.dynamic]]; that of local static variables is
|
| 2236 |
-
described in [[stmt.dcl]]. — *end note*]
|
| 2237 |
-
|
| 2238 |
-
An implementation is permitted to perform the initialization of a
|
| 2239 |
-
variable with static or thread storage duration as a static
|
| 2240 |
-
initialization even if such initialization is not required to be done
|
| 2241 |
-
statically, provided that
|
| 2242 |
-
|
| 2243 |
-
- the dynamic version of the initialization does not change the value of
|
| 2244 |
-
any other object of static or thread storage duration prior to its
|
| 2245 |
-
initialization, and
|
| 2246 |
-
- the static version of the initialization produces the same value in
|
| 2247 |
-
the initialized variable as would be produced by the dynamic
|
| 2248 |
-
initialization if all variables not required to be initialized
|
| 2249 |
-
statically were initialized dynamically.
|
| 2250 |
-
|
| 2251 |
-
[*Note 3*:
|
| 2252 |
-
|
| 2253 |
-
As a consequence, if the initialization of an object `obj1` refers to an
|
| 2254 |
-
object `obj2` of namespace scope potentially requiring dynamic
|
| 2255 |
-
initialization and defined later in the same translation unit, it is
|
| 2256 |
-
unspecified whether the value of `obj2` used will be the value of the
|
| 2257 |
-
fully initialized `obj2` (because `obj2` was statically initialized) or
|
| 2258 |
-
will be the value of `obj2` merely zero-initialized. For example,
|
| 2259 |
|
| 2260 |
``` cpp
|
| 2261 |
-
|
| 2262 |
-
|
| 2263 |
-
|
| 2264 |
-
|
| 2265 |
-
|
| 2266 |
-
|
| 2267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2268 |
```
|
| 2269 |
|
| 2270 |
-
|
| 2271 |
-
|
| 2272 |
-
|
| 2273 |
-
|
| 2274 |
-
|
| 2275 |
-
|
| 2276 |
-
|
| 2277 |
-
|
| 2278 |
-
|
| 2279 |
-
|
| 2280 |
-
|
| 2281 |
-
|
| 2282 |
-
|
| 2283 |
-
|
| 2284 |
-
|
| 2285 |
-
|
| 2286 |
-
|
| 2287 |
-
|
| 2288 |
-
|
| 2289 |
-
|
| 2290 |
-
|
| 2291 |
-
|
| 2292 |
-
|
| 2293 |
-
|
| 2294 |
-
|
| 2295 |
-
|
| 2296 |
-
|
| 2297 |
-
|
| 2298 |
-
|
| 2299 |
-
|
| 2300 |
-
|
| 2301 |
-
|
| 2302 |
-
|
| 2303 |
-
|
| 2304 |
-
|
| 2305 |
-
|
| 2306 |
-
|
| 2307 |
-
|
| 2308 |
-
|
| 2309 |
-
|
| 2310 |
-
|
| 2311 |
-
|
| 2312 |
-
|
| 2313 |
-
|
| 2314 |
-
|
| 2315 |
-
|
| 2316 |
-
|
| 2317 |
-
|
| 2318 |
-
*
|
| 2319 |
-
|
| 2320 |
-
|
| 2321 |
-
|
| 2322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2323 |
|
| 2324 |
[*Example 1*:
|
| 2325 |
|
| 2326 |
``` cpp
|
| 2327 |
-
|
| 2328 |
-
|
| 2329 |
-
|
| 2330 |
-
|
| 2331 |
-
|
| 2332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2333 |
}
|
| 2334 |
|
| 2335 |
-
|
| 2336 |
-
|
| 2337 |
A a;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2338 |
|
| 2339 |
-
|
| 2340 |
-
|
| 2341 |
-
|
| 2342 |
-
|
| 2343 |
-
|
|
|
|
|
|
|
|
|
|
| 2344 |
|
| 2345 |
-
|
| 2346 |
-
|
| 2347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2348 |
}
|
| 2349 |
```
|
| 2350 |
|
| 2351 |
-
|
| 2352 |
-
|
| 2353 |
-
|
| 2354 |
-
|
| 2355 |
-
|
| 2356 |
-
|
| 2357 |
-
|
| 2358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2359 |
|
| 2360 |
— *end example*]
|
| 2361 |
|
| 2362 |
-
|
| 2363 |
-
non-local inline variable with static storage duration is sequenced
|
| 2364 |
-
before the first statement of `main` or is deferred. If it is deferred,
|
| 2365 |
-
it strongly happens before any non-initialization odr-use of that
|
| 2366 |
-
variable. It is *implementation-defined* in which threads and at which
|
| 2367 |
-
points in the program such deferred dynamic initialization occurs.
|
| 2368 |
-
|
| 2369 |
-
It is *implementation-defined* whether the dynamic initialization of a
|
| 2370 |
-
non-local non-inline variable with thread storage duration is sequenced
|
| 2371 |
-
before the first statement of the initial function of a thread or is
|
| 2372 |
-
deferred. If it is deferred, the initialization associated with the
|
| 2373 |
-
entity for thread *t* is sequenced before the first non-initialization
|
| 2374 |
-
odr-use by *t* of any non-inline variable with thread storage duration
|
| 2375 |
-
defined in the same translation unit as the variable to be initialized.
|
| 2376 |
-
It is *implementation-defined* in which threads and at which points in
|
| 2377 |
-
the program such deferred dynamic initialization occurs.
|
| 2378 |
-
|
| 2379 |
-
If the initialization of a non-local variable with static or thread
|
| 2380 |
-
storage duration exits via an exception, `std::terminate` is called (
|
| 2381 |
-
[[except.terminate]]).
|
| 2382 |
-
|
| 2383 |
-
### Termination <a id="basic.start.term">[[basic.start.term]]</a>
|
| 2384 |
-
|
| 2385 |
-
Destructors ([[class.dtor]]) for initialized objects (that is, objects
|
| 2386 |
-
whose lifetime ([[basic.life]]) has begun) with static storage
|
| 2387 |
-
duration, and functions registered with `std::atexit`, are called as
|
| 2388 |
-
part of a call to `std::exit` ([[support.start.term]]). The call to
|
| 2389 |
-
`std::exit` is sequenced before the invocations of the destructors and
|
| 2390 |
-
the registered functions.
|
| 2391 |
-
|
| 2392 |
-
[*Note 1*: Returning from `main` invokes `std::exit` (
|
| 2393 |
-
[[basic.start.main]]). — *end note*]
|
| 2394 |
-
|
| 2395 |
-
Destructors for initialized objects with thread storage duration within
|
| 2396 |
-
a given thread are called as a result of returning from the initial
|
| 2397 |
-
function of that thread and as a result of that thread calling
|
| 2398 |
-
`std::exit`. The completions of the destructors for all initialized
|
| 2399 |
-
objects with thread storage duration within that thread strongly happen
|
| 2400 |
-
before the initiation of the destructors of any object with static
|
| 2401 |
-
storage duration.
|
| 2402 |
-
|
| 2403 |
-
If the completion of the constructor or dynamic initialization of an
|
| 2404 |
-
object with static storage duration strongly happens before that of
|
| 2405 |
-
another, the completion of the destructor of the second is sequenced
|
| 2406 |
-
before the initiation of the destructor of the first. If the completion
|
| 2407 |
-
of the constructor or dynamic initialization of an object with thread
|
| 2408 |
-
storage duration is sequenced before that of another, the completion of
|
| 2409 |
-
the destructor of the second is sequenced before the initiation of the
|
| 2410 |
-
destructor of the first. If an object is initialized statically, the
|
| 2411 |
-
object is destroyed in the same order as if the object was dynamically
|
| 2412 |
-
initialized. For an object of array or class type, all subobjects of
|
| 2413 |
-
that object are destroyed before any block-scope object with static
|
| 2414 |
-
storage duration initialized during the construction of the subobjects
|
| 2415 |
-
is destroyed. If the destruction of an object with static or thread
|
| 2416 |
-
storage duration exits via an exception, `std::terminate` is called (
|
| 2417 |
-
[[except.terminate]]).
|
| 2418 |
-
|
| 2419 |
-
If a function contains a block-scope object of static or thread storage
|
| 2420 |
-
duration that has been destroyed and the function is called during the
|
| 2421 |
-
destruction of an object with static or thread storage duration, the
|
| 2422 |
-
program has undefined behavior if the flow of control passes through the
|
| 2423 |
-
definition of the previously destroyed block-scope object. Likewise, the
|
| 2424 |
-
behavior is undefined if the block-scope object is used indirectly
|
| 2425 |
-
(i.e., through a pointer) after its destruction.
|
| 2426 |
-
|
| 2427 |
-
If the completion of the initialization of an object with static storage
|
| 2428 |
-
duration strongly happens before a call to `std::atexit` (see
|
| 2429 |
-
`<cstdlib>`, [[support.start.term]]), the call to the function passed
|
| 2430 |
-
to `std::atexit` is sequenced before the call to the destructor for the
|
| 2431 |
-
object. If a call to `std::atexit` strongly happens before the
|
| 2432 |
-
completion of the initialization of an object with static storage
|
| 2433 |
-
duration, the call to the destructor for the object is sequenced before
|
| 2434 |
-
the call to the function passed to `std::atexit`. If a call to
|
| 2435 |
-
`std::atexit` strongly happens before another call to `std::atexit`, the
|
| 2436 |
-
call to the function passed to the second `std::atexit` call is
|
| 2437 |
-
sequenced before the call to the function passed to the first
|
| 2438 |
-
`std::atexit` call.
|
| 2439 |
-
|
| 2440 |
-
If there is a use of a standard library object or function not permitted
|
| 2441 |
-
within signal handlers ([[support.runtime]]) that does not happen
|
| 2442 |
-
before ([[intro.multithread]]) completion of destruction of objects
|
| 2443 |
-
with static storage duration and execution of `std::atexit` registered
|
| 2444 |
-
functions ([[support.start.term]]), the program has undefined behavior.
|
| 2445 |
-
|
| 2446 |
-
[*Note 2*: If there is a use of an object with static storage duration
|
| 2447 |
-
that does not happen before the object’s destruction, the program has
|
| 2448 |
-
undefined behavior. Terminating every thread before a call to
|
| 2449 |
-
`std::exit` or the exit from `main` is sufficient, but not necessary, to
|
| 2450 |
-
satisfy these requirements. These requirements permit thread managers as
|
| 2451 |
-
static-storage-duration objects. — *end note*]
|
| 2452 |
-
|
| 2453 |
-
Calling the function `std::abort()` declared in `<cstdlib>` terminates
|
| 2454 |
-
the program without executing any destructors and without calling the
|
| 2455 |
-
functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
| 2456 |
-
|
| 2457 |
-
## Storage duration <a id="basic.stc">[[basic.stc]]</a>
|
| 2458 |
|
| 2459 |
The *storage duration* is the property of an object that defines the
|
| 2460 |
minimum potential lifetime of the storage containing the object. The
|
| 2461 |
storage duration is determined by the construct used to create the
|
| 2462 |
object and is one of the following:
|
|
@@ -2465,36 +3148,35 @@ object and is one of the following:
|
|
| 2465 |
- thread storage duration
|
| 2466 |
- automatic storage duration
|
| 2467 |
- dynamic storage duration
|
| 2468 |
|
| 2469 |
Static, thread, and automatic storage durations are associated with
|
| 2470 |
-
objects introduced by declarations
|
| 2471 |
-
|
| 2472 |
-
|
| 2473 |
-
*new-expression* ([[expr.new]]).
|
| 2474 |
|
| 2475 |
The storage duration categories apply to references as well.
|
| 2476 |
|
| 2477 |
When the end of the duration of a region of storage is reached, the
|
| 2478 |
values of all pointers representing the address of any part of that
|
| 2479 |
-
region of storage become invalid pointer values
|
| 2480 |
Indirection through an invalid pointer value and passing an invalid
|
| 2481 |
pointer value to a deallocation function have undefined behavior. Any
|
| 2482 |
other use of an invalid pointer value has *implementation-defined*
|
| 2483 |
-
behavior.[^
|
| 2484 |
|
| 2485 |
-
### Static storage duration <a id="basic.stc.static">[[basic.stc.static]]</a>
|
| 2486 |
|
| 2487 |
All variables which do not have dynamic storage duration, do not have
|
| 2488 |
thread storage duration, and are not local have *static storage
|
| 2489 |
-
duration*. The storage for these entities
|
| 2490 |
-
|
| 2491 |
|
| 2492 |
If a variable with static storage duration has initialization or a
|
| 2493 |
destructor with side effects, it shall not be eliminated even if it
|
| 2494 |
appears to be unused, except that a class object or its copy/move may be
|
| 2495 |
-
eliminated as specified in [[class.copy]].
|
| 2496 |
|
| 2497 |
The keyword `static` can be used to declare a local variable with static
|
| 2498 |
storage duration.
|
| 2499 |
|
| 2500 |
[*Note 1*: [[stmt.dcl]] describes the initialization of local `static`
|
|
@@ -2502,23 +3184,24 @@ variables; [[basic.start.term]] describes the destruction of local
|
|
| 2502 |
`static` variables. — *end note*]
|
| 2503 |
|
| 2504 |
The keyword `static` applied to a class data member in a class
|
| 2505 |
definition gives the data member static storage duration.
|
| 2506 |
|
| 2507 |
-
### Thread storage duration <a id="basic.stc.thread">[[basic.stc.thread]]</a>
|
| 2508 |
|
| 2509 |
-
All variables declared with the `thread_local` keyword have
|
| 2510 |
-
storage duration*. The storage for these entities
|
| 2511 |
duration of the thread in which they are created. There is a distinct
|
| 2512 |
object or reference per thread, and use of the declared name refers to
|
| 2513 |
the entity associated with the current thread.
|
| 2514 |
|
| 2515 |
-
A variable with thread storage duration
|
| 2516 |
-
|
| 2517 |
-
destroyed on thread exit
|
|
|
|
| 2518 |
|
| 2519 |
-
### Automatic storage duration <a id="basic.stc.auto">[[basic.stc.auto]]</a>
|
| 2520 |
|
| 2521 |
Block-scope variables not explicitly declared `static`, `thread_local`,
|
| 2522 |
or `extern` have *automatic storage duration*. The storage for these
|
| 2523 |
entities lasts until the block in which they are created exits.
|
| 2524 |
|
|
@@ -2527,47 +3210,46 @@ in [[stmt.dcl]]. — *end note*]
|
|
| 2527 |
|
| 2528 |
If a variable with automatic storage duration has initialization or a
|
| 2529 |
destructor with side effects, an implementation shall not destroy it
|
| 2530 |
before the end of its block nor eliminate it as an optimization, even if
|
| 2531 |
it appears to be unused, except that a class object or its copy/move may
|
| 2532 |
-
be eliminated as specified in [[class.copy]].
|
| 2533 |
|
| 2534 |
-
### Dynamic storage duration <a id="basic.stc.dynamic">[[basic.stc.dynamic]]</a>
|
| 2535 |
|
| 2536 |
-
Objects can be created dynamically during program execution
|
| 2537 |
-
[[intro.execution]]
|
| 2538 |
-
|
| 2539 |
-
|
| 2540 |
-
|
| 2541 |
new[]` and the global *deallocation functions* `operator
|
| 2542 |
delete` and `operator delete[]`.
|
| 2543 |
|
| 2544 |
[*Note 1*: The non-allocating forms described in
|
| 2545 |
[[new.delete.placement]] do not perform allocation or
|
| 2546 |
deallocation. — *end note*]
|
| 2547 |
|
| 2548 |
The library provides default definitions for the global allocation and
|
| 2549 |
deallocation functions. Some global allocation and deallocation
|
| 2550 |
-
functions are replaceable
|
| 2551 |
-
|
| 2552 |
function. Any such function definition replaces the default version
|
| 2553 |
-
provided in the library
|
| 2554 |
-
allocation and deallocation functions
|
| 2555 |
-
|
| 2556 |
-
program.
|
| 2557 |
|
| 2558 |
``` cpp
|
| 2559 |
-
void* operator new(std::size_t);
|
| 2560 |
-
void* operator new(std::size_t, std::align_val_t);
|
| 2561 |
|
| 2562 |
void operator delete(void*) noexcept;
|
| 2563 |
void operator delete(void*, std::size_t) noexcept;
|
| 2564 |
void operator delete(void*, std::align_val_t) noexcept;
|
| 2565 |
void operator delete(void*, std::size_t, std::align_val_t) noexcept;
|
| 2566 |
|
| 2567 |
-
void* operator new[](std::size_t);
|
| 2568 |
-
void* operator new[](std::size_t, std::align_val_t);
|
| 2569 |
|
| 2570 |
void operator delete[](void*) noexcept;
|
| 2571 |
void operator delete[](void*, std::size_t) noexcept;
|
| 2572 |
void operator delete[](void*, std::align_val_t) noexcept;
|
| 2573 |
void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
|
|
@@ -2578,152 +3260,165 @@ These implicit declarations introduce only the function names `operator`
|
|
| 2578 |
`delete[]`.
|
| 2579 |
|
| 2580 |
[*Note 2*: The implicit declarations do not introduce the names `std`,
|
| 2581 |
`std::size_t`, `std::align_val_t`, or any other names that the library
|
| 2582 |
uses to declare these names. Thus, a *new-expression*,
|
| 2583 |
-
*delete-expression* or function call that refers to one of these
|
| 2584 |
-
functions without including the header `<new>` is
|
| 2585 |
-
referring to `std` or `std::size_t` or
|
| 2586 |
-
unless the name has been declared by
|
| 2587 |
-
header. — *end note*]
|
| 2588 |
|
| 2589 |
Allocation and/or deallocation functions may also be declared and
|
| 2590 |
-
defined for any class
|
| 2591 |
|
| 2592 |
-
|
| 2593 |
-
|
| 2594 |
-
|
| 2595 |
-
[[basic.stc.dynamic.deallocation]].
|
| 2596 |
|
| 2597 |
-
#### Allocation functions <a id="basic.stc.dynamic.allocation">[[basic.stc.dynamic.allocation]]</a>
|
| 2598 |
|
| 2599 |
An allocation function shall be a class member function or a global
|
| 2600 |
function; a program is ill-formed if an allocation function is declared
|
| 2601 |
in a namespace scope other than global scope or declared static in
|
| 2602 |
global scope. The return type shall be `void*`. The first parameter
|
| 2603 |
-
shall have type `std::size_t`
|
| 2604 |
-
shall not have an associated default argument
|
| 2605 |
-
|
| 2606 |
-
|
| 2607 |
-
template
|
| 2608 |
-
|
| 2609 |
-
|
| 2610 |
-
|
| 2611 |
|
| 2612 |
-
|
| 2613 |
-
storage. If it is successful, it
|
| 2614 |
-
|
| 2615 |
-
|
| 2616 |
-
allocated
|
| 2617 |
-
|
| 2618 |
-
|
| 2619 |
-
|
| 2620 |
-
|
| 2621 |
-
|
| 2622 |
-
|
| 2623 |
-
|
| 2624 |
-
|
| 2625 |
-
|
| 2626 |
-
|
| 2627 |
-
|
| 2628 |
-
allocation
|
| 2629 |
-
[[new.delete.
|
| 2630 |
-
|
| 2631 |
-
|
| 2632 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2633 |
|
| 2634 |
An allocation function that fails to allocate storage can invoke the
|
| 2635 |
-
currently installed new-handler function
|
| 2636 |
|
| 2637 |
-
[*Note
|
| 2638 |
address of the currently installed `new_handler` using the
|
| 2639 |
-
`std::get_new_handler` function
|
| 2640 |
|
| 2641 |
-
|
| 2642 |
-
|
| 2643 |
-
|
| 2644 |
-
|
| 2645 |
-
|
| 2646 |
-
|
| 2647 |
|
| 2648 |
A global allocation function is only called as the result of a new
|
| 2649 |
-
expression
|
| 2650 |
-
syntax
|
| 2651 |
-
|
|
|
|
| 2652 |
|
| 2653 |
-
[*Note
|
| 2654 |
-
allocate storage for objects with static storage duration
|
| 2655 |
-
[[basic.stc.static]]
|
| 2656 |
-
duration
|
| 2657 |
-
[[expr.typeid]]
|
| 2658 |
-
[[except.throw]]
|
| 2659 |
|
| 2660 |
-
#### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
|
| 2661 |
|
| 2662 |
Deallocation functions shall be class member functions or global
|
| 2663 |
functions; a program is ill-formed if deallocation functions are
|
| 2664 |
declared in a namespace scope other than global scope or declared static
|
| 2665 |
in global scope.
|
| 2666 |
|
| 2667 |
-
|
| 2668 |
-
|
| 2669 |
-
|
| 2670 |
-
|
| 2671 |
|
| 2672 |
-
|
| 2673 |
-
|
| 2674 |
-
`std::align_val_t` or `std::size_t` [^14]; or
|
| 2675 |
-
- exactly three parameters, the type of the second being `std::size_t`
|
| 2676 |
-
and the type of the third being `std::align_val_t`.
|
| 2677 |
|
| 2678 |
-
|
| 2679 |
-
|
| 2680 |
-
|
|
|
|
|
|
|
|
|
|
| 2681 |
|
| 2682 |
-
|
| 2683 |
-
|
| 2684 |
-
|
| 2685 |
|
| 2686 |
-
A
|
| 2687 |
-
|
| 2688 |
-
|
|
|
|
|
|
|
|
|
|
| 2689 |
|
| 2690 |
If a deallocation function terminates by throwing an exception, the
|
| 2691 |
behavior is undefined. The value of the first argument supplied to a
|
| 2692 |
deallocation function may be a null pointer value; if so, and if the
|
| 2693 |
deallocation function is one supplied in the standard library, the call
|
| 2694 |
has no effect.
|
| 2695 |
|
| 2696 |
If the argument given to a deallocation function in the standard library
|
| 2697 |
-
is a pointer that is not the null pointer value
|
| 2698 |
deallocation function shall deallocate the storage referenced by the
|
| 2699 |
pointer, ending the duration of the region of storage.
|
| 2700 |
|
| 2701 |
-
#### Safely-derived pointers <a id="basic.stc.dynamic.safety">[[basic.stc.dynamic.safety]]</a>
|
| 2702 |
|
| 2703 |
A *traceable pointer object* is
|
| 2704 |
|
| 2705 |
-
- an object of an object pointer type
|
| 2706 |
- an object of an integral type that is at least as large as
|
| 2707 |
`std::intptr_t`, or
|
| 2708 |
-
- a sequence of elements in an array of narrow character type
|
| 2709 |
-
[[basic.fundamental]]
|
| 2710 |
match those of some object pointer type.
|
| 2711 |
|
| 2712 |
-
A pointer value is a *safely-derived pointer* to
|
| 2713 |
-
|
|
|
|
| 2714 |
|
| 2715 |
-
- the value returned by a call to the C++standard library
|
| 2716 |
-
of `::operator new(std::{}size_t)` or
|
| 2717 |
-
`::operator new(std::size_t, std::align_val_t)` ;[^
|
| 2718 |
- the result of taking the address of an object (or one of its
|
| 2719 |
subobjects) designated by an lvalue resulting from indirection through
|
| 2720 |
a safely-derived pointer value;
|
| 2721 |
-
- the result of well-defined pointer arithmetic
|
|
|
|
|
|
|
|
|
|
| 2722 |
safely-derived pointer value;
|
| 2723 |
-
- the result of a well-defined pointer conversion ([[conv.ptr]],
|
| 2724 |
-
[[expr.cast]]) of a safely-derived pointer value;
|
| 2725 |
- the result of a `reinterpret_cast` of a safely-derived pointer value;
|
| 2726 |
- the result of a `reinterpret_cast` of an integer representation of a
|
| 2727 |
safely-derived pointer value;
|
| 2728 |
- the value of an object whose value was copied from a traceable pointer
|
| 2729 |
object, where at the time of the copy the source object contained a
|
|
@@ -2749,299 +3444,418 @@ An implementation may have *relaxed pointer safety*, in which case the
|
|
| 2749 |
validity of a pointer value does not depend on whether it is a
|
| 2750 |
safely-derived pointer value. Alternatively, an implementation may have
|
| 2751 |
*strict pointer safety*, in which case a pointer value referring to an
|
| 2752 |
object with dynamic storage duration that is not a safely-derived
|
| 2753 |
pointer value is an invalid pointer value unless the referenced complete
|
| 2754 |
-
object has previously been declared reachable
|
| 2755 |
-
[[util.dynamic.safety]]).
|
| 2756 |
|
| 2757 |
-
[*Note
|
| 2758 |
-
passing it to a deallocation function) is undefined, see
|
| 2759 |
-
|
| 2760 |
-
|
| 2761 |
-
safely-derived pointer value. — *end note*]
|
| 2762 |
|
| 2763 |
It is *implementation-defined* whether an implementation has relaxed or
|
| 2764 |
strict pointer safety.
|
| 2765 |
|
| 2766 |
-
### Duration of subobjects <a id="basic.stc.inherit">[[basic.stc.inherit]]</a>
|
| 2767 |
|
| 2768 |
The storage duration of subobjects and reference members is that of
|
| 2769 |
-
their complete object
|
| 2770 |
|
| 2771 |
-
##
|
| 2772 |
|
| 2773 |
-
|
| 2774 |
-
|
| 2775 |
-
|
| 2776 |
-
|
| 2777 |
-
|
|
|
|
|
|
|
|
|
|
| 2778 |
|
| 2779 |
-
|
| 2780 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2781 |
|
| 2782 |
-
|
| 2783 |
|
| 2784 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2785 |
and
|
| 2786 |
-
-
|
| 2787 |
-
|
| 2788 |
|
| 2789 |
-
|
| 2790 |
-
lifetime only begins if that union member is the initialized member in
|
| 2791 |
-
the union ([[dcl.init.aggr]], [[class.base.init]]), or as described in
|
| 2792 |
-
[[class.union]]. The lifetime of an object *o* of type `T` ends when:
|
| 2793 |
-
|
| 2794 |
-
- if `T` is a class type with a non-trivial destructor (
|
| 2795 |
-
[[class.dtor]]), the destructor call starts, or
|
| 2796 |
-
- the storage which the object occupies is released, or is reused by an
|
| 2797 |
-
object that is not nested within *o* ([[intro.object]]).
|
| 2798 |
-
|
| 2799 |
-
The lifetime of a reference begins when its initialization is complete.
|
| 2800 |
-
The lifetime of a reference ends as if it were a scalar object.
|
| 2801 |
-
|
| 2802 |
-
[*Note 2*: [[class.base.init]] describes the lifetime of base and
|
| 2803 |
-
member subobjects. — *end note*]
|
| 2804 |
-
|
| 2805 |
-
The properties ascribed to objects and references throughout this
|
| 2806 |
-
International Standard apply for a given object or reference only during
|
| 2807 |
-
its lifetime.
|
| 2808 |
-
|
| 2809 |
-
[*Note 3*: In particular, before the lifetime of an object starts and
|
| 2810 |
-
after its lifetime ends there are significant restrictions on the use of
|
| 2811 |
-
the object, as described below, in [[class.base.init]] and in
|
| 2812 |
-
[[class.cdtor]]. Also, the behavior of an object under construction and
|
| 2813 |
-
destruction might not be the same as the behavior of an object whose
|
| 2814 |
-
lifetime has started and not ended. [[class.base.init]] and
|
| 2815 |
-
[[class.cdtor]] describe the behavior of objects during the construction
|
| 2816 |
-
and destruction phases. — *end note*]
|
| 2817 |
-
|
| 2818 |
-
A program may end the lifetime of any object by reusing the storage
|
| 2819 |
-
which the object occupies or by explicitly calling the destructor for an
|
| 2820 |
-
object of a class type with a non-trivial destructor. For an object of a
|
| 2821 |
-
class type with a non-trivial destructor, the program is not required to
|
| 2822 |
-
call the destructor explicitly before the storage which the object
|
| 2823 |
-
occupies is reused or released; however, if there is no explicit call to
|
| 2824 |
-
the destructor or if a *delete-expression* ([[expr.delete]]) is not
|
| 2825 |
-
used to release the storage, the destructor shall not be implicitly
|
| 2826 |
-
called and any program that depends on the side effects produced by the
|
| 2827 |
-
destructor has undefined behavior.
|
| 2828 |
-
|
| 2829 |
-
Before the lifetime of an object has started but after the storage which
|
| 2830 |
-
the object will occupy has been allocated[^16] or, after the lifetime of
|
| 2831 |
-
an object has ended and before the storage which the object occupied is
|
| 2832 |
-
reused or released, any pointer that represents the address of the
|
| 2833 |
-
storage location where the object will be or was located may be used but
|
| 2834 |
-
only in limited ways. For an object under construction or destruction,
|
| 2835 |
-
see [[class.cdtor]]. Otherwise, such a pointer refers to allocated
|
| 2836 |
-
storage ([[basic.stc.dynamic.deallocation]]), and using the pointer as
|
| 2837 |
-
if the pointer were of type `void*`, is well-defined. Indirection
|
| 2838 |
-
through such a pointer is permitted but the resulting lvalue may only be
|
| 2839 |
-
used in limited ways, as described below. The program has undefined
|
| 2840 |
-
behavior if:
|
| 2841 |
-
|
| 2842 |
-
- the object will be or was of a class type with a non-trivial
|
| 2843 |
-
destructor and the pointer is used as the operand of a
|
| 2844 |
-
*delete-expression*,
|
| 2845 |
-
- the pointer is used to access a non-static data member or call a
|
| 2846 |
-
non-static member function of the object, or
|
| 2847 |
-
- the pointer is implicitly converted ([[conv.ptr]]) to a pointer to a
|
| 2848 |
-
virtual base class, or
|
| 2849 |
-
- the pointer is used as the operand of a `static_cast` (
|
| 2850 |
-
[[expr.static.cast]]), except when the conversion is to pointer to
|
| 2851 |
-
cv `void`, or to pointer to cv `void` and subsequently to pointer to
|
| 2852 |
-
cv `char`, cv `unsigned char`, or cv `std::byte` ([[cstddef.syn]]),
|
| 2853 |
-
or
|
| 2854 |
-
- the pointer is used as the operand of a `dynamic_cast` (
|
| 2855 |
-
[[expr.dynamic.cast]]).
|
| 2856 |
|
| 2857 |
[*Example 1*:
|
| 2858 |
|
|
|
|
|
|
|
| 2859 |
``` cpp
|
| 2860 |
-
|
| 2861 |
-
|
| 2862 |
-
|
| 2863 |
-
|
| 2864 |
-
|
| 2865 |
-
|
| 2866 |
};
|
| 2867 |
|
| 2868 |
-
|
| 2869 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2870 |
|
| 2871 |
-
|
| 2872 |
-
|
| 2873 |
-
f(); // undefined behavior
|
| 2874 |
-
... = this; // OK, this points to valid memory
|
| 2875 |
-
}
|
| 2876 |
|
| 2877 |
-
void
|
| 2878 |
-
|
| 2879 |
-
|
| 2880 |
-
|
| 2881 |
-
|
| 2882 |
-
void* q = pb; // OK: pb points to valid memory
|
| 2883 |
-
pb->f(); // undefined behavior, lifetime of *pb has ended
|
| 2884 |
}
|
| 2885 |
```
|
| 2886 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2887 |
— *end example*]
|
| 2888 |
|
| 2889 |
-
|
| 2890 |
-
|
| 2891 |
-
|
| 2892 |
-
|
| 2893 |
-
object
|
| 2894 |
-
|
| 2895 |
-
|
| 2896 |
-
|
| 2897 |
-
|
| 2898 |
-
|
|
|
|
| 2899 |
|
| 2900 |
-
|
| 2901 |
-
|
| 2902 |
-
object, or
|
| 2903 |
-
- the glvalue is bound to a reference to a virtual base class (
|
| 2904 |
-
[[dcl.init.ref]]), or
|
| 2905 |
-
- the glvalue is used as the operand of a `dynamic_cast` (
|
| 2906 |
-
[[expr.dynamic.cast]]) or as the operand of `typeid`.
|
| 2907 |
|
| 2908 |
-
|
| 2909 |
-
|
| 2910 |
-
|
| 2911 |
-
|
| 2912 |
-
|
| 2913 |
-
|
| 2914 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2915 |
|
| 2916 |
-
|
| 2917 |
-
|
| 2918 |
-
|
| 2919 |
-
|
| 2920 |
-
|
| 2921 |
-
|
| 2922 |
-
|
| 2923 |
-
|
| 2924 |
-
|
| 2925 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2926 |
|
| 2927 |
[*Example 2*:
|
| 2928 |
|
| 2929 |
``` cpp
|
| 2930 |
-
|
| 2931 |
-
int i;
|
| 2932 |
-
void f();
|
| 2933 |
-
const C& operator=( const C& );
|
| 2934 |
-
};
|
| 2935 |
|
| 2936 |
-
|
| 2937 |
-
|
| 2938 |
-
|
| 2939 |
-
|
| 2940 |
-
|
| 2941 |
-
}
|
| 2942 |
-
return *this;
|
| 2943 |
-
}
|
| 2944 |
-
|
| 2945 |
-
C c1;
|
| 2946 |
-
C c2;
|
| 2947 |
-
c1 = c2; // well-defined
|
| 2948 |
-
c1.f(); // well-defined; c1 refers to a new object of type C
|
| 2949 |
```
|
| 2950 |
|
| 2951 |
— *end example*]
|
| 2952 |
|
| 2953 |
-
[*Note
|
| 2954 |
-
can be obtained from a pointer that represents the address of its
|
| 2955 |
-
storage by calling `std::launder` ([[support.dynamic]]). — *end note*]
|
| 2956 |
|
| 2957 |
-
|
| 2958 |
-
|
| 2959 |
-
[[basic.stc.auto]]) storage duration and if `T` has a non-trivial
|
| 2960 |
-
destructor,[^17] the program must ensure that an object of the original
|
| 2961 |
-
type occupies that same storage location when the implicit destructor
|
| 2962 |
-
call takes place; otherwise the behavior of the program is undefined.
|
| 2963 |
-
This is true even if the block is exited with an exception.
|
| 2964 |
|
| 2965 |
[*Example 3*:
|
| 2966 |
|
| 2967 |
``` cpp
|
| 2968 |
-
|
| 2969 |
-
struct B {
|
| 2970 |
-
~B();
|
| 2971 |
-
};
|
| 2972 |
-
|
| 2973 |
-
void h() {
|
| 2974 |
-
B b;
|
| 2975 |
-
new (&b) T;
|
| 2976 |
-
} // undefined behavior at block exit
|
| 2977 |
```
|
| 2978 |
|
| 2979 |
— *end example*]
|
| 2980 |
|
| 2981 |
-
|
| 2982 |
-
|
| 2983 |
-
|
| 2984 |
-
|
|
|
|
|
|
|
|
|
|
| 2985 |
|
| 2986 |
[*Example 4*:
|
| 2987 |
|
| 2988 |
``` cpp
|
| 2989 |
-
struct
|
| 2990 |
-
|
| 2991 |
-
~B();
|
| 2992 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2993 |
|
| 2994 |
-
|
| 2995 |
|
| 2996 |
-
|
| 2997 |
-
|
| 2998 |
-
|
| 2999 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3000 |
```
|
| 3001 |
|
| 3002 |
— *end example*]
|
| 3003 |
|
| 3004 |
-
|
| 3005 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3006 |
|
| 3007 |
-
|
| 3008 |
-
|
| 3009 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3010 |
|
| 3011 |
## Types <a id="basic.types">[[basic.types]]</a>
|
| 3012 |
|
| 3013 |
[*Note 1*: [[basic.types]] and the subclauses thereof impose
|
| 3014 |
requirements on implementations regarding the representation of types.
|
| 3015 |
There are two kinds of types: fundamental types and compound types.
|
| 3016 |
-
Types describe objects
|
| 3017 |
-
|
| 3018 |
|
| 3019 |
-
For any object (other than a
|
| 3020 |
-
type `T`, whether or not the object holds a valid
|
| 3021 |
-
underlying bytes
|
| 3022 |
-
into an array of `char`, `unsigned char`, or
|
| 3023 |
-
[[cstddef.syn]]
|
| 3024 |
-
the object, the object shall subsequently hold its
|
|
|
|
| 3025 |
|
| 3026 |
[*Example 1*:
|
| 3027 |
|
| 3028 |
``` cpp
|
| 3029 |
-
|
| 3030 |
char buf[N];
|
| 3031 |
T obj; // obj initialized to its original value
|
| 3032 |
std::memcpy(buf, &obj, N); // between these two calls to std::memcpy, obj might be modified
|
| 3033 |
std::memcpy(&obj, buf, N); // at this point, each subobject of obj of scalar type holds its original value
|
| 3034 |
```
|
| 3035 |
|
| 3036 |
— *end example*]
|
| 3037 |
|
| 3038 |
For any trivially copyable type `T`, if two pointers to `T` point to
|
| 3039 |
distinct `T` objects `obj1` and `obj2`, where neither `obj1` nor `obj2`
|
| 3040 |
-
is a
|
| 3041 |
-
making up `obj1` are copied into `obj2`,[^19] `obj2`
|
| 3042 |
-
hold the same value as `obj1`.
|
| 3043 |
|
| 3044 |
[*Example 2*:
|
| 3045 |
|
| 3046 |
``` cpp
|
| 3047 |
T* t1p;
|
|
@@ -3054,21 +3868,23 @@ std::memcpy(t1p, t2p, sizeof(T));
|
|
| 3054 |
|
| 3055 |
— *end example*]
|
| 3056 |
|
| 3057 |
The *object representation* of an object of type `T` is the sequence of
|
| 3058 |
*N* `unsigned char` objects taken up by the object of type `T`, where
|
| 3059 |
-
*N* equals `sizeof(T)`. The *value representation* of an object
|
| 3060 |
-
set of bits that
|
| 3061 |
-
|
| 3062 |
-
representation
|
| 3063 |
-
|
|
|
|
|
|
|
| 3064 |
|
| 3065 |
A class that has been declared but not defined, an enumeration type in
|
| 3066 |
-
certain contexts
|
| 3067 |
incomplete element type, is an *incompletely-defined object type*. [^21]
|
| 3068 |
-
Incompletely-defined object types and cv `void` are *incomplete types*
|
| 3069 |
-
[[basic.fundamental]]
|
| 3070 |
incomplete type.
|
| 3071 |
|
| 3072 |
A class type (such as “`class X`”) might be incomplete at one point in a
|
| 3073 |
translation unit and complete later on; the type “`class X`” is the same
|
| 3074 |
type at both points. The declared type of an array object might be an
|
|
@@ -3092,24 +3908,24 @@ extern int arr[]; // the type of arr is incomplete
|
|
| 3092 |
typedef int UNKA[]; // UNKA is an incomplete type
|
| 3093 |
UNKA* arrp; // arrp is a pointer to an incomplete type
|
| 3094 |
UNKA** arrpp;
|
| 3095 |
|
| 3096 |
void foo() {
|
| 3097 |
-
xp++; //
|
| 3098 |
-
arrp++; //
|
| 3099 |
arrpp++; // OK: sizeof UNKA* is known
|
| 3100 |
}
|
| 3101 |
|
| 3102 |
struct X { int i; }; // now X is a complete type
|
| 3103 |
int arr[10]; // now the type of arr is complete
|
| 3104 |
|
| 3105 |
X x;
|
| 3106 |
void bar() {
|
| 3107 |
xp = &x; // OK; type is ``pointer to X''
|
| 3108 |
-
arrp = &arr; //
|
| 3109 |
xp++; // OK: X is complete
|
| 3110 |
-
arrp++; //
|
| 3111 |
}
|
| 3112 |
```
|
| 3113 |
|
| 3114 |
— *end example*]
|
| 3115 |
|
|
@@ -3117,603 +3933,1507 @@ void bar() {
|
|
| 3117 |
contexts incomplete types are prohibited. — *end note*]
|
| 3118 |
|
| 3119 |
An *object type* is a (possibly cv-qualified) type that is not a
|
| 3120 |
function type, not a reference type, and not cv `void`.
|
| 3121 |
|
| 3122 |
-
Arithmetic types
|
| 3123 |
-
types, pointer
|
| 3124 |
-
|
| 3125 |
-
collectively called *scalar types*. Scalar types,
|
| 3126 |
-
[[class]]
|
| 3127 |
-
types are collectively called *
|
| 3128 |
-
|
| 3129 |
-
types
|
| 3130 |
-
*
|
| 3131 |
-
[[class]]
|
| 3132 |
-
types are collectively called *
|
| 3133 |
-
|
| 3134 |
-
|
| 3135 |
-
*
|
| 3136 |
|
| 3137 |
A type is a *literal type* if it is:
|
| 3138 |
|
| 3139 |
-
-
|
| 3140 |
- a scalar type; or
|
| 3141 |
- a reference type; or
|
| 3142 |
- an array of literal type; or
|
| 3143 |
-
- a possibly cv-qualified class type
|
| 3144 |
-
|
| 3145 |
-
- it has a
|
| 3146 |
-
- it is either a closure type
|
| 3147 |
-
aggregate type
|
| 3148 |
-
constructor or constructor template (possibly inherited
|
| 3149 |
-
[[namespace.udecl]]
|
| 3150 |
constructor,
|
| 3151 |
- if it is a union, at least one of its non-static data members is of
|
| 3152 |
non-volatile literal type, and
|
| 3153 |
- if it is not a union, all of its non-static data members and base
|
| 3154 |
classes are of non-volatile literal types.
|
| 3155 |
|
| 3156 |
[*Note 3*: A literal type is one for which it might be possible to
|
| 3157 |
create an object within a constant expression. It is not a guarantee
|
| 3158 |
that it is possible to create such an object, nor is it a guarantee that
|
| 3159 |
-
any object of that type will usable in a constant
|
| 3160 |
expression. — *end note*]
|
| 3161 |
|
| 3162 |
Two types *cv1* `T1` and *cv2* `T2` are *layout-compatible* types if
|
| 3163 |
-
`T1` and `T2` are the same type, layout-compatible enumerations
|
| 3164 |
-
[[dcl.enum]]
|
| 3165 |
-
[[class.mem]]
|
| 3166 |
|
| 3167 |
### Fundamental types <a id="basic.fundamental">[[basic.fundamental]]</a>
|
| 3168 |
|
| 3169 |
-
Objects declared as characters (`char`) shall be large enough to store
|
| 3170 |
-
any member of the implementation’s basic character set. If a character
|
| 3171 |
-
from this set is stored in a character object, the integral value of
|
| 3172 |
-
that character object is equal to the value of the single character
|
| 3173 |
-
literal form of that character. It is *implementation-defined* whether a
|
| 3174 |
-
`char` object can hold negative values. Characters can be explicitly
|
| 3175 |
-
declared `unsigned` or `signed`. Plain `char`, `signed char`, and
|
| 3176 |
-
`unsigned char` are three distinct types, collectively called *narrow
|
| 3177 |
-
character types*. A `char`, a `signed char`, and an `unsigned char`
|
| 3178 |
-
occupy the same amount of storage and have the same alignment
|
| 3179 |
-
requirements ([[basic.align]]); that is, they have the same object
|
| 3180 |
-
representation. For narrow character types, all bits of the object
|
| 3181 |
-
representation participate in the value representation.
|
| 3182 |
-
|
| 3183 |
-
[*Note 1*: A bit-field of narrow character type whose length is larger
|
| 3184 |
-
than the number of bits in the object representation of that type has
|
| 3185 |
-
padding bits; see [[class.bit]]. — *end note*]
|
| 3186 |
-
|
| 3187 |
-
For unsigned narrow character types, each possible bit pattern of the
|
| 3188 |
-
value representation represents a distinct number. These requirements do
|
| 3189 |
-
not hold for other types. In any particular implementation, a plain
|
| 3190 |
-
`char` object can take on either the same values as a `signed char` or
|
| 3191 |
-
an `unsigned
|
| 3192 |
-
char`; which one is *implementation-defined*. For each value *i* of type
|
| 3193 |
-
`unsigned char` in the range 0 to 255 inclusive, there exists a value
|
| 3194 |
-
*j* of type `char` such that the result of an integral conversion (
|
| 3195 |
-
[[conv.integral]]) from *i* to `char` is *j*, and the result of an
|
| 3196 |
-
integral conversion from *j* to `unsigned char` is *i*.
|
| 3197 |
-
|
| 3198 |
There are five *standard signed integer types* : “`signed char`”,
|
| 3199 |
“`short int`”, “`int`”, “`long int`”, and “`long long int`”. In this
|
| 3200 |
list, each type provides at least as much storage as those preceding it
|
| 3201 |
in the list. There may also be *implementation-defined* *extended signed
|
| 3202 |
integer types*. The standard and extended signed integer types are
|
| 3203 |
-
collectively called *signed integer types*.
|
| 3204 |
-
|
| 3205 |
-
|
| 3206 |
-
|
|
|
|
|
|
|
|
|
|
| 3207 |
|
| 3208 |
For each of the standard signed integer types, there exists a
|
| 3209 |
corresponding (but different) *standard unsigned integer type*:
|
| 3210 |
“`unsigned char`”, “`unsigned short int`”, “`unsigned int`”,
|
| 3211 |
-
“`unsigned long int`”, and “`unsigned long long int`”,
|
| 3212 |
-
|
| 3213 |
-
|
| 3214 |
-
|
| 3215 |
-
|
| 3216 |
-
|
| 3217 |
-
|
| 3218 |
-
|
| 3219 |
-
|
| 3220 |
-
|
| 3221 |
-
|
| 3222 |
-
|
| 3223 |
-
|
| 3224 |
-
|
| 3225 |
-
|
| 3226 |
-
|
| 3227 |
-
|
| 3228 |
-
|
| 3229 |
-
|
| 3230 |
-
|
| 3231 |
-
|
| 3232 |
-
|
| 3233 |
-
|
| 3234 |
-
|
| 3235 |
-
|
| 3236 |
-
|
| 3237 |
-
|
| 3238 |
-
|
| 3239 |
-
|
| 3240 |
-
|
| 3241 |
-
|
| 3242 |
-
|
| 3243 |
-
|
| 3244 |
-
|
| 3245 |
-
|
| 3246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3247 |
types or values. — *end note*]
|
| 3248 |
|
| 3249 |
-
|
| 3250 |
-
|
|
|
|
| 3251 |
|
| 3252 |
-
|
| 3253 |
-
|
| 3254 |
-
|
| 3255 |
-
representations of integral types shall define values by use of a pure
|
| 3256 |
-
binary numeration system.[^27]
|
| 3257 |
|
| 3258 |
-
|
| 3259 |
-
ones’ complement and signed magnitude representations for integral
|
| 3260 |
-
types. — *end example*]
|
| 3261 |
-
|
| 3262 |
-
There are three *floating-point* types: `float`, `double`, and
|
| 3263 |
`long double`. The type `double` provides at least as much precision as
|
| 3264 |
`float`, and the type `long double` provides at least as much precision
|
| 3265 |
as `double`. The set of values of the type `float` is a subset of the
|
| 3266 |
set of values of the type `double`; the set of values of the type
|
| 3267 |
`double` is a subset of the set of values of the type `long double`. The
|
| 3268 |
value representation of floating-point types is
|
| 3269 |
*implementation-defined*.
|
| 3270 |
|
| 3271 |
-
[*Note
|
| 3272 |
-
|
| 3273 |
-
[[support.limits]]. — *end note*]
|
| 3274 |
|
| 3275 |
-
Integral and floating types are collectively called *arithmetic*
|
| 3276 |
-
Specializations of the standard library template
|
| 3277 |
-
[[support.limits]]
|
| 3278 |
-
arithmetic type for an implementation.
|
| 3279 |
|
| 3280 |
A type cv `void` is an incomplete type that cannot be completed; such a
|
| 3281 |
type has an empty set of values. It is used as the return type for
|
| 3282 |
functions that do not return a value. Any expression can be explicitly
|
| 3283 |
-
converted to type cv `void` ([[expr.
|
| 3284 |
-
cv `void` shall be used only as an
|
| 3285 |
-
[[stmt.expr]]
|
| 3286 |
-
as a second or third operand of `?:`
|
| 3287 |
-
`typeid`, `noexcept`, or `decltype`, as the expression in
|
| 3288 |
-
statement
|
| 3289 |
cv `void`, or as the operand of an explicit conversion to type
|
| 3290 |
cv `void`.
|
| 3291 |
|
| 3292 |
-
A value of type `std::nullptr_t` is a null pointer constant
|
| 3293 |
-
[[conv.ptr]]
|
| 3294 |
-
member conversions ([[conv.ptr]], [[conv.mem]]).
|
| 3295 |
`sizeof(std::nullptr_t)` shall be equal to `sizeof(void*)`.
|
| 3296 |
|
| 3297 |
-
|
| 3298 |
-
|
| 3299 |
-
|
|
|
|
|
|
|
| 3300 |
|
| 3301 |
### Compound types <a id="basic.compound">[[basic.compound]]</a>
|
| 3302 |
|
| 3303 |
Compound types can be constructed in the following ways:
|
| 3304 |
|
| 3305 |
-
- *arrays* of objects of a given type,
|
| 3306 |
- *functions*, which have parameters of given types and return `void` or
|
| 3307 |
-
references or objects of a given type,
|
| 3308 |
- *pointers* to cv `void` or objects or functions (including static
|
| 3309 |
-
members of classes) of a given type,
|
| 3310 |
-
- *references* to objects or functions of a given type,
|
| 3311 |
There are two types of references:
|
| 3312 |
-
-
|
| 3313 |
-
-
|
| 3314 |
-
- *classes* containing a sequence of objects of various types
|
| 3315 |
-
|
| 3316 |
-
|
| 3317 |
-
|
| 3318 |
-
[[class.access]]);
|
| 3319 |
- *unions*, which are classes capable of containing objects of different
|
| 3320 |
-
types at different times,
|
| 3321 |
- *enumerations*, which comprise a set of named constant values. Each
|
| 3322 |
-
distinct enumeration constitutes a different *enumerated type*,
|
| 3323 |
[[dcl.enum]];
|
| 3324 |
-
- *pointers to non-static class members*, [^
|
| 3325 |
-
of a given type within objects of a given class,
|
|
|
|
|
|
|
| 3326 |
|
| 3327 |
These methods of constructing types can be applied recursively;
|
| 3328 |
-
restrictions are mentioned in [[dcl.
|
| 3329 |
-
|
| 3330 |
-
|
| 3331 |
-
|
| 3332 |
|
| 3333 |
The type of a pointer to cv `void` or a pointer to an object type is
|
| 3334 |
called an *object pointer type*.
|
| 3335 |
|
| 3336 |
[*Note 1*: A pointer to `void` does not have a pointer-to-object type,
|
| 3337 |
however, because `void` is not an object type. — *end note*]
|
| 3338 |
|
| 3339 |
The type of a pointer that can designate a function is called a
|
| 3340 |
-
*function pointer type*. A pointer to
|
| 3341 |
-
as a “pointer to `T`”.
|
| 3342 |
|
| 3343 |
[*Example 1*: A pointer to an object of type `int` is referred to as
|
| 3344 |
“pointer to `int`” and a pointer to an object of class `X` is called a
|
| 3345 |
“pointer to `X`”. — *end example*]
|
| 3346 |
|
| 3347 |
Except for pointers to static members, text referring to “pointers” does
|
| 3348 |
not apply to pointers to members. Pointers to incomplete types are
|
| 3349 |
-
allowed although there are restrictions on what can be done with them
|
| 3350 |
-
[[basic.align]]
|
| 3351 |
|
| 3352 |
- a *pointer to* an object or function (the pointer is said to *point*
|
| 3353 |
to the object or function), or
|
| 3354 |
-
- a *pointer past the end of* an object
|
| 3355 |
-
- the *null pointer value*
|
| 3356 |
- an *invalid pointer value*.
|
| 3357 |
|
| 3358 |
A value of a pointer type that is a pointer to or past the end of an
|
| 3359 |
-
object *represents the address* of the first byte in memory
|
| 3360 |
-
[[intro.memory]]
|
| 3361 |
memory after the end of the storage occupied by the object,
|
| 3362 |
respectively.
|
| 3363 |
|
| 3364 |
-
[*Note 2*: A pointer past the end of an object
|
| 3365 |
considered to point to an unrelated object of the object’s type that
|
| 3366 |
might be located at that address. A pointer value becomes invalid when
|
| 3367 |
the storage it denotes reaches the end of its storage duration; see
|
| 3368 |
[[basic.stc]]. — *end note*]
|
| 3369 |
|
| 3370 |
-
For purposes of pointer arithmetic
|
| 3371 |
[[expr.rel]], [[expr.eq]]), a pointer past the end of the last element
|
| 3372 |
of an array `x` of n elements is considered to be equivalent to a
|
| 3373 |
-
pointer to a hypothetical element `x
|
| 3374 |
-
|
| 3375 |
-
|
| 3376 |
-
|
|
|
|
|
|
|
| 3377 |
|
| 3378 |
-
[*Note 3*: Pointers to over-aligned types
|
| 3379 |
special representation, but their range of valid values is restricted by
|
| 3380 |
the extended alignment requirement. — *end note*]
|
| 3381 |
|
| 3382 |
Two objects *a* and *b* are *pointer-interconvertible* if:
|
| 3383 |
|
| 3384 |
- they are the same object, or
|
| 3385 |
-
- one is a
|
| 3386 |
-
|
| 3387 |
- one is a standard-layout class object and the other is the first
|
| 3388 |
non-static data member of that object, or, if the object has no
|
| 3389 |
-
non-static data members,
|
| 3390 |
-
|
| 3391 |
- there exists an object *c* such that *a* and *c* are
|
| 3392 |
pointer-interconvertible, and *c* and *b* are
|
| 3393 |
pointer-interconvertible.
|
| 3394 |
|
| 3395 |
If two objects are pointer-interconvertible, then they have the same
|
| 3396 |
address, and it is possible to obtain a pointer to one from a pointer to
|
| 3397 |
-
the other via a `reinterpret_cast`
|
| 3398 |
|
| 3399 |
[*Note 4*: An array object and its first element are not
|
| 3400 |
pointer-interconvertible, even though they have the same
|
| 3401 |
address. — *end note*]
|
| 3402 |
|
| 3403 |
-
A pointer to cv
|
| 3404 |
-
|
| 3405 |
-
|
| 3406 |
-
|
| 3407 |
-
cv `char*`.
|
| 3408 |
|
| 3409 |
### CV-qualifiers <a id="basic.type.qualifier">[[basic.type.qualifier]]</a>
|
| 3410 |
|
| 3411 |
A type mentioned in [[basic.fundamental]] and [[basic.compound]] is a
|
| 3412 |
*cv-unqualified type*. Each type which is a cv-unqualified complete or
|
| 3413 |
-
incomplete object type or is `void`
|
| 3414 |
corresponding cv-qualified versions of its type: a *const-qualified*
|
| 3415 |
version, a *volatile-qualified* version, and a
|
| 3416 |
-
*const-volatile-qualified* version. The type of an object
|
| 3417 |
-
[[intro.object]]
|
| 3418 |
-
*decl-specifier-seq*
|
| 3419 |
-
[[dcl.
|
| 3420 |
-
[[expr.new]]) when the object is created.
|
| 3421 |
|
| 3422 |
- A *const object* is an object of type `const T` or a non-mutable
|
| 3423 |
-
subobject of
|
| 3424 |
-
- A *volatile object* is an object of type `volatile T`
|
| 3425 |
-
|
| 3426 |
- A *const volatile object* is an object of type `const volatile T`, a
|
| 3427 |
-
non-mutable subobject of
|
| 3428 |
-
volatile object, or a non-mutable volatile subobject of a const
|
| 3429 |
object.
|
| 3430 |
|
| 3431 |
The cv-qualified or cv-unqualified versions of a type are distinct
|
| 3432 |
types; however, they shall have the same representation and alignment
|
| 3433 |
-
requirements
|
| 3434 |
|
| 3435 |
-
|
| 3436 |
-
cv-qualifiers (if any) of the types from which it is
|
| 3437 |
-
|
| 3438 |
-
[[dcl.array]]).
|
| 3439 |
|
| 3440 |
-
|
| 3441 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3442 |
|
| 3443 |
There is a partial ordering on cv-qualifiers, so that a type can be said
|
| 3444 |
-
to be *more cv-qualified* than another.
|
| 3445 |
-
|
| 3446 |
-
constitute this ordering.
|
| 3447 |
|
| 3448 |
-
**Table: Relations on `const` and `volatile`** <a id="
|
| 3449 |
|
| 3450 |
| | | |
|
| 3451 |
| --------------- | --- | ---------------- |
|
| 3452 |
| no cv-qualifier | < | `const` |
|
| 3453 |
| no cv-qualifier | < | `volatile` |
|
| 3454 |
| no cv-qualifier | < | `const volatile` |
|
| 3455 |
| `const` | < | `const volatile` |
|
| 3456 |
| `volatile` | < | `const volatile` |
|
| 3457 |
|
| 3458 |
|
| 3459 |
-
In this
|
| 3460 |
-
|
| 3461 |
-
|
| 3462 |
-
|
| 3463 |
-
|
| 3464 |
|
| 3465 |
-
[*Example
|
| 3466 |
no top-level cv-qualifiers. The type corresponding to the *type-id*
|
| 3467 |
`volatile int * const` has the top-level cv-qualifier `const`. For a
|
| 3468 |
class type `C`, the type corresponding to the *type-id*
|
| 3469 |
`void (C::* volatile)(int) const` has the top-level cv-qualifier
|
| 3470 |
`volatile`. — *end example*]
|
| 3471 |
|
| 3472 |
-
|
| 3473 |
-
|
| 3474 |
-
|
| 3475 |
-
|
| 3476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3477 |
|
| 3478 |
[*Example 2*:
|
| 3479 |
|
| 3480 |
``` cpp
|
| 3481 |
-
|
| 3482 |
-
|
| 3483 |
-
|
| 3484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3485 |
```
|
| 3486 |
|
| 3487 |
-
|
| 3488 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3489 |
|
| 3490 |
— *end example*]
|
| 3491 |
|
| 3492 |
-
|
| 3493 |
-
|
| 3494 |
-
|
| 3495 |
-
|
| 3496 |
-
|
| 3497 |
-
|
| 3498 |
-
|
| 3499 |
-
|
| 3500 |
-
|
| 3501 |
-
|
| 3502 |
-
|
| 3503 |
-
|
| 3504 |
-
|
| 3505 |
-
|
| 3506 |
-
|
| 3507 |
-
|
| 3508 |
-
|
| 3509 |
-
|
| 3510 |
-
|
| 3511 |
-
|
| 3512 |
-
|
| 3513 |
-
|
| 3514 |
-
|
| 3515 |
-
|
| 3516 |
-
|
| 3517 |
-
|
| 3518 |
-
|
| 3519 |
-
|
| 3520 |
-
|
| 3521 |
-
|
| 3522 |
-
|
| 3523 |
-
|
| 3524 |
-
|
| 3525 |
-
|
| 3526 |
-
|
| 3527 |
-
|
| 3528 |
-
of
|
| 3529 |
-
|
| 3530 |
-
|
| 3531 |
-
|
| 3532 |
-
|
| 3533 |
-
|
| 3534 |
-
|
| 3535 |
-
|
| 3536 |
-
|
| 3537 |
-
|
| 3538 |
-
|
| 3539 |
-
|
| 3540 |
-
|
| 3541 |
-
|
| 3542 |
-
|
| 3543 |
-
|
| 3544 |
-
|
| 3545 |
-
|
| 3546 |
-
|
| 3547 |
-
|
| 3548 |
-
|
| 3549 |
-
|
| 3550 |
-
|
| 3551 |
-
|
| 3552 |
-
|
| 3553 |
-
|
| 3554 |
-
|
| 3555 |
-
|
| 3556 |
-
|
| 3557 |
-
|
| 3558 |
-
|
| 3559 |
-
|
| 3560 |
-
|
| 3561 |
-
|
| 3562 |
-
|
| 3563 |
-
|
| 3564 |
-
|
| 3565 |
-
|
| 3566 |
-
|
| 3567 |
-
|
| 3568 |
-
|
| 3569 |
-
|
| 3570 |
-
[
|
| 3571 |
-
|
| 3572 |
-
|
| 3573 |
-
|
| 3574 |
-
|
| 3575 |
-
|
| 3576 |
-
|
| 3577 |
-
|
| 3578 |
-
|
| 3579 |
-
|
| 3580 |
-
|
| 3581 |
-
|
| 3582 |
-
|
| 3583 |
-
|
| 3584 |
-
|
| 3585 |
-
|
| 3586 |
-
|
| 3587 |
-
|
| 3588 |
-
|
| 3589 |
-
|
| 3590 |
-
|
| 3591 |
-
|
| 3592 |
-
|
| 3593 |
-
|
| 3594 |
-
|
| 3595 |
-
|
| 3596 |
-
|
| 3597 |
-
|
| 3598 |
-
|
| 3599 |
-
|
| 3600 |
-
|
| 3601 |
-
|
| 3602 |
-
|
| 3603 |
-
|
| 3604 |
-
|
| 3605 |
-
|
| 3606 |
-
|
| 3607 |
-
|
| 3608 |
-
|
| 3609 |
-
|
| 3610 |
-
|
| 3611 |
-
|
| 3612 |
-
|
| 3613 |
-
|
| 3614 |
-
|
| 3615 |
-
|
| 3616 |
-
|
| 3617 |
-
|
| 3618 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3619 |
|
| 3620 |
[*Example 1*:
|
| 3621 |
|
| 3622 |
``` cpp
|
| 3623 |
-
|
| 3624 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3625 |
```
|
| 3626 |
|
| 3627 |
-
|
| 3628 |
-
|
| 3629 |
-
`
|
| 3630 |
-
|
| 3631 |
-
|
|
|
|
|
|
|
|
|
|
| 3632 |
|
| 3633 |
— *end example*]
|
| 3634 |
|
| 3635 |
-
|
| 3636 |
-
|
| 3637 |
-
|
| 3638 |
-
|
| 3639 |
-
|
| 3640 |
-
|
| 3641 |
-
|
| 3642 |
-
|
| 3643 |
-
|
| 3644 |
-
|
| 3645 |
-
|
| 3646 |
-
|
| 3647 |
-
|
| 3648 |
-
|
| 3649 |
-
|
| 3650 |
-
|
| 3651 |
-
|
| 3652 |
-
|
| 3653 |
-
|
| 3654 |
-
|
| 3655 |
-
|
| 3656 |
-
|
| 3657 |
-
|
| 3658 |
-
|
| 3659 |
-
|
| 3660 |
-
|
| 3661 |
-
|
| 3662 |
-
|
| 3663 |
-
|
| 3664 |
-
|
| 3665 |
-
|
| 3666 |
-
|
| 3667 |
-
|
| 3668 |
-
|
| 3669 |
-
|
| 3670 |
-
|
| 3671 |
-
|
| 3672 |
-
|
| 3673 |
-
|
| 3674 |
-
|
| 3675 |
-
|
| 3676 |
-
|
| 3677 |
-
|
| 3678 |
-
|
| 3679 |
-
|
| 3680 |
-
|
| 3681 |
-
|
| 3682 |
-
|
| 3683 |
-
|
| 3684 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3685 |
|
| 3686 |
<!-- Link reference definitions -->
|
| 3687 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3688 |
[basic]: #basic
|
| 3689 |
[basic.align]: #basic.align
|
| 3690 |
[basic.compound]: #basic.compound
|
| 3691 |
[basic.def]: #basic.def
|
| 3692 |
[basic.def.odr]: #basic.def.odr
|
|
|
|
| 3693 |
[basic.fundamental]: #basic.fundamental
|
|
|
|
| 3694 |
[basic.funscope]: #basic.funscope
|
|
|
|
| 3695 |
[basic.life]: #basic.life
|
| 3696 |
[basic.link]: #basic.link
|
| 3697 |
[basic.lookup]: #basic.lookup
|
| 3698 |
[basic.lookup.argdep]: #basic.lookup.argdep
|
| 3699 |
[basic.lookup.classref]: #basic.lookup.classref
|
| 3700 |
[basic.lookup.elab]: #basic.lookup.elab
|
| 3701 |
[basic.lookup.qual]: #basic.lookup.qual
|
| 3702 |
[basic.lookup.udir]: #basic.lookup.udir
|
| 3703 |
[basic.lookup.unqual]: #basic.lookup.unqual
|
| 3704 |
-
[basic.lval]: #basic.lval
|
|
|
|
| 3705 |
[basic.namespace]: dcl.md#basic.namespace
|
|
|
|
| 3706 |
[basic.scope]: #basic.scope
|
| 3707 |
[basic.scope.block]: #basic.scope.block
|
| 3708 |
[basic.scope.class]: #basic.scope.class
|
| 3709 |
[basic.scope.declarative]: #basic.scope.declarative
|
| 3710 |
[basic.scope.enum]: #basic.scope.enum
|
| 3711 |
[basic.scope.hiding]: #basic.scope.hiding
|
| 3712 |
[basic.scope.namespace]: #basic.scope.namespace
|
|
|
|
| 3713 |
[basic.scope.pdecl]: #basic.scope.pdecl
|
| 3714 |
-
[basic.scope.proto]: #basic.scope.proto
|
| 3715 |
[basic.scope.temp]: #basic.scope.temp
|
| 3716 |
[basic.start]: #basic.start
|
| 3717 |
[basic.start.dynamic]: #basic.start.dynamic
|
| 3718 |
[basic.start.main]: #basic.start.main
|
| 3719 |
[basic.start.static]: #basic.start.static
|
|
@@ -3726,292 +5446,320 @@ not supported by an implementation, the program is ill-formed.
|
|
| 3726 |
[basic.stc.dynamic.safety]: #basic.stc.dynamic.safety
|
| 3727 |
[basic.stc.inherit]: #basic.stc.inherit
|
| 3728 |
[basic.stc.static]: #basic.stc.static
|
| 3729 |
[basic.stc.thread]: #basic.stc.thread
|
| 3730 |
[basic.type.qualifier]: #basic.type.qualifier
|
|
|
|
| 3731 |
[basic.types]: #basic.types
|
|
|
|
|
|
|
| 3732 |
[class]: class.md#class
|
|
|
|
| 3733 |
[class.access]: class.md#class.access
|
| 3734 |
-
[class.base.init]:
|
| 3735 |
[class.bit]: class.md#class.bit
|
| 3736 |
-
[class.cdtor]:
|
| 3737 |
-
[class.conv.fct]:
|
| 3738 |
-
[class.copy]:
|
| 3739 |
-
[class.ctor]:
|
|
|
|
|
|
|
| 3740 |
[class.derived]: class.md#class.derived
|
| 3741 |
-
[class.dtor]:
|
| 3742 |
-
[class.free]:
|
| 3743 |
[class.friend]: class.md#class.friend
|
| 3744 |
[class.local]: class.md#class.local
|
| 3745 |
[class.mem]: class.md#class.mem
|
| 3746 |
[class.member.lookup]: class.md#class.member.lookup
|
| 3747 |
[class.mfct]: class.md#class.mfct
|
| 3748 |
[class.mfct.non-static]: class.md#class.mfct.non-static
|
| 3749 |
[class.name]: class.md#class.name
|
| 3750 |
[class.nest]: class.md#class.nest
|
|
|
|
|
|
|
| 3751 |
[class.qual]: #class.qual
|
|
|
|
| 3752 |
[class.static]: class.md#class.static
|
| 3753 |
[class.static.data]: class.md#class.static.data
|
| 3754 |
-
[class.temporary]:
|
| 3755 |
[class.this]: class.md#class.this
|
| 3756 |
[class.union]: class.md#class.union
|
| 3757 |
-
[
|
| 3758 |
-
[conv
|
| 3759 |
-
[conv.
|
| 3760 |
-
[conv.
|
| 3761 |
-
[conv.
|
| 3762 |
-
[conv.
|
| 3763 |
-
[conv.
|
| 3764 |
-
[conv.
|
| 3765 |
-
[conv.
|
| 3766 |
-
[conv.
|
|
|
|
| 3767 |
[cpp.predefined]: cpp.md#cpp.predefined
|
| 3768 |
-
[cstddef.syn]:
|
|
|
|
| 3769 |
[dcl.align]: dcl.md#dcl.align
|
| 3770 |
[dcl.array]: dcl.md#dcl.array
|
|
|
|
|
|
|
|
|
|
| 3771 |
[dcl.dcl]: dcl.md#dcl.dcl
|
| 3772 |
[dcl.decl]: dcl.md#dcl.decl
|
| 3773 |
[dcl.enum]: dcl.md#dcl.enum
|
| 3774 |
[dcl.fct]: dcl.md#dcl.fct
|
| 3775 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
|
|
|
|
|
|
| 3776 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 3777 |
[dcl.init]: dcl.md#dcl.init
|
| 3778 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
|
|
|
| 3779 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
| 3780 |
[dcl.inline]: dcl.md#dcl.inline
|
| 3781 |
[dcl.link]: dcl.md#dcl.link
|
|
|
|
| 3782 |
[dcl.mptr]: dcl.md#dcl.mptr
|
| 3783 |
[dcl.name]: dcl.md#dcl.name
|
|
|
|
| 3784 |
[dcl.ptr]: dcl.md#dcl.ptr
|
| 3785 |
[dcl.ref]: dcl.md#dcl.ref
|
| 3786 |
[dcl.spec]: dcl.md#dcl.spec
|
|
|
|
| 3787 |
[dcl.stc]: dcl.md#dcl.stc
|
|
|
|
| 3788 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 3789 |
-
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 3790 |
[dcl.typedef]: dcl.md#dcl.typedef
|
| 3791 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3792 |
[diff.cpp11.basic]: compatibility.md#diff.cpp11.basic
|
| 3793 |
-
[
|
| 3794 |
[except.handle]: except.md#except.handle
|
|
|
|
| 3795 |
[except.spec]: except.md#except.spec
|
| 3796 |
[except.terminate]: except.md#except.terminate
|
| 3797 |
[except.throw]: except.md#except.throw
|
| 3798 |
[expr]: expr.md#expr
|
| 3799 |
[expr.add]: expr.md#expr.add
|
| 3800 |
[expr.alignof]: expr.md#expr.alignof
|
|
|
|
| 3801 |
[expr.ass]: expr.md#expr.ass
|
|
|
|
| 3802 |
[expr.call]: expr.md#expr.call
|
| 3803 |
[expr.cast]: expr.md#expr.cast
|
| 3804 |
[expr.comma]: expr.md#expr.comma
|
|
|
|
| 3805 |
[expr.cond]: expr.md#expr.cond
|
| 3806 |
[expr.const]: expr.md#expr.const
|
|
|
|
|
|
|
| 3807 |
[expr.delete]: expr.md#expr.delete
|
| 3808 |
[expr.dynamic.cast]: expr.md#expr.dynamic.cast
|
| 3809 |
[expr.eq]: expr.md#expr.eq
|
|
|
|
|
|
|
| 3810 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 3811 |
[expr.new]: expr.md#expr.new
|
| 3812 |
-
[expr.
|
| 3813 |
-
[expr.pre.incr]: expr.md#expr.pre.incr
|
| 3814 |
-
[expr.prim]: expr.md#expr.prim
|
| 3815 |
[expr.prim.id]: expr.md#expr.prim.id
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3816 |
[expr.prim.lambda.closure]: expr.md#expr.prim.lambda.closure
|
| 3817 |
-
[expr.
|
|
|
|
| 3818 |
[expr.ref]: expr.md#expr.ref
|
| 3819 |
[expr.reinterpret.cast]: expr.md#expr.reinterpret.cast
|
| 3820 |
[expr.rel]: expr.md#expr.rel
|
| 3821 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 3822 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 3823 |
[expr.sub]: expr.md#expr.sub
|
| 3824 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 3825 |
[expr.typeid]: expr.md#expr.typeid
|
| 3826 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 3827 |
-
[
|
| 3828 |
[headers]: library.md#headers
|
| 3829 |
-
[intro.execution]:
|
| 3830 |
-
[intro.memory]:
|
| 3831 |
-
[intro.multithread]:
|
| 3832 |
-
[intro.object]:
|
| 3833 |
-
[intro.
|
| 3834 |
-
[
|
|
|
|
| 3835 |
[lex.name]: lex.md#lex.name
|
|
|
|
| 3836 |
[locale]: localization.md#locale
|
| 3837 |
[meta.trans.other]: utilities.md#meta.trans.other
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3838 |
[multibyte.strings]: library.md#multibyte.strings
|
| 3839 |
-
[namespace.alias]: dcl.md#namespace.alias
|
| 3840 |
[namespace.def]: dcl.md#namespace.def
|
| 3841 |
[namespace.memdef]: dcl.md#namespace.memdef
|
| 3842 |
[namespace.qual]: #namespace.qual
|
| 3843 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 3844 |
[namespace.udir]: dcl.md#namespace.udir
|
| 3845 |
-
[new.delete]:
|
| 3846 |
-
[new.delete.array]:
|
| 3847 |
-
[new.delete.placement]:
|
| 3848 |
-
[new.delete.single]:
|
| 3849 |
-
[new.handler]:
|
| 3850 |
[over]: over.md#over
|
| 3851 |
[over.literal]: over.md#over.literal
|
| 3852 |
-
[over.load]: over.md#over.load
|
| 3853 |
[over.match]: over.md#over.match
|
| 3854 |
[over.oper]: over.md#over.oper
|
| 3855 |
[over.over]: over.md#over.over
|
| 3856 |
[ptr.align]: utilities.md#ptr.align
|
|
|
|
| 3857 |
[replacement.functions]: library.md#replacement.functions
|
| 3858 |
-
[
|
| 3859 |
[stmt.block]: stmt.md#stmt.block
|
| 3860 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 3861 |
[stmt.expr]: stmt.md#stmt.expr
|
| 3862 |
[stmt.goto]: stmt.md#stmt.goto
|
| 3863 |
[stmt.if]: stmt.md#stmt.if
|
| 3864 |
[stmt.label]: stmt.md#stmt.label
|
|
|
|
| 3865 |
[stmt.return]: stmt.md#stmt.return
|
| 3866 |
-
[
|
| 3867 |
-
[support.
|
| 3868 |
-
[support.
|
| 3869 |
-
[support.
|
| 3870 |
-
[support.
|
| 3871 |
-
[support.types]: language.md#support.types
|
| 3872 |
-
[tab:relations.on.const.and.volatile]: #tab:relations.on.const.and.volatile
|
| 3873 |
-
[temp]: temp.md#temp
|
| 3874 |
-
[temp.class.spec]: temp.md#temp.class.spec
|
| 3875 |
[temp.deduct.guide]: temp.md#temp.deduct.guide
|
| 3876 |
[temp.dep]: temp.md#temp.dep
|
|
|
|
| 3877 |
[temp.expl.spec]: temp.md#temp.expl.spec
|
| 3878 |
[temp.explicit]: temp.md#temp.explicit
|
| 3879 |
-
[temp.fct]: temp.md#temp.fct
|
| 3880 |
[temp.local]: temp.md#temp.local
|
| 3881 |
-
[temp.mem.func]: temp.md#temp.mem.func
|
| 3882 |
[temp.names]: temp.md#temp.names
|
| 3883 |
[temp.nondep]: temp.md#temp.nondep
|
| 3884 |
[temp.over]: temp.md#temp.over
|
| 3885 |
-
[temp.over.link]: temp.md#temp.over.link
|
| 3886 |
[temp.param]: temp.md#temp.param
|
| 3887 |
[temp.point]: temp.md#temp.point
|
|
|
|
| 3888 |
[temp.res]: temp.md#temp.res
|
| 3889 |
[temp.spec]: temp.md#temp.spec
|
| 3890 |
-
[temp.static]: temp.md#temp.static
|
| 3891 |
[temp.type]: temp.md#temp.type
|
|
|
|
|
|
|
|
|
|
| 3892 |
[thread.threads]: thread.md#thread.threads
|
| 3893 |
[util.dynamic.safety]: utilities.md#util.dynamic.safety
|
| 3894 |
|
| 3895 |
-
[^1]: Appearing inside the
|
| 3896 |
*linkage-specification* does not affect whether a declaration is a
|
| 3897 |
definition.
|
| 3898 |
|
| 3899 |
[^2]: An implementation is not required to call allocation and
|
| 3900 |
deallocation functions from constructors or destructors; however,
|
| 3901 |
this is a permissible implementation technique.
|
| 3902 |
|
| 3903 |
-
[^3]:
|
| 3904 |
-
looked up.
|
| 3905 |
-
|
| 3906 |
-
[^4]: This refers to unqualified names that occur, for instance, in a
|
| 3907 |
type or default argument in the *parameter-declaration-clause* or
|
| 3908 |
used in the function body.
|
| 3909 |
|
| 3910 |
-
[^
|
| 3911 |
-
name may be used in
|
| 3912 |
-
definition.
|
| 3913 |
|
| 3914 |
-
[^
|
| 3915 |
`Y`’s definition or whether `X`’s definition appears in a namespace
|
| 3916 |
-
scope enclosing `Y`’s definition
|
| 3917 |
|
| 3918 |
-
[^
|
| 3919 |
in the *parameter-declaration-clause* or in the
|
| 3920 |
*noexcept-specifier*.
|
| 3921 |
|
| 3922 |
-
[^
|
| 3923 |
the definition of class `X` or whether the member function is
|
| 3924 |
defined in a namespace scope enclosing `X`’s definition.
|
| 3925 |
|
| 3926 |
-
[^
|
| 3927 |
appearing in a *nested-name-specifier*, an
|
| 3928 |
*elaborated-type-specifier*, or a *base-specifier*.
|
| 3929 |
|
| 3930 |
-
[^
|
| 3931 |
-
|
| 3932 |
|
| 3933 |
-
[^
|
| 3934 |
-
|
| 3935 |
-
|
| 3936 |
-
[[basic.stc.static]]).
|
| 3937 |
|
| 3938 |
-
[^
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3939 |
value causes a system-generated runtime fault.
|
| 3940 |
|
| 3941 |
-
[^
|
| 3942 |
`std::malloc()` or `std::calloc()`, so the rules are substantially
|
| 3943 |
the same. C++ differs from C in requiring a zero request to return a
|
| 3944 |
non-null pointer.
|
| 3945 |
|
| 3946 |
-
[^
|
| 3947 |
an allocation function `void operator new(std::size_t, std::size_t)`
|
| 3948 |
-
as a placement allocation function
|
| 3949 |
|
| 3950 |
-
[^
|
| 3951 |
-
pointers to memory not allocated by `::operator new`. This
|
| 3952 |
-
the ability of many C++implementations to use binary
|
| 3953 |
-
components written in other languages. In particular,
|
| 3954 |
-
to C binaries, because indirection through pointers to
|
| 3955 |
-
allocated by `std::malloc` is not restricted.
|
| 3956 |
|
| 3957 |
-
[^
|
| 3958 |
-
|
| 3959 |
|
| 3960 |
-
[^
|
| 3961 |
-
implicitly—upon exit from the block for an object with automatic
|
| 3962 |
-
storage duration, upon exit from the thread for an object with
|
| 3963 |
-
thread storage duration, or upon exit from the program for an object
|
| 3964 |
-
with static storage duration.
|
| 3965 |
-
|
| 3966 |
-
[^18]: By using, for example, the library functions ([[headers]])
|
| 3967 |
`std::memcpy` or `std::memmove`.
|
| 3968 |
|
| 3969 |
-
[^19]: By using, for example, the library functions
|
| 3970 |
`std::memcpy` or `std::memmove`.
|
| 3971 |
|
| 3972 |
-
[^20]: The intent is that the memory model of C++is compatible with
|
| 3973 |
-
of ISO/IEC 9899 Programming Language C.
|
| 3974 |
|
| 3975 |
[^21]: The size and layout of an instance of an incompletely-defined
|
| 3976 |
object type is unknown.
|
| 3977 |
|
| 3978 |
-
[^22]:
|
| 3979 |
-
\[`INT_MIN`, `INT_MAX`\], as defined in the header `<climits>`.
|
| 3980 |
|
| 3981 |
-
[^23]:
|
| 3982 |
-
types and the sequences of *type-specifier*s that designate them.
|
| 3983 |
-
|
| 3984 |
-
[^24]: This implies that unsigned arithmetic does not overflow because a
|
| 3985 |
-
result that cannot be represented by the resulting unsigned integer
|
| 3986 |
-
type is reduced modulo the number that is one greater than the
|
| 3987 |
-
largest value that can be represented by the resulting unsigned
|
| 3988 |
-
integer type.
|
| 3989 |
-
|
| 3990 |
-
[^25]: Using a `bool` value in ways described by this International
|
| 3991 |
-
Standard as “undefined”, such as by examining the value of an
|
| 3992 |
-
uninitialized automatic object, might cause it to behave as if it is
|
| 3993 |
-
neither `true` nor `false`.
|
| 3994 |
-
|
| 3995 |
-
[^26]: Therefore, enumerations ([[dcl.enum]]) are not integral;
|
| 3996 |
-
however, enumerations can be promoted to integral types as specified
|
| 3997 |
-
in [[conv.prom]].
|
| 3998 |
-
|
| 3999 |
-
[^27]: A positional representation for integers that uses the binary
|
| 4000 |
-
digits 0 and 1, in which the values represented by successive bits
|
| 4001 |
-
are additive, begin with 1, and are multiplied by successive
|
| 4002 |
-
integral power of 2, except perhaps for the bit with the highest
|
| 4003 |
-
position. (Adapted from the *American National Dictionary for
|
| 4004 |
-
Information Processing Systems*.)
|
| 4005 |
-
|
| 4006 |
-
[^28]: Static class members are objects or functions, and pointers to
|
| 4007 |
them are ordinary pointers to objects or functions.
|
| 4008 |
|
| 4009 |
-
[^
|
| 4010 |
byte in memory that it will occupy or used to occupy.
|
| 4011 |
|
| 4012 |
-
[^
|
| 4013 |
imply interchangeability as arguments to functions, return values
|
| 4014 |
from functions, and non-static data members of unions.
|
| 4015 |
|
| 4016 |
-
[^
|
| 4017 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Basics <a id="basic">[[basic]]</a>
|
| 2 |
+
|
| 3 |
+
## Preamble <a id="basic.pre">[[basic.pre]]</a>
|
| 4 |
|
| 5 |
[*Note 1*: This Clause presents the basic concepts of the C++ language.
|
| 6 |
It explains the difference between an object and a name and how they
|
| 7 |
relate to the value categories for expressions. It introduces the
|
| 8 |
concepts of a declaration and a definition and presents C++’s notion of
|
|
|
|
| 13 |
|
| 14 |
[*Note 2*: This Clause does not cover concepts that affect only a
|
| 15 |
single part of the language. Such concepts are discussed in the relevant
|
| 16 |
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 a use of an *identifier* [[lex.name]],
|
| 23 |
+
*operator-function-id* [[over.oper]], *literal-operator-id*
|
| 24 |
+
[[over.literal]], *conversion-function-id* [[class.conv.fct]], or
|
| 25 |
+
*template-id* [[temp.names]] that denotes an entity or label (
|
| 26 |
[[stmt.goto]], [[stmt.label]]).
|
| 27 |
|
| 28 |
Every name that denotes an entity is introduced by a *declaration*.
|
| 29 |
Every name that denotes a label is introduced either by a `goto`
|
| 30 |
+
statement [[stmt.goto]] or a *labeled-statement* [[stmt.label]].
|
| 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 |
|
| 36 |
+
A *local entity* is a variable with automatic storage duration
|
| 37 |
+
[[basic.stc.auto]], a structured binding [[dcl.struct.bind]] whose
|
| 38 |
+
corresponding variable is such an entity, or the `*this` object
|
| 39 |
+
[[expr.prim.this]].
|
| 40 |
+
|
| 41 |
Some names denote types or templates. In general, whenever a name is
|
| 42 |
encountered it is necessary to determine whether that name denotes one
|
| 43 |
of these entities before continuing to parse the program that contains
|
| 44 |
+
it. The process that determines this is called *name lookup*
|
| 45 |
+
[[basic.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 the same type, or
|
| 52 |
- they are *template-id*s that refer to the same class, function, or
|
| 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 into a
|
| 64 |
+
translation unit or redeclare names introduced by previous declarations.
|
| 65 |
+
If so, the declaration specifies the interpretation and semantic
|
| 66 |
+
properties of these names. A declaration may also have effects
|
| 67 |
including:
|
| 68 |
|
| 69 |
+
- a static assertion [[dcl.pre]],
|
| 70 |
+
- controlling template instantiation [[temp.explicit]],
|
| 71 |
+
- guiding template argument deduction for constructors
|
| 72 |
+
[[temp.deduct.guide]],
|
| 73 |
+
- use of attributes [[dcl.attr]], and
|
| 74 |
- nothing (in the case of an *empty-declaration*).
|
| 75 |
|
| 76 |
+
Each entity declared by a *declaration* is also *defined* by that
|
| 77 |
+
declaration unless:
|
| 78 |
|
| 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 introduced by an *elaborated-type-specifier* [[class.name]],
|
| 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]],
|
| 95 |
+
- it is an *alias-declaration* [[dcl.typedef]],
|
| 96 |
+
- it is a *using-declaration* [[namespace.udecl]],
|
| 97 |
+
- it is a *deduction-guide* [[temp.deduct.guide]],
|
| 98 |
+
- it is a *static_assert-declaration* [[dcl.pre]],
|
| 99 |
+
- it is an *attribute-declaration* [[dcl.pre]],
|
| 100 |
+
- it is an *empty-declaration* [[dcl.pre]],
|
| 101 |
+
- it is a *using-directive* [[namespace.udir]],
|
| 102 |
+
- it is a *using-enum-declaration* [[enum.udecl]],
|
| 103 |
+
- it is a *template-declaration* [[temp.pre]] whose *template-head* is
|
| 104 |
+
not followed by either a *concept-definition* or a *declaration* that
|
| 105 |
+
defines a function, a class, a variable, or a static data member.
|
| 106 |
+
- it is an explicit instantiation declaration [[temp.explicit]], or
|
| 107 |
+
- it is an explicit specialization [[temp.expl.spec]] whose
|
| 108 |
*declaration* is not a definition.
|
| 109 |
|
| 110 |
+
A declaration is said to be a *definition* of each entity that it
|
| 111 |
+
defines.
|
| 112 |
+
|
| 113 |
[*Example 1*:
|
| 114 |
|
| 115 |
All but one of the following are definitions:
|
| 116 |
|
| 117 |
``` cpp
|
|
|
|
| 143 |
using N::d; // declares d
|
| 144 |
```
|
| 145 |
|
| 146 |
— *end example*]
|
| 147 |
|
| 148 |
+
[*Note 1*: In some circumstances, C++ implementations implicitly
|
| 149 |
+
define the default constructor [[class.default.ctor]], copy constructor,
|
| 150 |
+
move constructor [[class.copy.ctor]], copy assignment operator, move
|
| 151 |
+
assignment operator [[class.copy.assign]], or destructor [[class.dtor]]
|
| 152 |
+
member functions. — *end note*]
|
| 153 |
|
| 154 |
[*Example 2*:
|
| 155 |
|
| 156 |
Given
|
| 157 |
|
| 158 |
``` cpp
|
| 159 |
#include <string>
|
| 160 |
|
| 161 |
struct C {
|
| 162 |
+
std::string s; // std::string is the standard library class[string.classes]
|
| 163 |
};
|
| 164 |
|
| 165 |
int main() {
|
| 166 |
C a;
|
| 167 |
C b = a;
|
|
|
|
| 187 |
```
|
| 188 |
|
| 189 |
— *end example*]
|
| 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 [[basic.types]], an abstract class type
|
| 196 |
+
[[class.abstract]], or a (possibly multi-dimensional) array thereof.
|
| 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 |
+
variable, function, class type, enumeration type, template, default
|
| 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.prop]], a subexpression thereof, or a
|
| 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.
|
| 212 |
+
- If E is a subscripting operation [[expr.sub]] with an array operand,
|
| 213 |
+
the set contains the potential results of that operand.
|
| 214 |
+
- If E is a class member access expression [[expr.ref]] of the form E₁
|
| 215 |
+
`.` `template`ₒₚₜ E₂ naming a non-static data member, the set
|
| 216 |
+
contains the potential results of E₁.
|
| 217 |
+
- If E is a class member access expression naming a static data member,
|
| 218 |
+
the set contains the *id-expression* designating the data member.
|
| 219 |
+
- If E is a pointer-to-member expression [[expr.mptr.oper]] of the form
|
| 220 |
+
E₁ `.*` E₂, the set contains the potential results of E₁.
|
| 221 |
+
- If E has the form `(E₁)`, the set contains the potential results of
|
| 222 |
+
E₁.
|
| 223 |
+
- If E is a glvalue conditional expression [[expr.cond]], the set is the
|
| 224 |
+
union of the sets of potential results of the second and third
|
| 225 |
operands.
|
| 226 |
+
- If E is a comma expression [[expr.comma]], the set contains the
|
| 227 |
potential results of the right operand.
|
| 228 |
- Otherwise, the set is empty.
|
| 229 |
|
| 230 |
[*Note 1*:
|
| 231 |
|
| 232 |
This set is a (possibly-empty) set of *id-expression*s, each of which is
|
| 233 |
+
either E or a subexpression of E.
|
| 234 |
|
| 235 |
[*Example 1*:
|
| 236 |
|
| 237 |
In the following example, the set of potential results of the
|
| 238 |
initializer of `n` contains the first `S::x` subexpression, but not the
|
|
|
|
| 247 |
|
| 248 |
— *end example*]
|
| 249 |
|
| 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 ([[basic.lookup]], [[over.match]],
|
| 256 |
+
[[over.over]]) in an overload resolution performed as part of forming
|
| 257 |
+
that expression or conversion, unless it is a pure virtual function
|
| 258 |
+
and either the expression is not an *id-expression* naming the
|
| 259 |
+
function with an explicitly qualified name or the expression forms a
|
| 260 |
+
pointer to member [[expr.unary.op]]. \[*Note 2*: This covers taking
|
| 261 |
+
the address of functions ([[conv.func]], [[expr.unary.op]]), calls to
|
| 262 |
+
named functions [[expr.call]], operator overloading [[over]],
|
| 263 |
+
user-defined conversions [[class.conv.fct]], allocation functions for
|
| 264 |
+
*new-expression*s [[expr.new]], as well as non-default initialization
|
| 265 |
+
[[dcl.init]]. A constructor selected to copy or move an object of
|
| 266 |
+
class type is considered to be named by an expression or conversion
|
| 267 |
+
even if the call is actually elided by the implementation
|
| 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 `x` whose name appears as a potentially-evaluated expression
|
| 278 |
+
E is *odr-used* by E unless
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 [[expr.prop]]
|
| 289 |
+
to which the lvalue-to-rvalue conversion is not applied.
|
| 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 ([[class.mfct.non-static]])).
|
| 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
|
| 302 |
non-placement deallocation function for a class is odr-used by the
|
| 303 |
definition of the destructor of that class, or by being selected by the
|
| 304 |
+
lookup at the point of definition of a virtual destructor
|
| 305 |
+
[[class.dtor]].[^2]
|
| 306 |
+
|
| 307 |
+
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 declarative region
|
| 314 |
+
[[basic.scope.declarative]] if:
|
| 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 declarative region [[basic.scope.declarative]]
|
| 321 |
+
between the point at which the entity is introduced and the region
|
| 322 |
+
(where `*this` is considered to be introduced within the innermost
|
| 323 |
+
enclosing class or non-lambda function definition scope), either:
|
| 324 |
+
- the intervening declarative region is a block scope, or
|
| 325 |
+
- the intervening declarative region is the function parameter scope
|
| 326 |
+
of a *lambda-expression* that has a *simple-capture* naming the
|
| 327 |
+
entity or has a *capture-default*, and the block scope of the
|
| 328 |
+
*lambda-expression* is also an intervening declarative region.
|
| 329 |
+
|
| 330 |
+
If a local entity is odr-used in a declarative region in which it is not
|
| 331 |
+
odr-usable, the program is ill-formed.
|
| 332 |
+
|
| 333 |
+
[*Example 2*:
|
| 334 |
+
|
| 335 |
+
``` cpp
|
| 336 |
+
void f(int n) {
|
| 337 |
+
[] { n = 1; }; // error: n is not odr-usable due to intervening lambda-expression
|
| 338 |
+
struct A {
|
| 339 |
+
void f() { n = 2; } // error: n is not odr-usable due to intervening function definition scope
|
| 340 |
+
};
|
| 341 |
+
void g(int = n); // error: n is not odr-usable due to intervening function parameter scope
|
| 342 |
+
[=](int k = n) {}; // error: n is not odr-usable due to being
|
| 343 |
+
// outside the block scope of the lambda-expression
|
| 344 |
+
[&] { [n]{ return n; }; }; // OK
|
| 345 |
+
}
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
— *end example*]
|
| 349 |
|
| 350 |
Every program shall contain exactly one definition of every non-inline
|
| 351 |
function or variable that is odr-used in that program outside of a
|
| 352 |
+
discarded statement [[stmt.if]]; no diagnostic required. The definition
|
| 353 |
+
can appear explicitly in the program, it can be found in the standard or
|
| 354 |
+
a user-defined library, or (when appropriate) it is implicitly defined
|
| 355 |
+
(see [[class.default.ctor]], [[class.copy.ctor]], [[class.dtor]], and
|
| 356 |
+
[[class.copy.assign]]).
|
| 357 |
+
|
| 358 |
+
[*Example 3*:
|
| 359 |
+
|
| 360 |
+
``` cpp
|
| 361 |
+
auto f() {
|
| 362 |
+
struct A {};
|
| 363 |
+
return A{};
|
| 364 |
+
}
|
| 365 |
+
decltype(f()) g();
|
| 366 |
+
auto x = g();
|
| 367 |
+
```
|
| 368 |
+
|
| 369 |
+
A program containing this translation unit is ill-formed because `g` is
|
| 370 |
+
odr-used but not defined, and cannot be defined in any other translation
|
| 371 |
+
unit because the local class `A` cannot be named outside this
|
| 372 |
+
translation unit.
|
| 373 |
+
|
| 374 |
+
— *end example*]
|
| 375 |
+
|
| 376 |
+
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 is required to be reachable in every context in
|
| 383 |
+
which the class is used in a way that requires the class type to be
|
| 384 |
+
complete.
|
| 385 |
+
|
| 386 |
+
[*Example 4*:
|
| 387 |
|
| 388 |
The following complete translation unit is well-formed, even though it
|
| 389 |
never defines `X`:
|
| 390 |
|
| 391 |
``` cpp
|
|
|
|
| 399 |
[*Note 3*:
|
| 400 |
|
| 401 |
The rules for declarations and expressions describe in which contexts
|
| 402 |
complete class types are required. A class type `T` must be complete if:
|
| 403 |
|
| 404 |
+
- an object of type `T` is defined [[basic.def]], or
|
| 405 |
+
- a non-static class data member of type `T` is declared [[class.mem]],
|
| 406 |
+
or
|
| 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` ([[conv]], [[expr.type.conv]], [[expr.dynamic.cast]],
|
| 413 |
[[expr.static.cast]], [[expr.cast]]), or
|
| 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`
|
| 419 |
+
[[expr.ref]], or
|
| 420 |
+
- the `typeid` operator [[expr.typeid]] or the `sizeof` operator
|
| 421 |
+
[[expr.sizeof]] is applied to an operand of type `T`, or
|
| 422 |
+
- a function with a return type or argument type of type `T` is defined
|
| 423 |
+
[[basic.def]] or called [[expr.call]], or
|
| 424 |
+
- a class with a base class of type `T` is defined [[class.derived]], or
|
| 425 |
+
- an lvalue of type `T` is assigned to [[expr.ass]], or
|
| 426 |
+
- the type `T` is the subject of an `alignof` expression
|
| 427 |
+
[[expr.alignof]], or
|
|
|
|
|
|
|
| 428 |
- an *exception-declaration* has type `T`, reference to `T`, or pointer
|
| 429 |
+
to `T` [[except.handle]].
|
| 430 |
|
| 431 |
— *end note*]
|
| 432 |
|
| 433 |
+
There can be more than one definition of a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
|
| 435 |
+
- class type [[class]],
|
| 436 |
+
- enumeration type [[dcl.enum]],
|
| 437 |
+
- inline function or variable [[dcl.inline]],
|
| 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 |
+
in a program provided that each definition appears in a different
|
| 444 |
+
translation unit and the definitions satisfy the following requirements.
|
| 445 |
+
Given such an entity `D` defined in more than one translation unit, for
|
| 446 |
+
all definitions of `D`, or, if `D` is an unnamed enumeration, for all
|
| 447 |
+
definitions of `D` that are reachable at any given program point, the
|
| 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
|
| 454 |
+
sequence of tokens of the corresponding *lambda-expression*.
|
| 455 |
+
- In each such definition, corresponding names, looked up according to
|
| 456 |
+
[[basic.lookup]], shall refer to the same entity, after overload
|
| 457 |
+
resolution [[over.match]] and after matching of partial template
|
| 458 |
+
specialization [[temp.over]], except that a name can refer to
|
| 459 |
+
- a non-volatile const object with internal or no linkage if the
|
| 460 |
object
|
| 461 |
- has the same literal type in all definitions of `D`,
|
| 462 |
+
- is initialized with a constant expression [[expr.const]],
|
| 463 |
- is not odr-used in any definition of `D`, and
|
| 464 |
- has the same value in all definitions of `D`,
|
| 465 |
|
| 466 |
or
|
| 467 |
- a reference with internal or no linkage initialized with a constant
|
| 468 |
expression such that the reference refers to the same entity in all
|
| 469 |
+
definitions of `D`.
|
| 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]]), it is as if the
|
| 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
|
| 492 |
// translation unit 1:
|
| 493 |
struct X {
|
| 494 |
X(int, int);
|
| 495 |
X(int, int, int);
|
|
|
|
| 512 |
D d2; // X(int, int, int) called by D();
|
| 513 |
// D()'s implicit definition violates the ODR
|
| 514 |
```
|
| 515 |
|
| 516 |
— *end example*]
|
| 517 |
+
- If `D` is a class with a defaulted three-way comparison operator
|
| 518 |
+
function [[class.spaceship]], it is as if the operator was implicitly
|
| 519 |
+
defined in every translation unit where it is odr-used, and the
|
| 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 |
+
[[temp.nondep]], and also to dependent names at the point of
|
| 527 |
+
instantiation [[temp.dep]]. These requirements also apply to
|
| 528 |
+
corresponding entities defined within each definition of `D` (including
|
| 529 |
+
the closure types of *lambda-expression*s, but excluding entities
|
| 530 |
+
defined within default arguments or default template arguments of either
|
| 531 |
+
`D` or an entity not defined within `D`). For each such entity and for
|
| 532 |
+
`D` itself, the behavior is as if there is a single entity with a single
|
| 533 |
+
definition, including in the application of these requirements to other
|
| 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 |
+
may result in the different declarations having distinct types, and
|
| 540 |
+
*lambda-expression*s appearing in a default argument of `D` may still
|
| 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, []{});
|
| 553 |
+
}
|
| 554 |
+
inline void g(bool cond, void (*p)() = []{}) {
|
| 555 |
+
if (cond) g(false);
|
| 556 |
+
}
|
| 557 |
+
struct X {
|
| 558 |
+
void h(bool cond, void (*p)() = []{}) {
|
| 559 |
+
if (cond) h(false);
|
| 560 |
+
}
|
| 561 |
+
};
|
| 562 |
+
```
|
| 563 |
+
|
| 564 |
+
If the definition of `f` appears in multiple translation units, the
|
| 565 |
+
behavior of the program is as if there is only one definition of `f`. If
|
| 566 |
+
the definition of `g` appears in multiple translation units, the program
|
| 567 |
+
is ill-formed (no diagnostic required) because each such definition uses
|
| 568 |
+
a default argument that refers to a distinct *lambda-expression* closure
|
| 569 |
+
type. The definition of `X` can appear in multiple translation units of
|
| 570 |
+
a valid program; the *lambda-expression*s defined within the default
|
| 571 |
+
argument of `X::h` within the definition of `X` denote the same closure
|
| 572 |
+
type in each translation unit.
|
| 573 |
+
|
| 574 |
+
— *end example*]
|
| 575 |
+
|
| 576 |
+
If, at any point in the program, there is more than one reachable
|
| 577 |
+
unnamed enumeration definition in the same scope that have the same
|
| 578 |
+
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 |
### Declarative regions and scopes <a id="basic.scope.declarative">[[basic.scope.declarative]]</a>
|
| 585 |
|
| 586 |
Every name is introduced in some portion of program text called a
|
| 587 |
*declarative region*, which is the largest part of the program in which
|
| 588 |
+
that name is valid, that is, in which that name may be used as an
|
| 589 |
unqualified name to refer to the same entity. In general, each
|
| 590 |
particular name is valid only within some possibly discontiguous portion
|
| 591 |
of program text called its *scope*. To determine the scope of a
|
| 592 |
declaration, it is sometimes convenient to refer to the *potential
|
| 593 |
scope* of a declaration. The scope of a declaration is the same as its
|
|
|
|
| 620 |
|
| 621 |
— *end example*]
|
| 622 |
|
| 623 |
The names declared by a declaration are introduced into the scope in
|
| 624 |
which the declaration occurs, except that the presence of a `friend`
|
| 625 |
+
specifier [[class.friend]], certain uses of the
|
| 626 |
+
*elaborated-type-specifier* [[dcl.type.elab]], and *using-directive*s
|
| 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 *point of declaration* for a name is immediately after its complete
|
| 664 |
+
declarator [[dcl.decl]] and before its *initializer* (if any), except as
|
| 665 |
+
noted below.
|
| 666 |
|
| 667 |
[*Example 1*:
|
| 668 |
|
| 669 |
``` cpp
|
| 670 |
unsigned char x = 12;
|
| 671 |
{ unsigned char x = x; }
|
| 672 |
```
|
| 673 |
|
| 674 |
+
Here, the initialization of the second `x` has undefined behavior,
|
| 675 |
+
because the initializer accesses the second `x` outside its lifetime
|
| 676 |
+
[[basic.life]].
|
| 677 |
|
| 678 |
— *end example*]
|
| 679 |
|
| 680 |
[*Note 1*:
|
| 681 |
|
| 682 |
+
A name from an outer scope remains visible up to the point of
|
| 683 |
declaration of the name that hides it.
|
| 684 |
|
| 685 |
[*Example 2*:
|
| 686 |
|
| 687 |
``` cpp
|
|
|
|
| 695 |
|
| 696 |
— *end note*]
|
| 697 |
|
| 698 |
The point of declaration for a class or class template first declared by
|
| 699 |
a *class-specifier* is immediately after the *identifier* or
|
| 700 |
+
*simple-template-id* (if any) in its *class-head* [[class.pre]]. The
|
| 701 |
+
point of declaration for an enumeration is immediately after the
|
| 702 |
+
*identifier* (if any) in either its *enum-specifier* [[dcl.enum]] or its
|
| 703 |
+
first *opaque-enum-declaration* [[dcl.enum]], whichever comes first. The
|
| 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 point of declaration of a *using-declarator* that does not name a
|
| 708 |
+
constructor is immediately after the *using-declarator*
|
| 709 |
+
[[namespace.udecl]].
|
| 710 |
|
| 711 |
The point of declaration for an enumerator is immediately after its
|
| 712 |
*enumerator-definition*.
|
| 713 |
|
| 714 |
[*Example 3*:
|
|
|
|
| 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 point of declaration for a function-local predefined variable
|
| 772 |
+
[[dcl.fct.def.general]] is immediately before the *function-body* of a
|
| 773 |
+
function definition.
|
| 774 |
+
|
| 775 |
+
The point of declaration of a structured binding [[dcl.struct.bind]] is
|
| 776 |
+
immediately after the *identifier-list* of the structured binding
|
| 777 |
+
declaration.
|
| 778 |
+
|
| 779 |
+
The point of declaration for the variable or the structured bindings
|
| 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 point of declaration for a template parameter is immediately after
|
| 784 |
its complete *template-parameter*.
|
| 785 |
|
| 786 |
[*Example 4*:
|
|
|
|
| 795 |
|
| 796 |
— *end example*]
|
| 797 |
|
| 798 |
[*Note 5*: Friend declarations refer to functions or classes that are
|
| 799 |
members of the nearest enclosing namespace, but they do not introduce
|
| 800 |
+
new names into that namespace [[namespace.memdef]]. Function
|
| 801 |
declarations at block scope and variable declarations with the `extern`
|
| 802 |
specifier at block scope refer to declarations that are members of an
|
| 803 |
enclosing namespace, but they do not introduce new names into that
|
| 804 |
scope. — *end note*]
|
| 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 |
+
A name declared in a block [[stmt.block]] is local to that block; it has
|
| 812 |
+
*block scope*. Its potential scope begins at its point of declaration
|
| 813 |
+
[[basic.scope.pdecl]] and ends at the end of its block. A variable
|
| 814 |
+
declared at block scope is a *local variable*.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 815 |
|
| 816 |
The name declared in an *exception-declaration* is local to the
|
| 817 |
*handler* and shall not be redeclared in the outermost block of the
|
| 818 |
*handler*.
|
| 819 |
|
| 820 |
Names declared in the *init-statement*, the *for-range-declaration*, and
|
| 821 |
in the *condition* of `if`, `while`, `for`, and `switch` statements are
|
| 822 |
local to the `if`, `while`, `for`, or `switch` statement (including the
|
| 823 |
controlled statement), and shall not be redeclared in a subsequent
|
| 824 |
condition of that statement nor in the outermost block (or, for the `if`
|
| 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 {
|
| 834 |
+
int x; // error: redeclaration of x
|
| 835 |
+
}
|
| 836 |
+
```
|
| 837 |
+
|
| 838 |
+
— *end example*]
|
| 839 |
+
|
| 840 |
+
### Function parameter scope <a id="basic.scope.param">[[basic.scope.param]]</a>
|
| 841 |
+
|
| 842 |
+
A function parameter (including one appearing in a *lambda-declarator*)
|
| 843 |
+
or function-local predefined variable [[dcl.fct.def]] has *function
|
| 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 |
### Function scope <a id="basic.funscope">[[basic.funscope]]</a>
|
| 856 |
|
| 857 |
+
Labels [[stmt.label]] have *function scope* and may be used anywhere in
|
| 858 |
+
the function in which they are declared. Only labels have function
|
| 859 |
scope.
|
| 860 |
|
| 861 |
### Namespace scope <a id="basic.scope.namespace">[[basic.scope.namespace]]</a>
|
| 862 |
|
| 863 |
The declarative region of a *namespace-definition* is its
|
| 864 |
*namespace-body*. Entities declared in a *namespace-body* are said to be
|
| 865 |
*members* of the namespace, and names introduced by these declarations
|
| 866 |
into the declarative region of the namespace are said to be *member
|
| 867 |
names* of the namespace. A namespace member name has namespace scope.
|
| 868 |
Its potential scope includes its namespace from the name’s point of
|
| 869 |
+
declaration [[basic.scope.pdecl]] onwards; and for each
|
| 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*:
|
|
|
|
| 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 |
The potential scope of a name declared in a class consists not only of
|
| 964 |
the declarative region following the name’s point of declaration, but
|
| 965 |
+
also of all complete-class contexts [[class.mem]] of that class.
|
|
|
|
|
|
|
| 966 |
|
| 967 |
A name `N` used in a class `S` shall refer to the same declaration in
|
| 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 |
A name declared within a member function hides a declaration of the same
|
| 972 |
name whose scope extends to or past the end of the member function’s
|
| 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 |
typedef int c;
|
|
|
|
| 1011 |
— *end example*]
|
| 1012 |
|
| 1013 |
The name of a class member shall only be used as follows:
|
| 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 |
+
The name of a scoped enumerator [[dcl.enum]] has *enumeration scope*.
|
| 1027 |
Its potential scope begins at its point of declaration and terminates at
|
| 1028 |
the end of the *enum-specifier*.
|
| 1029 |
|
| 1030 |
### Template parameter scope <a id="basic.scope.temp">[[basic.scope.temp]]</a>
|
| 1031 |
|
|
|
|
| 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
|
|
|
|
| 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;
|
|
|
|
| 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]].
|
|
|
|
| 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 |
+
If the declarations found by name lookup all denote functions or
|
| 1159 |
+
function templates, the declarations are said to form an *overload set*.
|
| 1160 |
+
The declarations found by name lookup shall either all denote the same
|
| 1161 |
+
entity or form an overload set. Overload resolution ([[over.match]],
|
| 1162 |
+
[[over.over]]) takes place after name lookup has succeeded. The access
|
| 1163 |
+
rules [[class.access]] are considered only once name lookup and function
|
| 1164 |
overload resolution (if applicable) have succeeded. Only after name
|
| 1165 |
lookup, function overload resolution (if applicable) and access checking
|
| 1166 |
+
have succeeded are the semantic properties introduced by the name’s
|
| 1167 |
+
declaration and its reachable [[module.reach]] redeclarations used
|
| 1168 |
+
further in expression processing [[expr]].
|
| 1169 |
|
| 1170 |
+
A name “looked up in the context of an expression” is looked up in the
|
| 1171 |
+
scope where the expression is found.
|
| 1172 |
|
| 1173 |
+
The injected-class-name of a class [[class.pre]] is also considered to
|
| 1174 |
+
be a member of that class for the purposes of name hiding and lookup.
|
|
|
|
| 1175 |
|
| 1176 |
[*Note 1*: [[basic.link]] discusses linkage issues. The notions of
|
| 1177 |
scope, point of declaration and name hiding are discussed in
|
| 1178 |
[[basic.scope]]. — *end note*]
|
| 1179 |
|
|
|
|
| 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
|
| 1201 |
+
apply. In some cases a name followed by `<` is treated as a
|
| 1202 |
+
*template-name* even though name lookup did not find a *template-name*
|
| 1203 |
+
(see [[temp.names]]). For example,
|
| 1204 |
+
|
| 1205 |
+
``` cpp
|
| 1206 |
+
int h;
|
| 1207 |
+
void g();
|
| 1208 |
+
namespace N {
|
| 1209 |
+
struct A {};
|
| 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: lookup of f finds nothing, f treated as template name
|
| 1216 |
+
int y = g<N::A>(N::A()); // OK: lookup of g finds a function, g treated as template name
|
| 1217 |
+
int z = h<N::A>(N::A()); // error: h< does not begin a template-id
|
| 1218 |
+
```
|
| 1219 |
+
|
| 1220 |
+
The rules in [[basic.lookup.argdep]] have no effect on the syntactic
|
| 1221 |
+
interpretation of an expression. For example,
|
| 1222 |
|
| 1223 |
``` cpp
|
| 1224 |
typedef int f;
|
| 1225 |
namespace N {
|
| 1226 |
struct A {
|
|
|
|
| 1232 |
};
|
| 1233 |
}
|
| 1234 |
```
|
| 1235 |
|
| 1236 |
Because the expression is not a function call, the argument-dependent
|
| 1237 |
+
name lookup [[basic.lookup.argdep]] does not apply and the friend
|
| 1238 |
function `f` is not found.
|
| 1239 |
|
| 1240 |
— *end note*]
|
| 1241 |
|
| 1242 |
A name used in global scope, outside of any function, class or
|
|
|
|
| 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 {
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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*:
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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 namespaces* and a set of zero or more *associated
|
| 1511 |
+
entities* (other than namespaces) to be considered. The sets of
|
| 1512 |
+
namespaces and entities are determined entirely by the types of the
|
| 1513 |
+
function arguments (and the namespace of any template template
|
| 1514 |
+
argument). Typedef names and *using-declaration*s used to specify the
|
| 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 sets of namespaces and
|
| 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. Its associated namespaces are
|
| 1523 |
+
the innermost enclosing namespaces of its associated entities.
|
| 1524 |
+
Furthermore, if `T` is a class template specialization, its associated
|
| 1525 |
+
namespaces and entities also include: the namespaces and entities
|
| 1526 |
+
associated with the types of the template arguments provided for
|
| 1527 |
+
template type parameters (excluding template template parameters); the
|
| 1528 |
+
templates used as template template arguments; the namespaces of which
|
| 1529 |
+
any template template arguments are members; and the classes of which
|
| 1530 |
+
any member templates used as template template arguments are members.
|
| 1531 |
+
\[*Note 1*: Non-type template arguments do not contribute to the set
|
| 1532 |
+
of associated namespaces. — *end note*]
|
| 1533 |
- If `T` is an enumeration type, its associated namespace is the
|
| 1534 |
+
innermost enclosing namespace of its declaration, and its associated
|
| 1535 |
+
entities are `T` and, if it is a class member, the member’s class.
|
|
|
|
| 1536 |
- If `T` is a pointer to `U` or an array of `U`, its associated
|
| 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 namespaces and entities are those associated with the
|
| 1543 |
function parameter types and return type, together with those
|
| 1544 |
associated with `X`.
|
| 1545 |
- If `T` is a pointer to a data member of class `X`, its associated
|
| 1546 |
+
namespaces and entities are those associated with the member type
|
| 1547 |
together with those associated with `X`.
|
| 1548 |
|
| 1549 |
+
If an associated namespace is an inline namespace [[namespace.def]], its
|
| 1550 |
+
enclosing namespace is also included in the set. If an associated
|
| 1551 |
namespace directly contains inline namespaces, those inline namespaces
|
| 1552 |
are also included in the set. In addition, if the argument is the name
|
| 1553 |
+
or address of an overload set, its associated entities and namespaces
|
| 1554 |
+
are the union of those associated with each of the members of the set,
|
| 1555 |
+
i.e., the entities and namespaces associated with its parameter types
|
| 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 |
+
Let *X* be the lookup set produced by unqualified lookup
|
| 1562 |
+
[[basic.lookup.unqual]] and let *Y* be the lookup set produced by
|
| 1563 |
argument dependent lookup (defined as follows). If *X* contains
|
| 1564 |
|
| 1565 |
- a declaration of a class member, or
|
| 1566 |
- a block-scope function declaration that is not a *using-declaration*,
|
| 1567 |
or
|
|
|
|
| 1570 |
then *Y* is empty. Otherwise *Y* is the set of declarations found in the
|
| 1571 |
namespaces associated with the argument types as described below. The
|
| 1572 |
set of declarations found by the lookup of the name is the union of *X*
|
| 1573 |
and *Y*.
|
| 1574 |
|
| 1575 |
+
[*Note 2*: The namespaces and entities associated with the argument
|
| 1576 |
+
types can include namespaces and entities already considered by the
|
| 1577 |
ordinary unqualified lookup. — *end note*]
|
| 1578 |
|
| 1579 |
[*Example 2*:
|
| 1580 |
|
| 1581 |
``` cpp
|
|
|
|
| 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 {
|
| 1629 |
+
export struct X {};
|
| 1630 |
+
export void f(X);
|
| 1631 |
+
}
|
| 1632 |
+
namespace S {
|
| 1633 |
+
export void f(R::X, R::X);
|
| 1634 |
+
}
|
| 1635 |
+
```
|
| 1636 |
+
|
| 1637 |
+
Translation unit #2
|
| 1638 |
+
|
| 1639 |
+
``` cpp
|
| 1640 |
+
export module N;
|
| 1641 |
+
import M;
|
| 1642 |
+
export R::X make();
|
| 1643 |
+
namespace R { static int g(X); }
|
| 1644 |
+
export template<typename T, typename U> void apply(T t, U u) {
|
| 1645 |
+
f(t, u);
|
| 1646 |
+
g(t);
|
| 1647 |
+
}
|
| 1648 |
+
```
|
| 1649 |
+
|
| 1650 |
+
Translation unit #3
|
| 1651 |
+
|
| 1652 |
+
``` cpp
|
| 1653 |
+
module Q;
|
| 1654 |
+
import N;
|
| 1655 |
+
namespace S {
|
| 1656 |
+
struct Z { template<typename T> operator T(); };
|
| 1657 |
+
}
|
| 1658 |
+
void test() {
|
| 1659 |
+
auto x = make(); // OK, decltype(x) is R::X in module M
|
| 1660 |
+
R::f(x); // error: R and R::f are not visible here
|
| 1661 |
+
f(x); // OK, calls R::f from interface of M
|
| 1662 |
+
f(x, S::Z()); // error: S::f in module M not considered
|
| 1663 |
+
// even though S is an associated namespace
|
| 1664 |
+
apply(x, S::Z()); // error: S::f is visible in instantiation context, but
|
| 1665 |
+
// R::g has internal linkage and cannot be used outside TU #2
|
| 1666 |
+
}
|
| 1667 |
+
```
|
| 1668 |
+
|
| 1669 |
+
— *end example*]
|
| 1670 |
|
| 1671 |
### Qualified name lookup <a id="basic.lookup.qual">[[basic.lookup.qual]]</a>
|
| 1672 |
|
| 1673 |
The name of a class or namespace member or enumerator can be referred to
|
| 1674 |
+
after the `::` scope resolution operator [[expr.prim.id.qual]] applied
|
| 1675 |
+
to a *nested-name-specifier* that denotes its class, namespace, or
|
| 1676 |
enumeration. If a `::` scope resolution operator in a
|
| 1677 |
*nested-name-specifier* is not preceded by a *decltype-specifier*,
|
| 1678 |
lookup of the name preceding that `::` considers only namespaces, types,
|
| 1679 |
and templates whose specializations are types. If the name found does
|
| 1680 |
not designate a namespace or a class, enumeration, or dependent type,
|
|
|
|
| 1688 |
static int n;
|
| 1689 |
};
|
| 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 |
[*Note 1*: Multiply qualified names, such as `N1::N2::N3::n`, can be
|
| 1700 |
+
used to refer to members of nested classes [[class.nest]] or members of
|
| 1701 |
+
nested namespaces. — *end note*]
|
| 1702 |
|
| 1703 |
In a declaration in which the *declarator-id* is a *qualified-id*, names
|
| 1704 |
used before the *qualified-id* being declared are looked up in the
|
| 1705 |
defining namespace scope; names following the *qualified-id* are looked
|
| 1706 |
up in the scope of the member’s class or namespace.
|
|
|
|
| 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 |
+
A name prefixed by the unary scope operator `::` [[expr.prim.id.qual]]
|
| 1725 |
+
is looked up in global scope, in the translation unit where it is used.
|
| 1726 |
+
The name shall be declared in global namespace scope or shall be a name
|
| 1727 |
whose declaration is visible in global scope because of a
|
| 1728 |
+
*using-directive* [[namespace.qual]]. The use of `::` allows a global
|
| 1729 |
+
name to be referred to even if its identifier has been hidden
|
| 1730 |
+
[[basic.scope.hiding]].
|
| 1731 |
|
| 1732 |
A name prefixed by a *nested-name-specifier* that nominates an
|
| 1733 |
enumeration type shall represent an *enumerator* of that enumeration.
|
| 1734 |
|
| 1735 |
+
In a *qualified-id* of the form:
|
|
|
|
|
|
|
|
|
|
| 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 {
|
|
|
|
| 1769 |
|
| 1770 |
#### Class members <a id="class.qual">[[class.qual]]</a>
|
| 1771 |
|
| 1772 |
If the *nested-name-specifier* of a *qualified-id* nominates a class,
|
| 1773 |
the name specified after the *nested-name-specifier* is looked up in the
|
| 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 |
[*Note 1*: A class member can be referred to using a *qualified-id* at
|
| 1779 |
+
any point in its potential scope [[basic.scope.class]]. — *end note*]
|
|
|
|
| 1780 |
|
| 1781 |
The exceptions to the name lookup rule above are the following:
|
| 1782 |
|
| 1783 |
- the lookup for a destructor is as specified in [[basic.lookup.qual]];
|
| 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
|
|
|
|
| 1818 |
|
| 1819 |
A::A() { }
|
| 1820 |
B::B() { }
|
| 1821 |
|
| 1822 |
B::A ba; // object of type A
|
| 1823 |
+
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 |
|
|
|
|
| 1839 |
names in a *template-argument* of a *template-id* are looked up in the
|
| 1840 |
context in which the entire *postfix-expression* occurs.
|
| 1841 |
|
| 1842 |
For a namespace `X` and name `m`, the namespace-qualified lookup set
|
| 1843 |
S(X, m) is defined as follows: Let S'(X, m) be the set of all
|
| 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;
|
|
|
|
| 1992 |
— *end example*]
|
| 1993 |
|
| 1994 |
During the lookup of a qualified namespace member name, if the lookup
|
| 1995 |
finds more than one declaration of the member, and if one declaration
|
| 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*:
|
|
|
|
| 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
|
|
|
|
| 2074 |
|
| 2075 |
— *end example*]
|
| 2076 |
|
| 2077 |
### Elaborated type specifiers <a id="basic.lookup.elab">[[basic.lookup.elab]]</a>
|
| 2078 |
|
| 2079 |
+
An *elaborated-type-specifier* [[dcl.type.elab]] may be used to refer to
|
| 2080 |
+
a previously declared *class-name* or *enum-name* even though the name
|
| 2081 |
+
has been hidden by a non-type declaration [[basic.scope.hiding]].
|
|
|
|
| 2082 |
|
| 2083 |
If the *elaborated-type-specifier* has no *nested-name-specifier*, and
|
| 2084 |
unless the *elaborated-type-specifier* appears in a declaration with the
|
| 2085 |
following form:
|
| 2086 |
|
|
|
|
| 2112 |
|
| 2113 |
[*Example 1*:
|
| 2114 |
|
| 2115 |
``` cpp
|
| 2116 |
struct Node {
|
| 2117 |
+
struct Node* Next; // OK: Refers to injected-class-name Node
|
| 2118 |
+
struct Data* Data; // OK: Declares type Data at global scope and member Data
|
|
|
|
| 2119 |
};
|
| 2120 |
|
| 2121 |
struct Data {
|
| 2122 |
struct Node* Node; // OK: Refers to Node at global scope
|
| 2123 |
+
friend struct ::Glob; // error: Glob is not declared, cannot introduce a qualified type[dcl.type.elab]
|
| 2124 |
friend struct Glob; // OK: Refers to (as yet) undeclared Glob at global scope.
|
| 2125 |
...
|
| 2126 |
};
|
| 2127 |
|
| 2128 |
struct Base {
|
|
|
|
| 2133 |
friend class Data; // OK: nested Data is a friend
|
| 2134 |
struct Data { ... }; // Defines nested Data
|
| 2135 |
};
|
| 2136 |
|
| 2137 |
struct Data; // OK: Redeclares Data at global scope
|
| 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: refers to nested Data
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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 { };
|
|
|
|
| 2243 |
lookup for a *namespace-name* or for a name in a *nested-name-specifier*
|
| 2244 |
only namespace names are considered.
|
| 2245 |
|
| 2246 |
## Program and linkage <a id="basic.link">[[basic.link]]</a>
|
| 2247 |
|
| 2248 |
+
A *program* consists of one or more translation units [[lex.separate]]
|
| 2249 |
+
linked together. A translation unit consists of a sequence of
|
| 2250 |
declarations.
|
| 2251 |
|
| 2252 |
``` bnf
|
| 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 might denote the same object,
|
| 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
|
| 2264 |
other scopes of the same translation unit.
|
| 2265 |
+
- When a name has *module linkage*, the entity it denotes can be
|
| 2266 |
+
referred to by names from other scopes of the same module unit
|
| 2267 |
+
[[module.unit]] or from scopes of other module units of that same
|
| 2268 |
+
module.
|
| 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 |
+
A name having namespace scope [[basic.scope.namespace]] has internal
|
| 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 or exported, or
|
| 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. A name having namespace scope that has not been
|
| 2293 |
+
given internal linkage above and that is the name of
|
|
|
|
| 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
|
| 2299 |
+
purposes [[dcl.typedef]]; or
|
| 2300 |
+
- a named enumeration [[dcl.enum]], or an unnamed enumeration defined in
|
| 2301 |
+
a typedef declaration in which the enumeration has the typedef name
|
| 2302 |
+
for linkage purposes [[dcl.typedef]]; or
|
| 2303 |
+
- an unnamed enumeration that has an enumerator as a name for linkage
|
| 2304 |
+
purposes [[dcl.enum]]; or
|
| 2305 |
+
- a template
|
| 2306 |
+
|
| 2307 |
+
has its linkage determined as follows:
|
| 2308 |
+
|
| 2309 |
+
- if the enclosing namespace has internal linkage, the name has internal
|
| 2310 |
+
linkage;
|
| 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 of class scope, or an unnamed class or enumeration defined
|
| 2318 |
in a class-scope typedef declaration such that the class or enumeration
|
| 2319 |
+
has the typedef name for linkage purposes [[dcl.typedef]], has the same
|
| 2320 |
+
linkage, if any, as the name of the class of which it is a member.
|
| 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 g() {
|
| 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: external linkage, ill-formed
|
| 2350 |
}
|
|
|
|
| 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 names that are the same [[basic.pre]] and that are declared in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2392 |
different scopes shall denote the same variable, function, type,
|
| 2393 |
template or namespace if
|
| 2394 |
|
| 2395 |
+
- both names have external or module linkage and are declared in
|
| 2396 |
+
declarations attached to the same module, or else both names have
|
| 2397 |
+
internal linkage and are declared in the same translation unit; and
|
| 2398 |
- both names refer to members of the same namespace or to members, not
|
| 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 |
+
If multiple declarations of the same name with external linkage would
|
| 2405 |
+
declare the same entity except that they are attached to different
|
| 2406 |
+
modules, the program is ill-formed; no diagnostic is required.
|
| 2407 |
+
|
| 2408 |
+
[*Note 2*: *using-declaration*s, typedef declarations, and
|
| 2409 |
+
*alias-declaration*s do not declare entities, but merely introduce
|
| 2410 |
+
synonyms. Similarly, *using-directive*s do not declare entities.
|
| 2411 |
+
Enumerators do not have linkage, but may serve as the name of an
|
| 2412 |
+
enumeration with linkage [[dcl.enum]]. — *end note*]
|
| 2413 |
+
|
| 2414 |
+
If a declaration would redeclare a reachable declaration attached to a
|
| 2415 |
+
different module, the program is ill-formed.
|
| 2416 |
+
|
| 2417 |
+
[*Example 3*:
|
| 2418 |
+
|
| 2419 |
+
\`"decls.h"\`
|
| 2420 |
+
|
| 2421 |
+
``` cpp
|
| 2422 |
+
int f(); // #1, attached to the global module
|
| 2423 |
+
int g(); // #2, attached to the global module
|
| 2424 |
+
```
|
| 2425 |
+
|
| 2426 |
+
Module interface of \`M\`
|
| 2427 |
+
|
| 2428 |
+
``` cpp
|
| 2429 |
+
module;
|
| 2430 |
+
#include "decls.h"
|
| 2431 |
+
export module M;
|
| 2432 |
+
export using ::f; // OK: does not declare an entity, exports #1
|
| 2433 |
+
int g(); // error: matches #2, but attached to M
|
| 2434 |
+
export int h(); // #3
|
| 2435 |
+
export int k(); // #4
|
| 2436 |
+
```
|
| 2437 |
+
|
| 2438 |
+
Other translation unit
|
| 2439 |
+
|
| 2440 |
+
``` cpp
|
| 2441 |
+
import M;
|
| 2442 |
+
static int h(); // error: matches #3
|
| 2443 |
+
int k(); // error: matches #4
|
| 2444 |
+
```
|
| 2445 |
+
|
| 2446 |
+
— *end example*]
|
| 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 |
+
After all adjustments of types (during which typedefs [[dcl.typedef]]
|
| 2453 |
are replaced by their definitions), the types specified by all
|
| 2454 |
declarations referring to a given variable or function shall be
|
| 2455 |
identical, except that declarations for an array object can specify
|
| 2456 |
array types that differ by the presence or absence of a major array
|
| 2457 |
+
bound [[dcl.array]]. A violation of this rule on type identity does not
|
| 2458 |
+
require a diagnostic.
|
| 2459 |
+
|
| 2460 |
+
[*Note 3*: Linkage to non-C++ declarations can be achieved using a
|
| 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 4*: Non-dependent names in an
|
| 2472 |
+
instantiated declaration do not refer to a set of overloads
|
| 2473 |
+
[[temp.nondep]]. — *end note*]
|
| 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
|
| 2479 |
+
(but not the deduced return type for a (possibly instantiated)
|
| 2480 |
+
definition of a function with a declared return type that uses a
|
| 2481 |
+
placeholder type [[dcl.spec.auto]]),
|
| 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 [[basic.def.odr]],
|
| 2488 |
+
|
| 2489 |
+
or defines a constexpr variable initialized to a TU-local value (defined
|
| 2490 |
+
below).
|
| 2491 |
+
|
| 2492 |
+
[*Note 5*: An inline function template can be an exposure even though
|
| 2493 |
+
explicit specializations of it might be usable in other translation
|
| 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
|
| 2500 |
+
- does not have a name with linkage and is declared, or introduced by
|
| 2501 |
+
a *lambda-expression*, within the definition of a TU-local entity,
|
| 2502 |
+
- a type with no name that is defined outside a *class-specifier*,
|
| 2503 |
+
function body, or *initializer* or is introduced by a
|
| 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 6*: The specialization might have
|
| 2510 |
+
been implicitly or explicitly instantiated. — *end note*]
|
| 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 |
+
|
| 2521 |
+
If a (possibly instantiated) declaration of, or a deduction guide for, a
|
| 2522 |
+
non-TU-local entity in a module interface unit (outside the
|
| 2523 |
+
*private-module-fragment*, if any) or module partition [[module.unit]]
|
| 2524 |
+
is an exposure, the program is ill-formed. Such a declaration in any
|
| 2525 |
+
other context is deprecated [[depr.local]].
|
| 2526 |
+
|
| 2527 |
+
If a declaration that appears in one translation unit names a TU-local
|
| 2528 |
+
entity declared in another translation unit that is not a header unit,
|
| 2529 |
+
the program is ill-formed. A declaration instantiated for a template
|
| 2530 |
+
specialization [[temp.spec]] appears at the point of instantiation of
|
| 2531 |
+
the specialization [[temp.point]].
|
| 2532 |
+
|
| 2533 |
+
[*Example 4*:
|
| 2534 |
+
|
| 2535 |
+
Translation unit #1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2536 |
|
| 2537 |
``` cpp
|
| 2538 |
+
export module A;
|
| 2539 |
+
static void f() {}
|
| 2540 |
+
inline void it() { f(); } // error: is an exposure of f
|
| 2541 |
+
static inline void its() { f(); } // OK
|
| 2542 |
+
template<int> void g() { its(); } // OK
|
| 2543 |
+
template void g<0>();
|
| 2544 |
+
|
| 2545 |
+
decltype(f) *fp; // error: f (though not its type) is TU-local
|
| 2546 |
+
auto &fr = f; // OK
|
| 2547 |
+
constexpr auto &fr2 = fr; // error: is an exposure of f
|
| 2548 |
+
constexpr static auto fp2 = fr; // OK
|
| 2549 |
+
|
| 2550 |
+
struct S { void (&ref)(); } s{f}; // OK, value is TU-local
|
| 2551 |
+
constexpr extern struct W { S &s; } wrap{s}; // OK, value is not TU-local
|
| 2552 |
+
|
| 2553 |
+
static auto x = []{f();}; // OK
|
| 2554 |
+
auto x2 = x; // error: the closure type is TU-local
|
| 2555 |
+
int y = ([]{f();}(),0); // error: the closure type is not TU-local
|
| 2556 |
+
int y2 = (x,0); // OK
|
| 2557 |
+
|
| 2558 |
+
namespace N {
|
| 2559 |
+
struct A {};
|
| 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 a specialization might be an exposure
|
| 2566 |
+
```
|
| 2567 |
+
|
| 2568 |
+
Translation unit #2
|
| 2569 |
+
|
| 2570 |
+
``` cpp
|
| 2571 |
+
module A;
|
| 2572 |
+
void other() {
|
| 2573 |
+
g<0>(); // OK, specialization is explicitly instantiated
|
| 2574 |
+
g<1>(); // error: instantiation uses TU-local its
|
| 2575 |
+
h(N::A{}); // error: overload set contains TU-local N::adl(int)
|
| 2576 |
+
h(0); // OK, calls adl(double)
|
| 2577 |
+
adl(N::A{}); // OK; N::adl(int) not found, calls N::adl(N::A)
|
| 2578 |
+
fr(); // OK, calls f
|
| 2579 |
+
constexpr auto ptr = fr; // error: fr is not usable in constant expressions here
|
| 2580 |
+
}
|
| 2581 |
+
```
|
| 2582 |
+
|
| 2583 |
+
— *end example*]
|
| 2584 |
+
|
| 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 any member of the basic
|
| 2591 |
+
execution character set [[lex.charset]] and the eight-bit code units of
|
| 2592 |
+
the Unicode UTF-8 encoding form and is composed of a contiguous sequence
|
| 2593 |
+
of bits,[^9] the number of which is *implementation-defined*. The least
|
| 2594 |
+
significant bit is called the *low-order bit*; the most significant bit
|
| 2595 |
+
is called the *high-order bit*. The memory available to a C++ program
|
| 2596 |
+
consists of one or more sequences of contiguous bytes. Every byte has a
|
| 2597 |
+
unique address.
|
| 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 or a maximal
|
| 2603 |
+
sequence of adjacent bit-fields all having nonzero width.
|
| 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*]
|
| 2609 |
+
|
| 2610 |
+
Two or more threads of execution [[intro.multithread]] can access
|
| 2611 |
+
separate memory locations without interfering with each other.
|
| 2612 |
+
|
| 2613 |
+
[*Note 3*: Thus a bit-field and an adjacent non-bit-field are in
|
| 2614 |
+
separate memory locations, and therefore can be concurrently updated by
|
| 2615 |
+
two threads of execution without interference. The same applies to two
|
| 2616 |
+
bit-fields, if one is declared inside a nested struct declaration and
|
| 2617 |
+
the other is not, or if the two are separated by a zero-length bit-field
|
| 2618 |
+
declaration, or if they are separated by a non-bit-field declaration. It
|
| 2619 |
+
is not safe to concurrently update two bit-fields in the same struct if
|
| 2620 |
+
all fields between them are also bit-fields of nonzero
|
| 2621 |
+
width. — *end note*]
|
| 2622 |
+
|
| 2623 |
+
[*Example 1*:
|
| 2624 |
+
|
| 2625 |
+
A class declared as
|
| 2626 |
+
|
| 2627 |
+
``` cpp
|
| 2628 |
+
struct {
|
| 2629 |
+
char a;
|
| 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
|
| 2641 |
+
`c` together constitute the fourth memory location. The bit-fields `b`
|
| 2642 |
+
and `c` cannot be concurrently modified, but `b` and `a`, for example,
|
| 2643 |
+
can be.
|
| 2644 |
+
|
| 2645 |
+
— *end example*]
|
| 2646 |
+
|
| 2647 |
+
### Object model <a id="intro.object">[[intro.object]]</a>
|
| 2648 |
+
|
| 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 ([[conv.rval]], [[class.temporary]]). An object occupies a
|
| 2655 |
+
region of storage in its period of construction [[class.cdtor]],
|
| 2656 |
+
throughout its lifetime [[basic.life]], and in its period of destruction
|
| 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]]. Some objects are polymorphic
|
| 2666 |
+
[[class.virtual]]; the implementation generates information associated
|
| 2667 |
+
with each such object that makes it possible to determine that object’s
|
| 2668 |
+
type during program execution. For other objects, the interpretation of
|
| 2669 |
+
the values found therein is determined by the type of the *expression*s
|
| 2670 |
+
[[expr.compound]] used to access them.
|
| 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
|
| 2676 |
+
object is created in storage associated with a member subobject or array
|
| 2677 |
+
element *e* (which may or may not be within its lifetime), the created
|
| 2678 |
+
object is a subobject of *e*’s containing object if:
|
| 2679 |
+
|
| 2680 |
+
- the lifetime of *e*’s containing object has begun and not ended, and
|
| 2681 |
+
- the storage for the new object exactly overlays the storage location
|
| 2682 |
+
associated with *e*, and
|
| 2683 |
+
- the new object is of the same type as *e* (ignoring cv-qualification).
|
| 2684 |
+
|
| 2685 |
+
If a complete object is created [[expr.new]] in storage associated with
|
| 2686 |
+
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 smaller array object that satisfies these constraints.
|
| 2693 |
+
|
| 2694 |
+
[*Note 2*: If that portion of the array previously provided storage for
|
| 2695 |
+
another object, the lifetime of that object ends because its storage was
|
| 2696 |
+
reused [[basic.life]]. — *end note*]
|
| 2697 |
|
| 2698 |
[*Example 1*:
|
| 2699 |
|
| 2700 |
``` cpp
|
| 2701 |
+
template<typename ...T>
|
| 2702 |
+
struct AlignedUnion {
|
| 2703 |
+
alignas(T...) unsigned char data[max(sizeof(T)...)];
|
| 2704 |
+
};
|
| 2705 |
+
int f() {
|
| 2706 |
+
AlignedUnion<int, char> au;
|
| 2707 |
+
int *p = new (au.data) int; // OK, au.data provides storage
|
| 2708 |
+
char *c = new (au.data) char(); // OK, ends lifetime of *p
|
| 2709 |
+
char *d = new (au.data + 1) char();
|
| 2710 |
+
return *c + *d; // OK
|
| 2711 |
}
|
| 2712 |
|
| 2713 |
+
struct A { unsigned char a[32]; };
|
| 2714 |
+
struct B { unsigned char b[16]; };
|
| 2715 |
A a;
|
| 2716 |
+
B *b = new (a.a + 8) B; // a.a provides storage for *b
|
| 2717 |
+
int *p = new (b->b + 4) int; // b->b provides storage for *p
|
| 2718 |
+
// a.a does not provide storage for *p (directly),
|
| 2719 |
+
// but *p is nested within a (see below)
|
| 2720 |
+
```
|
| 2721 |
+
|
| 2722 |
+
— *end example*]
|
| 2723 |
+
|
| 2724 |
+
An object *a* is *nested within* another object *b* if:
|
| 2725 |
+
|
| 2726 |
+
- *a* is a subobject of *b*, or
|
| 2727 |
+
- *b* provides storage for *a*, or
|
| 2728 |
+
- there exists an object *c* where *a* is nested within *c*, and *c* is
|
| 2729 |
+
nested within *b*.
|
| 2730 |
+
|
| 2731 |
+
For every object `x`, there is some object called the *complete object
|
| 2732 |
+
of* `x`, determined as follows:
|
| 2733 |
+
|
| 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 data member [[class.mem]], or an array element
|
| 2740 |
+
is of class type, its type is considered the *most derived class*, to
|
| 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:
|
| 2746 |
+
|
| 2747 |
+
- a base class subobject, or
|
| 2748 |
+
- a non-static data member declared with the `no_unique_address`
|
| 2749 |
+
attribute [[dcl.attr.nouniqueaddr]].
|
| 2750 |
+
|
| 2751 |
+
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 length.
|
| 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.[^10]
|
| 2774 |
+
|
| 2775 |
+
[*Example 2*:
|
| 2776 |
+
|
| 2777 |
+
``` cpp
|
| 2778 |
+
static const char test1 = 'x';
|
| 2779 |
+
static const char test2 = 'x';
|
| 2780 |
+
const bool b = &test1 != &test2; // always true
|
| 2781 |
+
```
|
| 2782 |
+
|
| 2783 |
+
— *end example*]
|
| 2784 |
+
|
| 2785 |
+
The address of a non-bit-field subobject of zero size is the address of
|
| 2786 |
+
an unspecified byte of storage occupied by the complete object of that
|
| 2787 |
+
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 would
|
| 2794 |
+
result in the program having defined behavior. If no such set of objects
|
| 2795 |
+
would give the program defined behavior, the behavior of the program is
|
| 2796 |
+
undefined. If multiple such sets of objects would give the program
|
| 2797 |
+
defined behavior, it is unspecified which such set of objects is
|
| 2798 |
+
created.
|
| 2799 |
+
|
| 2800 |
+
[*Note 3*: Such operations do not start the lifetimes of subobjects of
|
| 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
|
| 2806 |
+
*suitable created object*. These operations select one of the
|
| 2807 |
+
implicitly-created objects whose address is the address of the start of
|
| 2808 |
+
the region of storage, and produce a pointer value that points to that
|
| 2809 |
+
object, if that value would result in the program having defined
|
| 2810 |
+
behavior. If no such pointer value would give the program defined
|
| 2811 |
+
behavior, the behavior of the program is undefined. If multiple such
|
| 2812 |
+
pointer values would give the program defined behavior, it is
|
| 2813 |
+
unspecified which such pointer value is produced.
|
| 2814 |
+
|
| 2815 |
+
[*Example 3*:
|
| 2816 |
+
|
| 2817 |
+
``` cpp
|
| 2818 |
+
#include <cstdlib>
|
| 2819 |
+
struct X { int a, b; };
|
| 2820 |
+
X *make_x() {
|
| 2821 |
+
// The call to std::malloc implicitly creates an object of type X
|
| 2822 |
+
// and its subobjects a and b, and returns a pointer to that X object
|
| 2823 |
+
// (or an object that is pointer-interconvertible[basic.compound] with it),
|
| 2824 |
+
// in order to give the subsequent class member access operations
|
| 2825 |
+
// defined behavior.
|
| 2826 |
+
X *p = (X*)std::malloc(sizeof(struct X));
|
| 2827 |
+
p->a = 1;
|
| 2828 |
+
p->b = 2;
|
| 2829 |
+
return p;
|
| 2830 |
}
|
| 2831 |
```
|
| 2832 |
|
| 2833 |
+
— *end example*]
|
| 2834 |
+
|
| 2835 |
+
An operation that begins the lifetime of an array of `char`,
|
| 2836 |
+
`unsigned char`, or `std::byte` implicitly creates objects within the
|
| 2837 |
+
region of storage occupied by the array.
|
| 2838 |
+
|
| 2839 |
+
[*Note 4*: The array object provides storage for these
|
| 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 5*: Some functions in the C++ standard library implicitly create
|
| 2847 |
+
objects ([[allocator.traits.members]], [[c.malloc]], [[cstring.syn]],
|
| 2848 |
+
[[bit.cast]]). — *end note*]
|
| 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 |
+
multi-dimensional) array thereof, that class type has a trivial default
|
| 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 ([[dcl.init.aggr]], [[class.base.init]]), or as described in
|
| 2866 |
+
[[class.union]] and [[class.copy.ctor]], and except as described in
|
| 2867 |
+
[[allocator.members]]. The lifetime of an object *o* of type `T` ends
|
| 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]].
|
| 2874 |
+
|
| 2875 |
+
The lifetime of a reference begins when its initialization is complete.
|
| 2876 |
+
The lifetime of a reference ends as if it were a scalar object requiring
|
| 2877 |
+
storage.
|
| 2878 |
+
|
| 2879 |
+
[*Note 1*: [[class.base.init]] describes the lifetime of base and
|
| 2880 |
+
member subobjects. — *end note*]
|
| 2881 |
+
|
| 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 might not be the same as the behavior of an object whose
|
| 2890 |
+
lifetime has started and not ended. [[class.base.init]] and
|
| 2891 |
+
[[class.cdtor]] describe the behavior of an object during its periods of
|
| 2892 |
+
construction and destruction. — *end note*]
|
| 2893 |
+
|
| 2894 |
+
A program may end the lifetime of any object by reusing the storage
|
| 2895 |
+
which the object occupies or by explicitly calling a destructor or
|
| 2896 |
+
pseudo-destructor [[expr.prim.id.dtor]] for the object. For an object of
|
| 2897 |
+
a class type, the program is not required to call the destructor
|
| 2898 |
+
explicitly before the storage which the object occupies is reused or
|
| 2899 |
+
released; however, if there is no explicit call to the destructor or if
|
| 2900 |
+
a *delete-expression* [[expr.delete]] is not used to release the
|
| 2901 |
+
storage, the destructor is not implicitly called and any program that
|
| 2902 |
+
depends on the side effects produced by the destructor has undefined
|
| 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[^11] or, after the lifetime of
|
| 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 |
+
- the object will be or was of a class type with a non-trivial
|
| 2918 |
+
destructor and the pointer is used as the operand of a
|
| 2919 |
+
*delete-expression*,
|
| 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`
|
| 2925 |
+
[[expr.static.cast]], except when the conversion is to pointer to
|
| 2926 |
+
cv `void`, or to pointer to cv `void` and subsequently to pointer to
|
| 2927 |
+
cv `char`, cv `unsigned char`, or cv `std::byte` [[cstddef.syn]], or
|
| 2928 |
+
- the pointer is used as the operand of a `dynamic_cast`
|
| 2929 |
+
[[expr.dynamic.cast]].
|
| 2930 |
+
|
| 2931 |
+
[*Example 1*:
|
| 2932 |
+
|
| 2933 |
+
``` cpp
|
| 2934 |
+
#include <cstdlib>
|
| 2935 |
+
|
| 2936 |
+
struct B {
|
| 2937 |
+
virtual void f();
|
| 2938 |
+
void mutate();
|
| 2939 |
+
virtual ~B();
|
| 2940 |
+
};
|
| 2941 |
+
|
| 2942 |
+
struct D1 : B { void f(); };
|
| 2943 |
+
struct D2 : B { void f(); };
|
| 2944 |
+
|
| 2945 |
+
void B::mutate() {
|
| 2946 |
+
new (this) D2; // reuses storage --- ends the lifetime of *this
|
| 2947 |
+
f(); // undefined behavior
|
| 2948 |
+
... = this; // OK, this points to valid memory
|
| 2949 |
+
}
|
| 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: pb points to valid memory
|
| 2956 |
+
void* q = pb; // OK: pb points to valid memory
|
| 2957 |
+
pb->f(); // undefined behavior: lifetime of *pb has ended
|
| 2958 |
+
}
|
| 2959 |
+
```
|
| 2960 |
+
|
| 2961 |
+
— *end example*]
|
| 2962 |
+
|
| 2963 |
+
Similarly, before the lifetime of an object has started but after the
|
| 2964 |
+
storage which the object will occupy has been allocated or, after the
|
| 2965 |
+
lifetime of an object has ended and before the storage which the object
|
| 2966 |
+
occupied is reused or released, any glvalue that refers to the original
|
| 2967 |
+
object may be used but only in limited ways. For an object under
|
| 2968 |
+
construction or destruction, see [[class.cdtor]]. Otherwise, such a
|
| 2969 |
+
glvalue refers to allocated storage [[basic.stc.dynamic.allocation]],
|
| 2970 |
+
and using the properties of the glvalue that do not depend on its value
|
| 2971 |
+
is well-defined. The program has undefined behavior if:
|
| 2972 |
+
|
| 2973 |
+
- the glvalue is used to access the object, or
|
| 2974 |
+
- the glvalue is used to call a non-static member function of the
|
| 2975 |
+
object, or
|
| 2976 |
+
- the glvalue is bound to a reference to a virtual base class
|
| 2977 |
+
[[dcl.init.ref]], or
|
| 2978 |
+
- the glvalue is used as the operand of a `dynamic_cast`
|
| 2979 |
+
[[expr.dynamic.cast]] or as the operand of `typeid`.
|
| 2980 |
+
|
| 2981 |
+
If, after the lifetime of an object has ended and before the storage
|
| 2982 |
+
which the object occupied is reused or released, a new object is created
|
| 2983 |
+
at the storage location which the original object occupied, a pointer
|
| 2984 |
+
that pointed to the original object, a reference that referred to the
|
| 2985 |
+
original object, or the name of the original object will automatically
|
| 2986 |
+
refer to the new object and, once the lifetime of the new object has
|
| 2987 |
+
started, can be used to manipulate the new object, if the original
|
| 2988 |
+
object is transparently replaceable (see below) by the new object. An
|
| 2989 |
+
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 complete const object, and
|
| 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₂.
|
| 3001 |
+
|
| 3002 |
+
[*Example 2*:
|
| 3003 |
+
|
| 3004 |
+
``` cpp
|
| 3005 |
+
struct C {
|
| 3006 |
+
int i;
|
| 3007 |
+
void f();
|
| 3008 |
+
const C& operator=( const C& );
|
| 3009 |
+
};
|
| 3010 |
+
|
| 3011 |
+
const C& C::operator=( const C& other) {
|
| 3012 |
+
if ( this != &other ) {
|
| 3013 |
+
this->~C(); // lifetime of *this ends
|
| 3014 |
+
new (this) C(other); // new object of type C created
|
| 3015 |
+
f(); // well-defined
|
| 3016 |
+
}
|
| 3017 |
+
return *this;
|
| 3018 |
+
}
|
| 3019 |
+
|
| 3020 |
+
C c1;
|
| 3021 |
+
C c2;
|
| 3022 |
+
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 3*: If these conditions are not met, a pointer to the new object
|
| 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,[^12] the program must ensure that an object of the original
|
| 3036 |
+
type occupies that same storage location when the implicit destructor
|
| 3037 |
+
call takes place; otherwise the behavior of the program is undefined.
|
| 3038 |
+
This is true even if the block is exited with an exception.
|
| 3039 |
+
|
| 3040 |
+
[*Example 3*:
|
| 3041 |
+
|
| 3042 |
+
``` cpp
|
| 3043 |
+
class T { };
|
| 3044 |
+
struct B {
|
| 3045 |
+
~B();
|
| 3046 |
+
};
|
| 3047 |
+
|
| 3048 |
+
void h() {
|
| 3049 |
+
B b;
|
| 3050 |
+
new (&b) T;
|
| 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*:
|
| 3062 |
+
|
| 3063 |
+
``` cpp
|
| 3064 |
+
struct B {
|
| 3065 |
+
B();
|
| 3066 |
+
~B();
|
| 3067 |
+
};
|
| 3068 |
+
|
| 3069 |
+
const B b;
|
| 3070 |
+
|
| 3071 |
+
void h() {
|
| 3072 |
+
b.~B();
|
| 3073 |
+
new (const_cast<B*>(&b)) const B; // undefined behavior
|
| 3074 |
+
}
|
| 3075 |
+
```
|
| 3076 |
+
|
| 3077 |
+
— *end example*]
|
| 3078 |
+
|
| 3079 |
+
In this subclause, “before” and “after” refer to the “happens before”
|
| 3080 |
+
relation [[intro.multithread]].
|
| 3081 |
+
|
| 3082 |
+
[*Note 4*: Therefore, undefined behavior results if an object that is
|
| 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 |
+
|
| 3088 |
+
When storage for an object with automatic or dynamic storage duration is
|
| 3089 |
+
obtained, the object has an *indeterminate value*, and if no
|
| 3090 |
+
initialization is performed for the object, that object retains an
|
| 3091 |
+
indeterminate value until that value is replaced [[expr.ass]].
|
| 3092 |
+
|
| 3093 |
+
[*Note 1*: Objects with static or thread storage duration are
|
| 3094 |
+
zero-initialized, see [[basic.start.static]]. — *end note*]
|
| 3095 |
+
|
| 3096 |
+
If an indeterminate value is produced by an evaluation, the behavior is
|
| 3097 |
+
undefined except in the following cases:
|
| 3098 |
+
|
| 3099 |
+
- If an indeterminate value of unsigned ordinary character type
|
| 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 ([[conv.integral]],
|
| 3106 |
+
[[expr.type.conv]], [[expr.static.cast]], [[expr.cast]]) to an
|
| 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
|
| 3113 |
+
`std::byte` type is produced by the evaluation of the right operand of
|
| 3114 |
+
a simple assignment operator [[expr.ass]] whose first operand is an
|
| 3115 |
+
lvalue of unsigned ordinary character type or `std::byte` type, an
|
| 3116 |
+
indeterminate value replaces the value of the object referred to by
|
| 3117 |
+
the left operand.
|
| 3118 |
+
- If an indeterminate value of unsigned ordinary character type is
|
| 3119 |
+
produced by the evaluation of the initialization expression when
|
| 3120 |
+
initializing an object of unsigned ordinary character type, that
|
| 3121 |
+
object is initialized to an indeterminate value.
|
| 3122 |
+
- If an indeterminate value of unsigned ordinary character type or
|
| 3123 |
+
`std::byte` type is produced by the evaluation of the initialization
|
| 3124 |
+
expression when initializing an object of `std::byte` type, that
|
| 3125 |
+
object is initialized to an indeterminate value.
|
| 3126 |
+
|
| 3127 |
+
[*Example 1*:
|
| 3128 |
+
|
| 3129 |
+
``` cpp
|
| 3130 |
+
int f(bool b) {
|
| 3131 |
+
unsigned char c;
|
| 3132 |
+
unsigned char d = c; // OK, d has an indeterminate value
|
| 3133 |
+
int e = d; // undefined behavior
|
| 3134 |
+
return b ? d : 0; // undefined behavior if b is true
|
| 3135 |
+
}
|
| 3136 |
+
```
|
| 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:
|
|
|
|
| 3148 |
- thread storage duration
|
| 3149 |
- automatic storage duration
|
| 3150 |
- dynamic storage duration
|
| 3151 |
|
| 3152 |
Static, thread, and automatic storage durations are associated with
|
| 3153 |
+
objects introduced by declarations [[basic.def]] and implicitly created
|
| 3154 |
+
by the implementation [[class.temporary]]. The dynamic storage duration
|
| 3155 |
+
is associated with objects created by a *new-expression* [[expr.new]].
|
|
|
|
| 3156 |
|
| 3157 |
The storage duration categories apply to references as well.
|
| 3158 |
|
| 3159 |
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.[^13]
|
| 3166 |
|
| 3167 |
+
#### Static storage duration <a id="basic.stc.static">[[basic.stc.static]]</a>
|
| 3168 |
|
| 3169 |
All variables which do not have dynamic storage duration, do not have
|
| 3170 |
thread storage duration, and are not local have *static storage
|
| 3171 |
+
duration*. The storage for these entities lasts for the duration of the
|
| 3172 |
+
program ([[basic.start.static]], [[basic.start.term]]).
|
| 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 local variable with static
|
| 3180 |
storage duration.
|
| 3181 |
|
| 3182 |
[*Note 1*: [[stmt.dcl]] describes the initialization of local `static`
|
|
|
|
| 3184 |
`static` variables. — *end note*]
|
| 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
|
| 3193 |
duration of the thread in which they are created. There is a distinct
|
| 3194 |
object or reference per thread, and use of the declared name refers to
|
| 3195 |
the entity associated with the current thread.
|
| 3196 |
|
| 3197 |
+
[*Note 1*: A variable with thread storage duration is initialized as
|
| 3198 |
+
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 |
Block-scope variables not explicitly declared `static`, `thread_local`,
|
| 3205 |
or `extern` have *automatic storage duration*. The storage for these
|
| 3206 |
entities lasts until the block in which they are created exits.
|
| 3207 |
|
|
|
|
| 3210 |
|
| 3211 |
If a variable with automatic storage duration has initialization or a
|
| 3212 |
destructor with side effects, an implementation shall not destroy it
|
| 3213 |
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 |
new[]` and the global *deallocation functions* `operator
|
| 3225 |
delete` and `operator delete[]`.
|
| 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]]. A C++ program shall provide at
|
| 3234 |
+
most one definition of a replaceable allocation or deallocation
|
| 3235 |
function. Any such function definition replaces the default version
|
| 3236 |
+
provided in the library [[replacement.functions]]. The following
|
| 3237 |
+
allocation and deallocation functions [[support.dynamic]] are implicitly
|
| 3238 |
+
declared in global scope in each translation unit of a program.
|
|
|
|
| 3239 |
|
| 3240 |
``` cpp
|
| 3241 |
+
[[nodiscard]] void* operator new(std::size_t);
|
| 3242 |
+
[[nodiscard]] void* operator new(std::size_t, std::align_val_t);
|
| 3243 |
|
| 3244 |
void operator delete(void*) noexcept;
|
| 3245 |
void operator delete(void*, std::size_t) noexcept;
|
| 3246 |
void operator delete(void*, std::align_val_t) noexcept;
|
| 3247 |
void operator delete(void*, std::size_t, std::align_val_t) noexcept;
|
| 3248 |
|
| 3249 |
+
[[nodiscard]] void* operator new[](std::size_t);
|
| 3250 |
+
[[nodiscard]] void* operator new[](std::size_t, std::align_val_t);
|
| 3251 |
|
| 3252 |
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;
|
|
|
|
| 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>` is
|
| 3267 |
+
well-formed. However, referring to `std` or `std::size_t` or
|
| 3268 |
+
`std::align_val_t` is ill-formed unless the name has been declared by
|
| 3269 |
+
importing or including the appropriate header. — *end note*]
|
| 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
|
| 3275 |
+
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 shall be a class member function or a global
|
| 3282 |
function; a program is ill-formed if an allocation function is declared
|
| 3283 |
in a namespace scope other than global scope or declared static in
|
| 3284 |
global scope. The return type shall be `void*`. The first parameter
|
| 3285 |
+
shall have type `std::size_t` [[support.types]]. The first parameter
|
| 3286 |
+
shall not have an associated default argument [[dcl.fct.default]]. The
|
| 3287 |
+
value of the first parameter is interpreted as the requested size of the
|
| 3288 |
+
allocation. An allocation function can be a function template. Such a
|
| 3289 |
+
template shall declare its return type and first parameter as specified
|
| 3290 |
+
above (that is, template parameter types shall not be used in the return
|
| 3291 |
+
type and first parameter type). Template allocation functions shall have
|
| 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
|
| 3298 |
+
allocated by successive calls to an allocation function are unspecified.
|
| 3299 |
+
Even if the size of the space requested is zero, the request can fail.
|
| 3300 |
+
If the request succeeds, the value returned by a replaceable allocation
|
| 3301 |
+
function is a non-null pointer value [[basic.compound]] `p0` different
|
| 3302 |
+
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.[^14]
|
| 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 |
+
|
| 3314 |
+
- If the allocation function takes an argument of type
|
| 3315 |
+
`std::align_val_t`, the storage will have the alignment specified by
|
| 3316 |
+
the value of this argument.
|
| 3317 |
+
- Otherwise, if the allocation function is named `operator new[]`, the
|
| 3318 |
+
storage is aligned for any object that does not have new-extended
|
| 3319 |
+
alignment [[basic.align]] and is no larger than the requested size.
|
| 3320 |
+
- Otherwise, the storage is aligned for any object that does not have
|
| 3321 |
+
new-extended alignment and is of the requested size.
|
| 3322 |
|
| 3323 |
An allocation function that fails to allocate storage can invoke the
|
| 3324 |
+
currently installed new-handler function [[new.handler]], if any.
|
| 3325 |
|
| 3326 |
+
[*Note 3*: A program-supplied allocation function can obtain the
|
| 3327 |
address of the currently installed `new_handler` using the
|
| 3328 |
+
`std::get_new_handler` function [[get.new.handler]]. — *end note*]
|
| 3329 |
|
| 3330 |
+
An allocation function that has a non-throwing exception specification
|
| 3331 |
+
[[except.spec]] indicates failure by returning a null pointer value. Any
|
| 3332 |
+
other allocation function never returns a null pointer value and
|
| 3333 |
+
indicates failure only by throwing an exception [[except.throw]] of a
|
| 3334 |
+
type that would match a handler [[except.handle]] of type
|
| 3335 |
+
`std::bad_alloc` [[bad.alloc]].
|
| 3336 |
|
| 3337 |
A global allocation function is only called as the result of a new
|
| 3338 |
+
expression [[expr.new]], or called directly using the function call
|
| 3339 |
+
syntax [[expr.call]], or called indirectly to allocate storage for a
|
| 3340 |
+
coroutine state [[dcl.fct.def.coroutine]], or called indirectly through
|
| 3341 |
+
calls to the functions in the C++ standard library.
|
| 3342 |
|
| 3343 |
+
[*Note 4*: In particular, a global allocation function is not called to
|
| 3344 |
+
allocate storage for objects with static storage duration
|
| 3345 |
+
[[basic.stc.static]], for objects or references with thread storage
|
| 3346 |
+
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 |
Deallocation functions shall be class member functions or global
|
| 3353 |
functions; a program is ill-formed if deallocation functions are
|
| 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`.
|
| 3361 |
|
| 3362 |
+
[*Note 5*: Array deletion cannot use a destroying operator
|
| 3363 |
+
delete. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 3364 |
|
| 3365 |
+
Each deallocation function shall return `void`. If the function is a
|
| 3366 |
+
destroying operator delete declared in class type `C`, the type of its
|
| 3367 |
+
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` [^15], then
|
| 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
|
| 3379 |
+
parameter. A deallocation function template shall have two or more
|
| 3380 |
+
function parameters. A template instance is never a usual deallocation
|
| 3381 |
+
function, regardless of its signature.
|
| 3382 |
|
| 3383 |
If a deallocation function terminates by throwing an exception, the
|
| 3384 |
behavior is undefined. The value of the first argument supplied to a
|
| 3385 |
deallocation function may be a null pointer value; if so, and if the
|
| 3386 |
deallocation function is one supplied in the standard library, the call
|
| 3387 |
has no effect.
|
| 3388 |
|
| 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
|
|
|
|
| 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* ([[basic.fundamental]],
|
| 3467 |
+
[[basic.compound]]) which place restrictions on the addresses at which
|
| 3468 |
+
an object of that type may be allocated. An *alignment* is an
|
| 3469 |
+
*implementation-defined* integer value representing the number of bytes
|
| 3470 |
+
between successive addresses at which a given object can be allocated.
|
| 3471 |
+
An object type imposes an alignment requirement on every object of that
|
| 3472 |
+
type; stricter alignment can be requested using the alignment specifier
|
| 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 might be different
|
| 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 |
|
| 3484 |
+
``` cpp
|
| 3485 |
+
struct B { long double d; };
|
| 3486 |
+
struct D : virtual B { char c; };
|
| 3487 |
+
```
|
| 3488 |
+
|
| 3489 |
+
When `D` is the type of a complete object, it will have a subobject of
|
| 3490 |
+
type `B`, so it must be aligned appropriately for a `long double`. If
|
| 3491 |
+
`D` appears as a subobject of another object that also has `B` as a
|
| 3492 |
+
virtual base class, the `B` subobject might be part of a different
|
| 3493 |
+
subobject, reducing the alignment requirements on the `D` subobject.
|
| 3494 |
+
|
| 3495 |
+
— *end example*]
|
| 3496 |
+
|
| 3497 |
+
The result of the `alignof` operator reflects the alignment requirement
|
| 3498 |
+
of the type in the complete-object case.
|
| 3499 |
+
|
| 3500 |
+
An *extended alignment* is represented by an alignment greater than
|
| 3501 |
+
`alignof(std::max_align_t)`. It is *implementation-defined* whether any
|
| 3502 |
+
extended alignments are supported and the contexts in which they are
|
| 3503 |
+
supported [[dcl.align]]. A type having an extended alignment requirement
|
| 3504 |
+
is an *over-aligned type*.
|
| 3505 |
+
|
| 3506 |
+
[*Note 1*: Every over-aligned type is or contains a class type to which
|
| 3507 |
+
extended alignment applies (possibly through a non-static data
|
| 3508 |
+
member). — *end note*]
|
| 3509 |
+
|
| 3510 |
+
A *new-extended alignment* is represented by an alignment greater than
|
| 3511 |
+
`__STDCPP_DEFAULT_NEW_ALIGNMENT__` [[cpp.predefined]].
|
| 3512 |
+
|
| 3513 |
+
Alignments are represented as values of the type `std::size_t`. Valid
|
| 3514 |
+
alignments include only those values returned by an `alignof` expression
|
| 3515 |
+
for the fundamental types plus an additional *implementation-defined*
|
| 3516 |
+
set of values, which may be empty. Every alignment value shall be a
|
| 3517 |
+
non-negative integral power of two.
|
| 3518 |
+
|
| 3519 |
+
Alignments have an order from *weaker* to *stronger* or *stricter*
|
| 3520 |
+
alignments. Stricter alignments have larger alignment values. An address
|
| 3521 |
+
that satisfies an alignment requirement also satisfies any weaker valid
|
| 3522 |
+
alignment requirement.
|
| 3523 |
+
|
| 3524 |
+
The alignment requirement of a complete type can be queried using an
|
| 3525 |
+
`alignof` expression [[expr.alignof]]. Furthermore, the narrow character
|
| 3526 |
+
types [[basic.fundamental]] shall have the weakest alignment
|
| 3527 |
+
requirement.
|
| 3528 |
+
|
| 3529 |
+
[*Note 2*: This enables the ordinary character types to be used as the
|
| 3530 |
+
underlying type for an aligned memory area [[dcl.align]]. — *end note*]
|
| 3531 |
+
|
| 3532 |
+
Comparing alignments is meaningful and provides the obvious results:
|
| 3533 |
+
|
| 3534 |
+
- Two alignments are equal when their numeric values are equal.
|
| 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; the aligned-storage
|
| 3541 |
+
templates in the library [[meta.trans.other]] can be used to obtain
|
| 3542 |
+
aligned storage. — *end note*]
|
| 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-copyable type (see below), and
|
| 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.prop]], all the semantic restrictions shall be respected as if
|
| 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 |
+
[[expr.call]], no temporary is introduced, so the foregoing does not
|
| 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 ([[dcl.init.ref]],
|
| 3575 |
+
[[expr.type.conv]], [[expr.dynamic.cast]], [[expr.static.cast]],
|
| 3576 |
+
[[expr.const.cast]], [[expr.cast]]),
|
| 3577 |
+
- when performing member access on a class prvalue ([[expr.ref]],
|
| 3578 |
+
[[expr.mptr.oper]]),
|
| 3579 |
+
- when performing an array-to-pointer conversion or subscripting on an
|
| 3580 |
+
array prvalue ([[conv.array]], [[expr.sub]]),
|
| 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 ([[expr.typeid]], [[expr.sizeof]]),
|
| 3584 |
and
|
| 3585 |
+
- when a prvalue that has type other than cv `void` appears as a
|
| 3586 |
+
discarded-value expression [[expr.prop]].
|
| 3587 |
|
| 3588 |
+
— *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3589 |
|
| 3590 |
[*Example 1*:
|
| 3591 |
|
| 3592 |
+
Consider the following code:
|
| 3593 |
+
|
| 3594 |
``` cpp
|
| 3595 |
+
class X {
|
| 3596 |
+
public:
|
| 3597 |
+
X(int);
|
| 3598 |
+
X(const X&);
|
| 3599 |
+
X& operator=(const X&);
|
| 3600 |
+
~X();
|
| 3601 |
};
|
| 3602 |
|
| 3603 |
+
class Y {
|
| 3604 |
+
public:
|
| 3605 |
+
Y(int);
|
| 3606 |
+
Y(Y&&);
|
| 3607 |
+
~Y();
|
| 3608 |
+
};
|
| 3609 |
|
| 3610 |
+
X f(X);
|
| 3611 |
+
Y g(Y);
|
|
|
|
|
|
|
|
|
|
| 3612 |
|
| 3613 |
+
void h() {
|
| 3614 |
+
X a(1);
|
| 3615 |
+
X b = f(X(2));
|
| 3616 |
+
Y c = g(Y(3));
|
| 3617 |
+
a = f(a);
|
|
|
|
|
|
|
| 3618 |
}
|
| 3619 |
```
|
| 3620 |
|
| 3621 |
+
`X(2)` is constructed in the space used to hold `f()`’s argument and
|
| 3622 |
+
`Y(3)` is constructed in the space used to hold `g()`’s argument.
|
| 3623 |
+
Likewise, `f()`’s result is constructed directly in `b` and `g()`’s
|
| 3624 |
+
result is constructed directly in `c`. On the other hand, the expression
|
| 3625 |
+
`a = f(a)` requires a temporary for the result of `f(a)`, which is
|
| 3626 |
+
materialized so that the reference parameter of `X::operator=(const X&)`
|
| 3627 |
+
can bind to it.
|
| 3628 |
+
|
| 3629 |
— *end example*]
|
| 3630 |
|
| 3631 |
+
When an object of class type `X` is passed to or returned from a
|
| 3632 |
+
function, if `X` has at least one eligible copy or move constructor
|
| 3633 |
+
[[special]], each such constructor is trivial, and the destructor of `X`
|
| 3634 |
+
is either trivial or deleted, implementations are permitted to create a
|
| 3635 |
+
temporary object to hold the function parameter or result object. The
|
| 3636 |
+
temporary object is constructed from the function argument or return
|
| 3637 |
+
value, respectively, and the function’s parameter or return object is
|
| 3638 |
+
initialized as if by using the eligible trivial constructor to copy the
|
| 3639 |
+
temporary (even if that constructor is inaccessible or would not be
|
| 3640 |
+
selected by overload resolution to perform a copy or move of the
|
| 3641 |
+
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 ([[class.default.ctor]],
|
| 3648 |
+
[[class.copy.ctor]]), it shall ensure that a constructor is called for
|
| 3649 |
+
the temporary object. Similarly, the destructor shall be called for a
|
| 3650 |
+
temporary with a non-trivial destructor [[class.dtor]]. Temporary
|
| 3651 |
+
objects are destroyed as the last step in evaluating the full-expression
|
| 3652 |
+
[[intro.execution]] that (lexically) contains the point where they were
|
| 3653 |
+
created. This is true even if that evaluation ends in throwing an
|
| 3654 |
+
exception. The value computations and side effects of destroying a
|
| 3655 |
+
temporary object are associated only with the full-expression, not with
|
| 3656 |
+
any specific subexpression.
|
| 3657 |
|
| 3658 |
+
There are three contexts in which temporaries are destroyed at a
|
| 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 ([[expr.prim.lambda.capture]],
|
| 3664 |
+
[[class.copy.ctor]]). In either case, if the constructor has one or more
|
| 3665 |
+
default arguments, the destruction of every temporary created in a
|
| 3666 |
+
default argument is sequenced before the construction of the next array
|
| 3667 |
+
element, if any.
|
| 3668 |
+
|
| 3669 |
+
The third context is when a reference is bound to a temporary
|
| 3670 |
+
object.[^17] The temporary object to which the reference is bound or the
|
| 3671 |
+
temporary object that is the complete object of a subobject to which the
|
| 3672 |
+
reference is bound persists for the lifetime of the reference if the
|
| 3673 |
+
glvalue to which the reference is bound was obtained through one of the
|
| 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,
|
| 3680 |
+
- a class member access [[expr.ref]] using the `.` operator where the
|
| 3681 |
+
left operand is one of these expressions and the right operand
|
| 3682 |
+
designates a non-static data member of non-reference type,
|
| 3683 |
+
- a pointer-to-member operation [[expr.mptr.oper]] using the `.*`
|
| 3684 |
+
operator where the left operand is one of these expressions and the
|
| 3685 |
+
right operand is a pointer to data member of non-reference type,
|
| 3686 |
+
- a
|
| 3687 |
+
- `const_cast` [[expr.const.cast]],
|
| 3688 |
+
- `static_cast` [[expr.static.cast]],
|
| 3689 |
+
- `dynamic_cast` [[expr.dynamic.cast]], or
|
| 3690 |
+
- `reinterpret_cast` [[expr.reinterpret.cast]]
|
| 3691 |
+
|
| 3692 |
+
converting, without a user-defined conversion, a glvalue operand that
|
| 3693 |
+
is one of these expressions to a glvalue that refers to the object
|
| 3694 |
+
designated by the operand, or to its complete object or a subobject
|
| 3695 |
+
thereof,
|
| 3696 |
+
- a conditional expression [[expr.cond]] that is a glvalue where the
|
| 3697 |
+
second or third operand is one of these expressions, or
|
| 3698 |
+
- a comma expression [[expr.comma]] that is a glvalue where the right
|
| 3699 |
+
operand is one of these expressions.
|
| 3700 |
|
| 3701 |
[*Example 2*:
|
| 3702 |
|
| 3703 |
``` cpp
|
| 3704 |
+
template<typename T> using id = T;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3705 |
|
| 3706 |
+
int i = 1;
|
| 3707 |
+
int&& a = id<int[3]>{1, 2, 3}[i]; // temporary array has same lifetime as a
|
| 3708 |
+
const int& b = static_cast<const int&>(0); // temporary int has same lifetime as b
|
| 3709 |
+
int&& c = cond ? id<int[3]>{1, 2, 3}[i] : static_cast<int&&>(0);
|
| 3710 |
+
// exactly one of the two temporaries is lifetime-extended
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3711 |
```
|
| 3712 |
|
| 3713 |
— *end example*]
|
| 3714 |
|
| 3715 |
+
[*Note 5*:
|
|
|
|
|
|
|
| 3716 |
|
| 3717 |
+
An explicit type conversion ([[expr.type.conv]], [[expr.cast]]) is
|
| 3718 |
+
interpreted as a sequence of elementary casts, covered above.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3719 |
|
| 3720 |
[*Example 3*:
|
| 3721 |
|
| 3722 |
``` cpp
|
| 3723 |
+
const int& x = (const int&)1; // temporary for value 1 has same lifetime as x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3724 |
```
|
| 3725 |
|
| 3726 |
— *end example*]
|
| 3727 |
|
| 3728 |
+
— *end note*]
|
| 3729 |
+
|
| 3730 |
+
[*Note 6*:
|
| 3731 |
+
|
| 3732 |
+
If a temporary object has a reference member initialized by another
|
| 3733 |
+
temporary object, lifetime extension applies recursively to such a
|
| 3734 |
+
member’s initializer.
|
| 3735 |
|
| 3736 |
[*Example 4*:
|
| 3737 |
|
| 3738 |
``` cpp
|
| 3739 |
+
struct S {
|
| 3740 |
+
const int& m;
|
|
|
|
| 3741 |
};
|
| 3742 |
+
const S& s = S{1}; // both S and int temporaries have lifetime of s
|
| 3743 |
+
```
|
| 3744 |
+
|
| 3745 |
+
— *end example*]
|
| 3746 |
|
| 3747 |
+
— *end note*]
|
| 3748 |
|
| 3749 |
+
The exceptions to this lifetime rule are:
|
| 3750 |
+
|
| 3751 |
+
- A temporary object bound to a reference parameter in a function call
|
| 3752 |
+
[[expr.call]] persists until the completion of the full-expression
|
| 3753 |
+
containing the call.
|
| 3754 |
+
- A temporary object bound to a reference element of an aggregate of
|
| 3755 |
+
class type initialized from a parenthesized *expression-list*
|
| 3756 |
+
[[dcl.init]] persists until the completion of the full-expression
|
| 3757 |
+
containing the *expression-list*.
|
| 3758 |
+
- The lifetime of a temporary bound to the returned value in a function
|
| 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 may introduce a dangling reference. — *end note*]
|
| 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 destruction of a temporary whose lifetime is not extended by being
|
| 3775 |
+
bound to a reference is sequenced before the destruction of every
|
| 3776 |
+
temporary which is constructed earlier in the same full-expression. If
|
| 3777 |
+
the lifetime of two or more temporaries to which references are bound
|
| 3778 |
+
ends at the same point, these temporaries are destroyed at that point in
|
| 3779 |
+
the reverse order of the completion of their construction. In addition,
|
| 3780 |
+
the destruction of temporaries bound to references shall take into
|
| 3781 |
+
account the ordering of destruction of objects with static, thread, or
|
| 3782 |
+
automatic storage duration ([[basic.stc.static]], [[basic.stc.thread]],
|
| 3783 |
+
[[basic.stc.auto]]); that is, if `obj1` is an object with the same
|
| 3784 |
+
storage duration as the temporary and created before the temporary is
|
| 3785 |
+
created the temporary shall be destroyed before `obj1` is destroyed; if
|
| 3786 |
+
`obj2` is an object with the same storage duration as the temporary and
|
| 3787 |
+
created after the temporary is created the temporary shall be destroyed
|
| 3788 |
+
after `obj2` is destroyed.
|
| 3789 |
+
|
| 3790 |
+
[*Example 6*:
|
| 3791 |
|
| 3792 |
+
``` cpp
|
| 3793 |
+
struct S {
|
| 3794 |
+
S();
|
| 3795 |
+
S(int);
|
| 3796 |
+
friend S operator+(const S&, const S&);
|
| 3797 |
+
~S();
|
| 3798 |
+
};
|
| 3799 |
+
S obj1;
|
| 3800 |
+
const S& cr = S(16)+S(23);
|
| 3801 |
+
S obj2;
|
| 3802 |
+
```
|
| 3803 |
+
|
| 3804 |
+
The expression `S(16) + S(23)` creates three temporaries: a first
|
| 3805 |
+
temporary `T1` to hold the result of the expression `S(16)`, a second
|
| 3806 |
+
temporary `T2` to hold the result of the expression `S(23)`, and a third
|
| 3807 |
+
temporary `T3` to hold the result of the addition of these two
|
| 3808 |
+
expressions. The temporary `T3` is then bound to the reference `cr`. It
|
| 3809 |
+
is unspecified whether `T1` or `T2` is created first. On an
|
| 3810 |
+
implementation where `T1` is created before `T2`, `T2` shall be
|
| 3811 |
+
destroyed before `T1`. The temporaries `T1` and `T2` are bound to the
|
| 3812 |
+
reference parameters of `operator+`; these temporaries are destroyed at
|
| 3813 |
+
the end of the full-expression containing the call to `operator+`. The
|
| 3814 |
+
temporary `T3` bound to the reference `cr` is destroyed at the end of
|
| 3815 |
+
`cr`’s lifetime, that is, at the end of the program. In addition, the
|
| 3816 |
+
order in which `T3` is destroyed takes into account the destruction
|
| 3817 |
+
order of other objects with static storage duration. That is, because
|
| 3818 |
+
`obj1` is constructed before `T3`, and `T3` is constructed before
|
| 3819 |
+
`obj2`, `obj2` shall be destroyed before `T3`, and `T3` shall be
|
| 3820 |
+
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]]. [^18] If the content of that array is
|
| 3837 |
+
copied back into the object, the object shall subsequently hold its
|
| 3838 |
+
original value.
|
| 3839 |
|
| 3840 |
[*Example 1*:
|
| 3841 |
|
| 3842 |
``` cpp
|
| 3843 |
+
constexpr std::size_t N = sizeof(T);
|
| 3844 |
char buf[N];
|
| 3845 |
T obj; // obj initialized to its original value
|
| 3846 |
std::memcpy(buf, &obj, N); // between these two calls to std::memcpy, obj might be modified
|
| 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 any trivially copyable type `T`, if two pointers to `T` point to
|
| 3853 |
distinct `T` objects `obj1` and `obj2`, where neither `obj1` nor `obj2`
|
| 3854 |
+
is a potentially-overlapping subobject, if the underlying bytes
|
| 3855 |
+
[[intro.memory]] making up `obj1` are copied into `obj2`,[^19] `obj2`
|
| 3856 |
+
shall subsequently hold the same value as `obj1`.
|
| 3857 |
|
| 3858 |
[*Example 2*:
|
| 3859 |
|
| 3860 |
``` cpp
|
| 3861 |
T* t1p;
|
|
|
|
| 3868 |
|
| 3869 |
— *end example*]
|
| 3870 |
|
| 3871 |
The *object representation* of an object of type `T` is the sequence of
|
| 3872 |
*N* `unsigned char` objects taken up by the object of type `T`, where
|
| 3873 |
+
*N* equals `sizeof(T)`. The *value representation* of an object of type
|
| 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.[^20]
|
| 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*. [^21]
|
| 3884 |
+
Incompletely-defined object types and cv `void` are *incomplete types*
|
| 3885 |
+
[[basic.fundamental]]. Objects shall not be defined to have an
|
| 3886 |
incomplete type.
|
| 3887 |
|
| 3888 |
A class type (such as “`class X`”) might be incomplete at one point in a
|
| 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 might be an
|
|
|
|
| 3908 |
typedef int UNKA[]; // UNKA is an incomplete type
|
| 3909 |
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: sizeof UNKA* is known
|
| 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; // error: different types
|
| 3925 |
xp++; // OK: X is complete
|
| 3926 |
+
arrp++; // error: UNKA can't be completed
|
| 3927 |
}
|
| 3928 |
```
|
| 3929 |
|
| 3930 |
— *end example*]
|
| 3931 |
|
|
|
|
| 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 |
|
| 3938 |
+
Arithmetic types [[basic.fundamental]], enumeration types, pointer
|
| 3939 |
+
types, pointer-to-member types [[basic.compound]], `std::nullptr_t`, and
|
| 3940 |
+
cv-qualified [[basic.type.qualifier]] versions of these types are
|
| 3941 |
+
collectively called *scalar types*. Scalar types, trivially copyable
|
| 3942 |
+
class types [[class.prop]], arrays of such types, and cv-qualified
|
| 3943 |
+
versions of these types are collectively called *trivially copyable
|
| 3944 |
+
types*. Scalar types, trivial class types [[class.prop]], arrays of such
|
| 3945 |
+
types and cv-qualified versions of these types are collectively called
|
| 3946 |
+
*trivial types*. Scalar types, standard-layout class types
|
| 3947 |
+
[[class.prop]], arrays of such types and cv-qualified versions of these
|
| 3948 |
+
types are collectively called *standard-layout types*. Scalar types,
|
| 3949 |
+
implicit-lifetime class types [[class.prop]], array types, and
|
| 3950 |
+
cv-qualified versions of these types are collectively called
|
| 3951 |
+
*implicit-lifetime types*.
|
| 3952 |
|
| 3953 |
A type is a *literal type* if it is:
|
| 3954 |
|
| 3955 |
+
- cv `void`; or
|
| 3956 |
- a scalar type; or
|
| 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 |
+
- it is either a closure type [[expr.prim.lambda.closure]], an
|
| 3963 |
+
aggregate type [[dcl.init.aggr]], or has at least one constexpr
|
| 3964 |
+
constructor or constructor template (possibly inherited
|
| 3965 |
+
[[namespace.udecl]] from a base class) that is not a copy or move
|
| 3966 |
constructor,
|
| 3967 |
- if it is a union, at least one of its non-static data members is of
|
| 3968 |
non-volatile literal type, and
|
| 3969 |
- if it is not a union, all of its non-static data members and base
|
| 3970 |
classes are of non-volatile literal types.
|
| 3971 |
|
| 3972 |
[*Note 3*: A literal type is one for which it might be possible to
|
| 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* types if
|
| 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>
|
| 3984 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3985 |
There are five *standard signed integer types* : “`signed char`”,
|
| 3986 |
“`short int`”, “`int`”, “`long int`”, and “`long long int`”. In this
|
| 3987 |
list, each type provides at least as much storage as those preceding it
|
| 3988 |
in the list. There may also be *implementation-defined* *extended signed
|
| 3989 |
integer types*. The standard and extended signed integer types are
|
| 3990 |
+
collectively called *signed integer types*. The range of representable
|
| 3991 |
+
values for a signed integer type is -2ᴺ⁻¹ to 2ᴺ⁻¹-1 (inclusive), where
|
| 3992 |
+
*N* is called the *width* of the type.
|
| 3993 |
+
|
| 3994 |
+
[*Note 1*: Plain `int`s are intended to have the natural width
|
| 3995 |
+
suggested by the architecture of the execution environment; the other
|
| 3996 |
+
signed integer types are provided to meet special needs. — *end note*]
|
| 3997 |
|
| 3998 |
For each of the standard signed integer types, there exists a
|
| 3999 |
corresponding (but different) *standard unsigned integer type*:
|
| 4000 |
“`unsigned char`”, “`unsigned short int`”, “`unsigned int`”,
|
| 4001 |
+
“`unsigned long int`”, and “`unsigned long long int`”. Likewise, for
|
| 4002 |
+
each of the extended signed integer types, there exists a corresponding
|
| 4003 |
+
*extended unsigned integer type*. The standard and extended unsigned
|
| 4004 |
+
integer types are collectively called *unsigned integer types*. An
|
| 4005 |
+
unsigned integer type has the same width *N* as the corresponding signed
|
| 4006 |
+
integer type. The range of representable values for the unsigned type is
|
| 4007 |
+
0 to 2ᴺ-1 (inclusive); arithmetic for the unsigned type is performed
|
| 4008 |
+
modulo 2ᴺ.
|
| 4009 |
+
|
| 4010 |
+
[*Note 2*: Unsigned arithmetic does not overflow. Overflow for signed
|
| 4011 |
+
arithmetic yields undefined behavior [[expr.pre]]. — *end note*]
|
| 4012 |
+
|
| 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.[^22]
|
| 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` | 16 |
|
| 4030 |
+
| `int` | 16 |
|
| 4031 |
+
| `long` | 32 |
|
| 4032 |
+
| `long long` | 64 |
|
| 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 |
+
|
| 4045 |
+
[*Note 4*: The signed and unsigned integer types satisfy the
|
| 4046 |
+
constraints given in ISO C 5.2.4.2.1. — *end note*]
|
| 4047 |
+
|
| 4048 |
+
Except as specified above, the width of a signed or unsigned integer
|
| 4049 |
+
type is *implementation-defined*.
|
| 4050 |
+
|
| 4051 |
+
Each value x of an unsigned integer type with width N has a unique
|
| 4052 |
+
representation $x = x_0 2^0 + x_1 2^1 + \ldots + x_{N-1} 2^{N-1}$, where
|
| 4053 |
+
each coefficient xᵢ is either 0 or 1; this is called the *base-2
|
| 4054 |
+
representation* of x. The base-2 representation of a value of signed
|
| 4055 |
+
integer type is the base-2 representation of the congruent value of the
|
| 4056 |
+
corresponding unsigned integer type. The standard signed integer types
|
| 4057 |
+
and standard unsigned integer types are collectively called the
|
| 4058 |
+
*standard integer types*, and the extended signed integer types and
|
| 4059 |
+
extended unsigned integer types are collectively called the *extended
|
| 4060 |
+
integer types*.
|
| 4061 |
+
|
| 4062 |
+
A fundamental type specified to have a signed or unsigned integer type
|
| 4063 |
+
as its *underlying type* has the same object representation, value
|
| 4064 |
+
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 values of type `char` can represent distinct codes for all members
|
| 4071 |
+
of the implementation’s basic character set. The three types `char`,
|
| 4072 |
+
`signed char`, and `unsigned char` are collectively called *ordinary
|
| 4073 |
+
character types*. The ordinary character types and `char8_t` are
|
| 4074 |
+
collectively called *narrow character types*. For narrow character
|
| 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. The values of
|
| 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>`.
|
| 4095 |
+
|
| 4096 |
+
Type `bool` is a distinct type that has the same object representation,
|
| 4097 |
+
value representation, and alignment requirements as an
|
| 4098 |
+
*implementation-defined* unsigned integer type. The values of type
|
| 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 |
+
Types `bool`, `char`, `wchar_t`, `char8_t`, `char16_t`, `char32_t`, and
|
| 4105 |
+
the signed and unsigned integer types are collectively called *integral
|
| 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 |
+
There are three *floating-point types*: `float`, `double`, and
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4113 |
`long double`. The type `double` provides at least as much precision as
|
| 4114 |
`float`, and the type `long double` provides at least as much precision
|
| 4115 |
as `double`. The set of values of the type `float` is a subset of the
|
| 4116 |
set of values of the type `double`; the set of values of the type
|
| 4117 |
`double` is a subset of the set of values of the type `long double`. The
|
| 4118 |
value representation of floating-point types is
|
| 4119 |
*implementation-defined*.
|
| 4120 |
|
| 4121 |
+
[*Note 9*: This document imposes no requirements on the accuracy of
|
| 4122 |
+
floating-point operations; see also [[support.limits]]. — *end note*]
|
|
|
|
| 4123 |
|
| 4124 |
+
Integral and floating-point types are collectively called *arithmetic*
|
| 4125 |
+
types. Specializations of the standard library template
|
| 4126 |
+
`std::numeric_limits` [[support.limits]] shall specify the maximum and
|
| 4127 |
+
minimum values of each arithmetic type for an implementation.
|
| 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` ([[expr.type.conv]], [[expr.static.cast]],
|
| 4133 |
+
[[expr.cast]]). An expression of type cv `void` shall be used only as an
|
| 4134 |
+
expression statement [[stmt.expr]], as an operand of a comma expression
|
| 4135 |
+
[[expr.comma]], as a second or third operand of `?:` [[expr.cond]], as
|
| 4136 |
+
the operand of `typeid`, `noexcept`, or `decltype`, as the expression in
|
| 4137 |
+
a `return` statement [[stmt.return]] for a function with the return type
|
| 4138 |
cv `void`, or as the operand of an explicit conversion to type
|
| 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 ([[conv.ptr]], [[conv.mem]]).
|
| 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 10*: Even if the implementation defines two or more fundamental
|
| 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]];
|
| 4157 |
- *functions*, which have parameters of given types and return `void` or
|
| 4158 |
+
references or objects of a given type, [[dcl.fct]];
|
| 4159 |
- *pointers* to cv `void` or objects or functions (including static
|
| 4160 |
+
members of classes) of a given type, [[dcl.ptr]];
|
| 4161 |
+
- *references* to objects or functions of a given type, [[dcl.ref]].
|
| 4162 |
There are two types of references:
|
| 4163 |
+
- lvalue reference
|
| 4164 |
+
- rvalue reference
|
| 4165 |
+
- *classes* containing a sequence of objects of various types [[class]],
|
| 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. Each
|
| 4172 |
+
distinct enumeration constitutes a different *enumerated type*,
|
| 4173 |
[[dcl.enum]];
|
| 4174 |
+
- *pointers to non-static class members*, [^23] which identify members
|
| 4175 |
+
of a given type within objects of a given class, [[dcl.mptr]].
|
| 4176 |
+
Pointers to data members and pointers to member functions are
|
| 4177 |
+
collectively called *pointer-to-member* types.
|
| 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]]
|
| 4183 |
+
is ill-formed.
|
| 4184 |
|
| 4185 |
The type of a pointer to cv `void` or a pointer to an object type is
|
| 4186 |
called an *object pointer type*.
|
| 4187 |
|
| 4188 |
[*Note 1*: A pointer to `void` does not have a pointer-to-object type,
|
| 4189 |
however, because `void` is not an object type. — *end note*]
|
| 4190 |
|
| 4191 |
The type of a pointer that can designate a function is called a
|
| 4192 |
+
*function pointer type*. A pointer to an object of type `T` is referred
|
| 4193 |
+
to as a “pointer to `T`”.
|
| 4194 |
|
| 4195 |
[*Example 1*: A pointer to an object of type `int` is referred to as
|
| 4196 |
“pointer to `int`” and a pointer to an object of class `X` is called a
|
| 4197 |
“pointer to `X`”. — *end example*]
|
| 4198 |
|
| 4199 |
Except for pointers to static members, text referring to “pointers” does
|
| 4200 |
not apply to pointers to members. Pointers to incomplete types are
|
| 4201 |
+
allowed although there are restrictions on what can be done with them
|
| 4202 |
+
[[basic.align]]. Every value of pointer type is one of the following:
|
| 4203 |
|
| 4204 |
- a *pointer to* an object or function (the pointer is said to *point*
|
| 4205 |
to the object or function), or
|
| 4206 |
+
- a *pointer past the end of* an object [[expr.add]], or
|
| 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 [^24] or the first byte in
|
| 4213 |
memory after the end of the storage occupied by the object,
|
| 4214 |
respectively.
|
| 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 that
|
| 4218 |
might be located at that address. A pointer value becomes invalid when
|
| 4219 |
the storage it denotes reaches the end of its storage duration; see
|
| 4220 |
[[basic.stc]]. — *end note*]
|
| 4221 |
|
| 4222 |
+
For purposes of pointer arithmetic [[expr.add]] and comparison (
|
| 4223 |
[[expr.rel]], [[expr.eq]]), a pointer past the end of the last element
|
| 4224 |
of an array `x` of n elements is considered to be equivalent to a
|
| 4225 |
+
pointer to a hypothetical array element n of `x` and an object of type
|
| 4226 |
+
`T` that is not an array element is considered to belong to an array
|
| 4227 |
+
with one element of type `T`. The value representation of pointer types
|
| 4228 |
+
is *implementation-defined*. Pointers to layout-compatible types shall
|
| 4229 |
+
have the same value representation and alignment requirements
|
| 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*]
|
| 4235 |
|
| 4236 |
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, or, if the object has no
|
| 4243 |
+
non-static data members, any base class subobject of that object
|
| 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
|
| 4250 |
address, and it is possible to obtain a pointer to one from a pointer to
|
| 4251 |
+
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*` shall have the same representation and alignment
|
| 4260 |
+
requirements as cv `char*`.
|
|
|
|
| 4261 |
|
| 4262 |
### CV-qualifiers <a id="basic.type.qualifier">[[basic.type.qualifier]]</a>
|
| 4263 |
|
| 4264 |
A type mentioned in [[basic.fundamental]] and [[basic.compound]] is a
|
| 4265 |
*cv-unqualified type*. Each type which is a cv-unqualified complete or
|
| 4266 |
+
incomplete object type or is `void` [[basic.types]] has three
|
| 4267 |
corresponding cv-qualified versions of its type: a *const-qualified*
|
| 4268 |
version, a *volatile-qualified* version, and a
|
| 4269 |
+
*const-volatile-qualified* version. The type of an object
|
| 4270 |
+
[[intro.object]] includes the *cv-qualifier*s specified in the
|
| 4271 |
+
*decl-specifier-seq* [[dcl.spec]], *declarator* [[dcl.decl]], *type-id*
|
| 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 |
The cv-qualified or cv-unqualified versions of a type are distinct
|
| 4284 |
types; however, they shall have the same representation and alignment
|
| 4285 |
+
requirements [[basic.align]].[^25]
|
| 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 1*: Cv-qualifiers applied to an array type attach to the
|
| 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*:
|
| 4300 |
+
|
| 4301 |
+
``` cpp
|
| 4302 |
+
typedef char CA[5];
|
| 4303 |
+
typedef const char CC;
|
| 4304 |
+
CC arr1[5] = { 0 };
|
| 4305 |
+
const CA arr2 = { 0 };
|
| 4306 |
+
```
|
| 4307 |
+
|
| 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 2*: See [[dcl.fct]] and [[class.this]] regarding function
|
| 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 |
|
| 4320 |
+
**Table: Relations on `const` and `volatile`** <a id="basic.type.qualifier.rel">[basic.type.qualifier.rel]</a>
|
| 4321 |
|
| 4322 |
| | | |
|
| 4323 |
| --------------- | --- | ---------------- |
|
| 4324 |
| no cv-qualifier | < | `const` |
|
| 4325 |
| no cv-qualifier | < | `volatile` |
|
| 4326 |
| no cv-qualifier | < | `const volatile` |
|
| 4327 |
| `const` | < | `const volatile` |
|
| 4328 |
| `volatile` | < | `const volatile` |
|
| 4329 |
|
| 4330 |
|
| 4331 |
+
In this document, the notation cv (or *cv1*, *cv2*, etc.), used in the
|
| 4332 |
+
description of types, represents an arbitrary set of cv-qualifiers,
|
| 4333 |
+
i.e., one of {`const`}, {`volatile`}, {`const`, `volatile`}, or the
|
| 4334 |
+
empty set. For a type cv `T`, the *top-level cv-qualifiers* of that type
|
| 4335 |
+
are those denoted by cv.
|
| 4336 |
|
| 4337 |
+
[*Example 2*: The type corresponding to the *type-id* `const int&` has
|
| 4338 |
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 |
+
### Integer conversion rank <a id="conv.rank">[[conv.rank]]</a>
|
| 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) shall have the same rank, even if they
|
| 4350 |
+
have the same representation.
|
| 4351 |
+
- The rank of a signed integer type shall be greater than the rank of
|
| 4352 |
+
any signed integer type with a smaller width.
|
| 4353 |
+
- The rank of `long long int` shall be greater than the rank of
|
| 4354 |
+
`long int`, which shall be greater than the rank of `int`, which shall
|
| 4355 |
+
be greater than the rank of `short int`, which shall be greater than
|
| 4356 |
+
the rank of `signed char`.
|
| 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 shall be greater than the rank
|
| 4360 |
+
of any extended integer type with the same width.
|
| 4361 |
+
- The rank of `char` shall equal the rank of `signed char` and
|
| 4362 |
+
`unsigned char`.
|
| 4363 |
+
- The rank of `bool` shall be less than the rank of all other standard
|
| 4364 |
+
integer types.
|
| 4365 |
+
- The ranks of `char8_t`, `char16_t`, `char32_t`, and `wchar_t` shall
|
| 4366 |
+
equal the ranks of their underlying types [[basic.fundamental]].
|
| 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` shall have
|
| 4373 |
+
greater rank than `T3`.
|
| 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
|
| 4384 |
+
[[basic.stc.auto]] is associated with each entry into its block. Such an
|
| 4385 |
+
object exists and retains its last-stored value during the execution of
|
| 4386 |
+
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
|
| 4397 |
+
*initializer-clause*.
|
| 4398 |
+
|
| 4399 |
+
[*Example 1*:
|
| 4400 |
+
|
| 4401 |
+
``` cpp
|
| 4402 |
+
struct A { int x; };
|
| 4403 |
+
struct B { int y; struct A a; };
|
| 4404 |
+
B b = { 5, { 1+1 } };
|
| 4405 |
+
```
|
| 4406 |
+
|
| 4407 |
+
The constituent expressions of the *initializer* used for the
|
| 4408 |
+
initialization of `b` are `5` and `1+1`.
|
| 4409 |
+
|
| 4410 |
+
— *end example*]
|
| 4411 |
+
|
| 4412 |
+
The *immediate subexpressions* of an expression E are
|
| 4413 |
+
|
| 4414 |
+
- the constituent expressions of E’s operands [[expr.prop]],
|
| 4415 |
+
- any function call that E implicitly invokes,
|
| 4416 |
+
- if E is a *lambda-expression* [[expr.prim.lambda]], the initialization
|
| 4417 |
+
of the entities captured by copy and the constituent expressions of
|
| 4418 |
+
the *initializer* of the *init-capture*s,
|
| 4419 |
+
- if E is a function call [[expr.call]] or implicitly invokes a
|
| 4420 |
+
function, the constituent expressions of each default argument
|
| 4421 |
+
[[dcl.fct.default]] used in the call, or
|
| 4422 |
+
- if E creates an aggregate object [[dcl.init.aggr]], the constituent
|
| 4423 |
+
expressions of each default member initializer [[class.mem]] used in
|
| 4424 |
+
the initialization.
|
| 4425 |
+
|
| 4426 |
+
A *subexpression* of an expression E is an immediate subexpression of E
|
| 4427 |
+
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.prop]],
|
| 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,
|
| 4441 |
+
- an invocation of a destructor generated at the end of the lifetime of
|
| 4442 |
+
an object other than a temporary object [[class.temporary]] whose
|
| 4443 |
+
lifetime has not been extended, or
|
| 4444 |
+
- an expression that is not a subexpression of another expression and
|
| 4445 |
+
that is not otherwise part of a full-expression.
|
| 4446 |
+
|
| 4447 |
+
If a language construct is defined to produce an implicit call of a
|
| 4448 |
+
function, a use of the language construct is considered to be an
|
| 4449 |
+
expression for the purposes of this definition. Conversions applied to
|
| 4450 |
+
the result of an expression in order to satisfy the requirements of the
|
| 4451 |
+
language construct in which the expression appears are also considered
|
| 4452 |
+
to be part of the full-expression. For an initializer, performing the
|
| 4453 |
+
initialization of the entity (including evaluating default member
|
| 4454 |
+
initializers of an aggregate) is also considered part of the
|
| 4455 |
+
full-expression.
|
| 4456 |
|
| 4457 |
[*Example 2*:
|
| 4458 |
|
| 4459 |
``` cpp
|
| 4460 |
+
struct S {
|
| 4461 |
+
S(int i): I(i) { } // full-expression is initialization of I
|
| 4462 |
+
int& v() { return I; }
|
| 4463 |
+
~S() noexcept(false) { }
|
| 4464 |
+
private:
|
| 4465 |
+
int I;
|
| 4466 |
+
};
|
| 4467 |
+
|
| 4468 |
+
S s1(1); // full-expression comprises call of S::S(int)
|
| 4469 |
+
void f() {
|
| 4470 |
+
S s2 = 2; // full-expression comprises call of S::S(int)
|
| 4471 |
+
if (S(3).v()) // full-expression includes lvalue-to-rvalue and int to bool conversions,
|
| 4472 |
+
// performed before temporary is deleted at end of full-expression
|
| 4473 |
+
{ }
|
| 4474 |
+
bool b = noexcept(S()); // exception specification of destructor of S considered for noexcept
|
| 4475 |
+
|
| 4476 |
+
// full-expression is destruction of s2 at end of block
|
| 4477 |
+
}
|
| 4478 |
+
struct B {
|
| 4479 |
+
B(S = S(0));
|
| 4480 |
+
};
|
| 4481 |
+
B b[2] = { B(), B() }; // full-expression is the entire initialization
|
| 4482 |
+
// including the destruction of temporaries
|
| 4483 |
```
|
| 4484 |
|
| 4485 |
+
— *end example*]
|
| 4486 |
+
|
| 4487 |
+
[*Note 2*: The evaluation of a full-expression can include the
|
| 4488 |
+
evaluation of subexpressions that are not lexically part of the
|
| 4489 |
+
full-expression. For example, subexpressions involved in evaluating
|
| 4490 |
+
default arguments [[dcl.fct.default]] are considered to be created in
|
| 4491 |
+
the expression that calls the function, not the expression that defines
|
| 4492 |
+
the default argument. — *end note*]
|
| 4493 |
+
|
| 4494 |
+
Reading an object designated by a `volatile` glvalue [[basic.lval]],
|
| 4495 |
+
modifying an object, calling a library I/O function, or calling a
|
| 4496 |
+
function that does any of those operations are all *side effects*, which
|
| 4497 |
+
are changes in the state of the execution environment. *Evaluation* of
|
| 4498 |
+
an expression (or a subexpression) in general includes both value
|
| 4499 |
+
computations (including determining the identity of an object for
|
| 4500 |
+
glvalue evaluation and fetching a value previously assigned to an object
|
| 4501 |
+
for prvalue evaluation) and initiation of side effects. When a call to a
|
| 4502 |
+
library I/O function returns or an access through a volatile glvalue is
|
| 4503 |
+
evaluated the side effect is considered complete, even though some
|
| 4504 |
+
external actions implied by the call (such as the I/O itself) or by the
|
| 4505 |
+
`volatile` access may not have completed yet.
|
| 4506 |
+
|
| 4507 |
+
*Sequenced before* is an asymmetric, transitive, pair-wise relation
|
| 4508 |
+
between evaluations executed by a single thread [[intro.multithread]],
|
| 4509 |
+
which induces a partial order among those evaluations. Given any two
|
| 4510 |
+
evaluations *A* and *B*, if *A* is sequenced before *B* (or,
|
| 4511 |
+
equivalently, *B* is *sequenced after* *A*), then the execution of *A*
|
| 4512 |
+
shall precede the execution of *B*. If *A* is not sequenced before *B*
|
| 4513 |
+
and *B* is not sequenced before *A*, then *A* and *B* are *unsequenced*.
|
| 4514 |
+
|
| 4515 |
+
[*Note 3*: The execution of unsequenced evaluations can
|
| 4516 |
+
overlap. — *end note*]
|
| 4517 |
+
|
| 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 could be executed first. — *end note*]
|
| 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.[^26]
|
| 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
|
| 4538 |
+
execution of a program, unsequenced and indeterminately sequenced
|
| 4539 |
+
evaluations of its subexpressions need not be performed consistently in
|
| 4540 |
+
different evaluations. — *end note*]
|
| 4541 |
+
|
| 4542 |
+
The value computations of the operands of an operator are sequenced
|
| 4543 |
+
before the value computation of the result of the operator. If a side
|
| 4544 |
+
effect on a memory location [[intro.memory]] is unsequenced relative to
|
| 4545 |
+
either another side effect on the same memory location or a value
|
| 4546 |
+
computation using the value of any object in the same memory location,
|
| 4547 |
+
and they are not potentially concurrent [[intro.multithread]], the
|
| 4548 |
+
behavior is undefined.
|
| 4549 |
+
|
| 4550 |
+
[*Note 6*: The next subclause imposes similar, but more complex
|
| 4551 |
+
restrictions on potentially concurrent computations. — *end note*]
|
| 4552 |
+
|
| 4553 |
+
[*Example 3*:
|
| 4554 |
+
|
| 4555 |
+
``` cpp
|
| 4556 |
+
void g(int i) {
|
| 4557 |
+
i = 7, i++, i++; // i becomes 9
|
| 4558 |
+
|
| 4559 |
+
i = i++ + 1; // the value of i is incremented
|
| 4560 |
+
i = i++ + i; // undefined behavior
|
| 4561 |
+
i = i + 1; // the value of i is incremented
|
| 4562 |
+
}
|
| 4563 |
+
```
|
| 4564 |
|
| 4565 |
— *end example*]
|
| 4566 |
|
| 4567 |
+
When calling a function (whether or not the function is inline), every
|
| 4568 |
+
value computation and side effect associated with any argument
|
| 4569 |
+
expression, or with the postfix expression designating the called
|
| 4570 |
+
function, is sequenced before execution of every expression or statement
|
| 4571 |
+
in the body of the called function. For each function invocation *F*,
|
| 4572 |
+
for every evaluation *A* that occurs within *F* and every evaluation *B*
|
| 4573 |
+
that does not occur within *F* but is evaluated on the same thread and
|
| 4574 |
+
as part of the same signal handler (if any), either *A* is sequenced
|
| 4575 |
+
before *B* or *B* is sequenced before *A*.[^27]
|
| 4576 |
+
|
| 4577 |
+
[*Note 7*: If *A* and *B* would not otherwise be sequenced then they
|
| 4578 |
+
are indeterminately sequenced. — *end note*]
|
| 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
|
| 4584 |
+
allocation and constructor functions; see [[expr.new]]. For another
|
| 4585 |
+
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 |
+
whatever the syntax of the expression that calls the function might be.
|
| 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 8*: When a signal is received for another reason, the execution
|
| 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.[^28] Under a hosted implementation, a C++ program
|
| 4614 |
+
can have more than one thread running concurrently. The execution of
|
| 4615 |
+
each thread proceeds as defined by the remainder of this document. The
|
| 4616 |
+
execution of the entire program consists of an execution of all of its
|
| 4617 |
+
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*]
|
| 4623 |
+
|
| 4624 |
+
Under a freestanding implementation, it is *implementation-defined*
|
| 4625 |
+
whether a program can have more than one thread of execution.
|
| 4626 |
+
|
| 4627 |
+
For a signal handler that is not executed as a result of a call to the
|
| 4628 |
+
`std::raise` function, it is unspecified which thread of execution
|
| 4629 |
+
contains the signal handler invocation.
|
| 4630 |
+
|
| 4631 |
+
#### Data races <a id="intro.races">[[intro.races]]</a>
|
| 4632 |
+
|
| 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 may instead be undefined behavior. Much
|
| 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
|
| 4645 |
+
location [[intro.memory]] and the other one reads or modifies the same
|
| 4646 |
+
memory location.
|
| 4647 |
+
|
| 4648 |
+
The library defines a number of atomic operations [[atomics]] and
|
| 4649 |
+
operations on mutexes [[thread]] that are specially identified as
|
| 4650 |
+
synchronization operations. These operations play a special role in
|
| 4651 |
+
making assignments in one thread visible to another. A synchronization
|
| 4652 |
+
operation on one or more memory locations is either a consume operation,
|
| 4653 |
+
an acquire operation, a release operation, or both an acquire and
|
| 4654 |
+
release operation. A synchronization operation without an associated
|
| 4655 |
+
memory location is a fence and can be either an acquire fence, a release
|
| 4656 |
+
fence, or both an acquire and release fence. In addition, there are
|
| 4657 |
+
relaxed atomic operations, which are not synchronization operations, and
|
| 4658 |
+
atomic read-modify-write operations, which have special characteristics.
|
| 4659 |
+
|
| 4660 |
+
[*Note 2*: For example, a call that acquires a mutex will perform an
|
| 4661 |
+
acquire operation on the locations comprising the mutex.
|
| 4662 |
+
Correspondingly, a call that releases the same mutex will perform a
|
| 4663 |
+
release operation on those same locations. Informally, performing a
|
| 4664 |
+
release operation on A forces prior side effects on other memory
|
| 4665 |
+
locations to become visible to other threads that later perform a
|
| 4666 |
+
consume or an acquire operation on A. “Relaxed” atomic operations are
|
| 4667 |
+
not synchronization operations even though, like synchronization
|
| 4668 |
+
operations, they cannot contribute to data races. — *end note*]
|
| 4669 |
+
|
| 4670 |
+
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 |
+
may 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
|
| 4682 |
+
subsequent operation is an atomic read-modify-write operation.
|
| 4683 |
+
|
| 4684 |
+
Certain library calls *synchronize with* other library calls performed
|
| 4685 |
+
by another thread. For example, an atomic store-release synchronizes
|
| 4686 |
+
with a load-acquire that takes its value from the store
|
| 4687 |
+
[[atomics.order]].
|
| 4688 |
+
|
| 4689 |
+
[*Note 4*: Except in the specified cases, reading a later value does
|
| 4690 |
+
not necessarily ensure visibility as described below. Such a requirement
|
| 4691 |
+
would sometimes interfere with efficient implementation. — *end note*]
|
| 4692 |
+
|
| 4693 |
+
[*Note 5*: The specifications of the synchronization operations define
|
| 4694 |
+
when one reads the value written by another. For atomic objects, the
|
| 4695 |
+
definition is clear. All operations on a given mutex occur in a single
|
| 4696 |
+
total order. Each mutex acquisition “reads the value written” by the
|
| 4697 |
+
last mutex release. — *end note*]
|
| 4698 |
+
|
| 4699 |
+
An evaluation A *carries a dependency* to an evaluation B if
|
| 4700 |
+
|
| 4701 |
+
- the value of A is used as an operand of B, unless:
|
| 4702 |
+
- B is an invocation of any specialization of `std::kill_dependency`
|
| 4703 |
+
[[atomics.order]], or
|
| 4704 |
+
- A is the left operand of a built-in logical (`&&`, see
|
| 4705 |
+
[[expr.log.and]]) or logical (`||`, see [[expr.log.or]]) operator,
|
| 4706 |
+
or
|
| 4707 |
+
- A is the left operand of a conditional (`?:`, see [[expr.cond]])
|
| 4708 |
+
operator, or
|
| 4709 |
+
- A is the left operand of the built-in comma (`,`) operator
|
| 4710 |
+
[[expr.comma]];
|
| 4711 |
+
|
| 4712 |
+
or
|
| 4713 |
+
- A writes a scalar object or bit-field M, B reads the value written by
|
| 4714 |
+
A from M, and A is sequenced before B, or
|
| 4715 |
+
- for some evaluation X, A carries a dependency to X, and X carries a
|
| 4716 |
+
dependency to B.
|
| 4717 |
+
|
| 4718 |
+
[*Note 6*: “Carries a dependency to” is a subset of “is sequenced
|
| 4719 |
+
before”, and is similarly strictly intra-thread. — *end note*]
|
| 4720 |
+
|
| 4721 |
+
An evaluation A is *dependency-ordered before* an evaluation B if
|
| 4722 |
+
|
| 4723 |
+
- A performs a release operation on an atomic object M, and, in another
|
| 4724 |
+
thread, B performs a consume operation on M and reads the value
|
| 4725 |
+
written by A, or
|
| 4726 |
+
- for some evaluation X, A is dependency-ordered before X and X carries
|
| 4727 |
+
a dependency to B.
|
| 4728 |
+
|
| 4729 |
+
[*Note 7*: The relation “is dependency-ordered before” is analogous to
|
| 4730 |
+
“synchronizes with”, but uses release/consume in place of
|
| 4731 |
+
release/acquire. — *end note*]
|
| 4732 |
+
|
| 4733 |
+
An evaluation A *inter-thread happens before* an evaluation B if
|
| 4734 |
+
|
| 4735 |
+
- A synchronizes with B, or
|
| 4736 |
+
- A is dependency-ordered before B, or
|
| 4737 |
+
- for some evaluation X
|
| 4738 |
+
- A synchronizes with X and X is sequenced before B, or
|
| 4739 |
+
- A is sequenced before X and X inter-thread happens before B, or
|
| 4740 |
+
- A inter-thread happens before X and X inter-thread happens before B.
|
| 4741 |
+
|
| 4742 |
+
[*Note 8*: The “inter-thread happens before” relation describes
|
| 4743 |
+
arbitrary concatenations of “sequenced before”, “synchronizes with” and
|
| 4744 |
+
“dependency-ordered before” relationships, with two exceptions. The
|
| 4745 |
+
first exception is that a concatenation is not permitted to end with
|
| 4746 |
+
“dependency-ordered before” followed by “sequenced before”. The reason
|
| 4747 |
+
for this limitation is that a consume operation participating in a
|
| 4748 |
+
“dependency-ordered before” relationship provides ordering only with
|
| 4749 |
+
respect to operations to which this consume operation actually carries a
|
| 4750 |
+
dependency. The reason that this limitation applies only to the end of
|
| 4751 |
+
such a concatenation is that any subsequent release operation will
|
| 4752 |
+
provide the required ordering for a prior consume operation. The second
|
| 4753 |
+
exception is that a concatenation is not permitted to consist entirely
|
| 4754 |
+
of “sequenced before”. The reasons for this limitation are (1) to permit
|
| 4755 |
+
“inter-thread happens before” to be transitively closed and (2) the
|
| 4756 |
+
“happens before” relation, defined below, provides for relationships
|
| 4757 |
+
consisting entirely of “sequenced before”. — *end note*]
|
| 4758 |
+
|
| 4759 |
+
An evaluation A *happens before* an evaluation B (or, equivalently, B
|
| 4760 |
+
*happens after* A) if:
|
| 4761 |
+
|
| 4762 |
+
- A is sequenced before B, or
|
| 4763 |
+
- A inter-thread happens before B.
|
| 4764 |
+
|
| 4765 |
+
The implementation shall ensure that no program execution demonstrates a
|
| 4766 |
+
cycle in the “happens before” relation.
|
| 4767 |
+
|
| 4768 |
+
[*Note 9*: This cycle would otherwise be possible only through the use
|
| 4769 |
+
of consume operations. — *end note*]
|
| 4770 |
+
|
| 4771 |
+
An evaluation A *simply happens before* an evaluation B if either
|
| 4772 |
+
|
| 4773 |
+
- A is sequenced before B, or
|
| 4774 |
+
- A synchronizes with B, or
|
| 4775 |
+
- A simply happens before X and X simply happens before B.
|
| 4776 |
+
|
| 4777 |
+
[*Note 10*: In the absence of consume operations, the happens before
|
| 4778 |
+
and simply happens before relations are identical. — *end note*]
|
| 4779 |
+
|
| 4780 |
+
An evaluation A *strongly happens before* an evaluation D if, either
|
| 4781 |
+
|
| 4782 |
+
- A is sequenced before D, or
|
| 4783 |
+
- A synchronizes with D, and both A and D are sequentially consistent
|
| 4784 |
+
atomic operations [[atomics.order]], or
|
| 4785 |
+
- there are evaluations B and C such that A is sequenced before B, B
|
| 4786 |
+
simply happens before C, and C is sequenced before D, or
|
| 4787 |
+
- there is an evaluation B such that A strongly happens before B, and B
|
| 4788 |
+
strongly happens before D.
|
| 4789 |
+
|
| 4790 |
+
[*Note 11*: Informally, if A strongly happens before B, then A appears
|
| 4791 |
+
to be evaluated before B in all contexts. Strongly happens before
|
| 4792 |
+
excludes consume operations. — *end note*]
|
| 4793 |
+
|
| 4794 |
+
A *visible side effect* A on a scalar object or bit-field M with respect
|
| 4795 |
+
to a value computation B of M satisfies the conditions:
|
| 4796 |
+
|
| 4797 |
+
- A happens before B and
|
| 4798 |
+
- there is no other side effect X to M such that A happens before X and
|
| 4799 |
+
X happens before B.
|
| 4800 |
+
|
| 4801 |
+
The value of a non-atomic scalar object or bit-field M, as determined by
|
| 4802 |
+
evaluation B, shall be the value stored by the visible side effect A.
|
| 4803 |
+
|
| 4804 |
+
[*Note 12*: If there is ambiguity about which side effect to a
|
| 4805 |
+
non-atomic object or bit-field is visible, then the behavior is either
|
| 4806 |
+
unspecified or undefined. — *end note*]
|
| 4807 |
+
|
| 4808 |
+
[*Note 13*: This states that operations on ordinary objects are not
|
| 4809 |
+
visibly reordered. This is not actually detectable without data races,
|
| 4810 |
+
but it is necessary to ensure that data races, as defined below, and
|
| 4811 |
+
with suitable restrictions on the use of atomics, correspond to data
|
| 4812 |
+
races in a simple interleaved (sequentially consistent)
|
| 4813 |
+
execution. — *end note*]
|
| 4814 |
+
|
| 4815 |
+
The value of an atomic object M, as determined by evaluation B, shall be
|
| 4816 |
+
the value stored by some side effect A that modifies M, where B does not
|
| 4817 |
+
happen before A.
|
| 4818 |
+
|
| 4819 |
+
[*Note 14*: The set of such side effects is also restricted by the rest
|
| 4820 |
+
of the rules described here, and in particular, by the coherence
|
| 4821 |
+
requirements below. — *end note*]
|
| 4822 |
+
|
| 4823 |
+
If an operation A that modifies an atomic object M happens before an
|
| 4824 |
+
operation B that modifies M, then A shall be earlier than B in the
|
| 4825 |
+
modification order of M.
|
| 4826 |
+
|
| 4827 |
+
[*Note 15*: This requirement is known as write-write
|
| 4828 |
+
coherence. — *end note*]
|
| 4829 |
+
|
| 4830 |
+
If a value computation A of an atomic object M happens before a value
|
| 4831 |
+
computation B of M, and A takes its value from a side effect X on M,
|
| 4832 |
+
then the value computed by B shall either be the value stored by X or
|
| 4833 |
+
the value stored by a side effect Y on M, where Y follows X in the
|
| 4834 |
+
modification order of M.
|
| 4835 |
+
|
| 4836 |
+
[*Note 16*: This requirement is known as read-read
|
| 4837 |
+
coherence. — *end note*]
|
| 4838 |
+
|
| 4839 |
+
If a value computation A of an atomic object M happens before an
|
| 4840 |
+
operation B that modifies M, then A shall take its value from a side
|
| 4841 |
+
effect X on M, where X precedes B in the modification order of M.
|
| 4842 |
+
|
| 4843 |
+
[*Note 17*: This requirement is known as read-write
|
| 4844 |
+
coherence. — *end note*]
|
| 4845 |
+
|
| 4846 |
+
If a side effect X on an atomic object M happens before a value
|
| 4847 |
+
computation B of M, then the evaluation B shall take its value from X or
|
| 4848 |
+
from a side effect Y that follows X in the modification order of M.
|
| 4849 |
+
|
| 4850 |
+
[*Note 18*: This requirement is known as write-read
|
| 4851 |
+
coherence. — *end note*]
|
| 4852 |
+
|
| 4853 |
+
[*Note 19*: The four preceding coherence requirements effectively
|
| 4854 |
+
disallow compiler reordering of atomic operations to a single object,
|
| 4855 |
+
even if both operations are relaxed loads. This effectively makes the
|
| 4856 |
+
cache coherence guarantee provided by most hardware available to C++
|
| 4857 |
+
atomic operations. — *end note*]
|
| 4858 |
+
|
| 4859 |
+
[*Note 20*: The value observed by a load of an atomic depends on the
|
| 4860 |
+
“happens before” relation, which depends on the values observed by loads
|
| 4861 |
+
of atomics. The intended reading is that there must exist an association
|
| 4862 |
+
of atomic loads with modifications they observe that, together with
|
| 4863 |
+
suitably chosen modification orders and the “happens before” relation
|
| 4864 |
+
derived as described above, satisfy the resulting constraints as imposed
|
| 4865 |
+
here. — *end note*]
|
| 4866 |
+
|
| 4867 |
+
Two actions are *potentially concurrent* if
|
| 4868 |
+
|
| 4869 |
+
- they are performed by different threads, or
|
| 4870 |
+
- they are unsequenced, at least one is performed by a signal handler,
|
| 4871 |
+
and they are not both performed by the same signal handler invocation.
|
| 4872 |
+
|
| 4873 |
+
The execution of a program contains a *data race* if it contains two
|
| 4874 |
+
potentially concurrent conflicting actions, at least one of which is not
|
| 4875 |
+
atomic, and neither happens before the other, except for the special
|
| 4876 |
+
case for signal handlers described below. Any such data race results in
|
| 4877 |
+
undefined behavior.
|
| 4878 |
+
|
| 4879 |
+
[*Note 21*: It can be shown that programs that correctly use mutexes
|
| 4880 |
+
and `memory_order::seq_cst` operations to prevent all data races and use
|
| 4881 |
+
no other synchronization operations behave as if the operations executed
|
| 4882 |
+
by their constituent threads were simply interleaved, with each value
|
| 4883 |
+
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 must perform an
|
| 4890 |
+
undefined operation. — *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
|
| 4896 |
+
divided into two groups A and B, such that no evaluations in B happen
|
| 4897 |
+
before evaluations in A, and the evaluations of such
|
| 4898 |
+
`volatile std::sig_atomic_t` objects take values as though all
|
| 4899 |
+
evaluations in A happened before the execution of the signal handler and
|
| 4900 |
+
the execution of the signal handler happened before all evaluations in
|
| 4901 |
+
B.
|
| 4902 |
+
|
| 4903 |
+
[*Note 22*: Compiler transformations that introduce assignments to a
|
| 4904 |
+
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 may alias is
|
| 4911 |
+
also generally precluded, since this may violate the coherence
|
| 4912 |
+
rules. — *end note*]
|
| 4913 |
+
|
| 4914 |
+
[*Note 23*: Transformations that introduce a speculative read of a
|
| 4915 |
+
potentially shared memory location may not preserve the semantics of the
|
| 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*]
|
| 4922 |
+
|
| 4923 |
+
#### Forward progress <a id="intro.progress">[[intro.progress]]</a>
|
| 4924 |
+
|
| 4925 |
+
The implementation may assume that any thread will eventually do one of
|
| 4926 |
+
the following:
|
| 4927 |
+
|
| 4928 |
+
- terminate,
|
| 4929 |
+
- make a call to a library I/O function,
|
| 4930 |
+
- perform an access through a volatile glvalue, or
|
| 4931 |
+
- perform a synchronization operation or an atomic operation.
|
| 4932 |
+
|
| 4933 |
+
[*Note 1*: This is intended to allow compiler transformations such as
|
| 4934 |
+
removal of empty loops, even when termination cannot be
|
| 4935 |
+
proven. — *end note*]
|
| 4936 |
+
|
| 4937 |
+
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 may 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 may prevent
|
| 4951 |
+
forward progress, e.g., by repeatedly stealing a cache line for
|
| 4952 |
+
unrelated purposes between load-locked and store-conditional
|
| 4953 |
+
instructions. Implementations should ensure that such effects cannot
|
| 4954 |
+
indefinitely delay progress under expected operating conditions, and
|
| 4955 |
+
that such anomalies can therefore safely be ignored by programmers.
|
| 4956 |
+
Outside this document, this property is sometimes termed
|
| 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,
|
| 4963 |
+
- performing an access through a volatile glvalue, or
|
| 4964 |
+
- completion of a call to a library I/O function, a synchronization
|
| 4965 |
+
operation, or an atomic operation.
|
| 4966 |
+
|
| 4967 |
+
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 might consist 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
|
| 4979 |
+
thread of execution can execute forever without an execution step
|
| 4980 |
+
occurring. — *end note*]
|
| 4981 |
+
|
| 4982 |
+
A thread of execution *makes progress* when an execution step occurs or
|
| 4983 |
+
a lock-free execution does not complete because there are other
|
| 4984 |
+
concurrent threads that are not blocked in a standard library function
|
| 4985 |
+
(see above).
|
| 4986 |
+
|
| 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 executions (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.
|
| 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 7*: This does not specify a requirement for when to start this
|
| 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
|
| 5017 |
+
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 8*: Threads of execution providing weakly parallel forward
|
| 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 9*: For example, some kinds of synchronization between threads
|
| 5035 |
+
of execution may only make progress if the respective threads of
|
| 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 10*: It is unspecified which thread or threads of execution in S
|
| 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 11*: A thread of execution B thus can temporarily provide an
|
| 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 may also temporarily
|
| 5061 |
+
provide a stronger forward progress guarantee to C. — *end note*]
|
| 5062 |
+
|
| 5063 |
+
[*Note 12*: If all threads of execution in S finish executing (e.g.,
|
| 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 13*: This does not remove any constraints regarding blocking
|
| 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 |
+
|
| 5075 |
+
An implementation should ensure that the last value (in modification
|
| 5076 |
+
order) assigned by an atomic or synchronization operation will become
|
| 5077 |
+
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 a global function called `main` attached to the
|
| 5084 |
+
global module. Executing a program starts a main thread of execution (
|
| 5085 |
+
[[intro.multithread]], [[thread.threads]]) in which the `main` function
|
| 5086 |
+
is invoked, and in which variables of static storage duration might be
|
| 5087 |
+
initialized [[basic.start.static]] and destroyed [[basic.start.term]].
|
| 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 of namespace scope with static storage duration; termination
|
| 5094 |
+
contains the execution of destructors for objects with static storage
|
| 5095 |
+
duration. — *end note*]
|
| 5096 |
+
|
| 5097 |
+
An implementation shall not predefine the `main` function. This function
|
| 5098 |
+
shall not be overloaded. Its type shall have C++ language linkage and it
|
| 5099 |
+
shall have a declared return type of type `int`, but otherwise its type
|
| 5100 |
+
is *implementation-defined*. An implementation shall allow both
|
| 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
|
| 5106 |
+
exposition, the first function parameter is called `argc` and the second
|
| 5107 |
+
function parameter is called `argv`, where `argc` shall be the number of
|
| 5108 |
+
arguments passed to the program from the environment in which the
|
| 5109 |
+
program is run. If `argc` is nonzero these arguments shall be supplied
|
| 5110 |
+
in `argv[0]` through `argv[argc-1]` as pointers to the initial
|
| 5111 |
+
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 |
+
[*Note 2*: It is recommended that any further (optional) parameters be
|
| 5118 |
+
added after `argv`. — *end note*]
|
| 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 `constexpr` is ill-formed. The function `main` shall not be
|
| 5124 |
+
a coroutine [[dcl.fct.def.coroutine]]. The `main` function shall not be
|
| 5125 |
+
declared with a *linkage-specification* [[dcl.link]]. A program that
|
| 5126 |
+
declares a variable `main` at global scope, or that declares a function
|
| 5127 |
+
`main` at global scope attached to a named module, or that declares the
|
| 5128 |
+
name `main` with C language linkage (in any namespace) is ill-formed.
|
| 5129 |
+
The name `main` is not otherwise reserved.
|
| 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 called to end a program during the destruction of an
|
| 5138 |
+
object with static or thread storage duration, the program has undefined
|
| 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
|
| 5145 |
+
effect is equivalent to a `return` with operand `0` (see also
|
| 5146 |
+
[[except.handle]]).
|
| 5147 |
+
|
| 5148 |
+
#### Static initialization <a id="basic.start.static">[[basic.start.static]]</a>
|
| 5149 |
+
|
| 5150 |
+
Variables with static storage duration are initialized as a consequence
|
| 5151 |
+
of program initiation. Variables with thread storage duration are
|
| 5152 |
+
initialized as a consequence of thread execution. Within each of these
|
| 5153 |
+
phases of initiation, initialization occurs as follows.
|
| 5154 |
+
|
| 5155 |
+
*Constant initialization* is performed if a variable or temporary object
|
| 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 *dynamic
|
| 5162 |
+
initialization*. All static initialization strongly happens before
|
| 5163 |
+
[[intro.races]] any dynamic initialization.
|
| 5164 |
+
|
| 5165 |
+
[*Note 1*: The dynamic initialization of non-local variables is
|
| 5166 |
+
described in [[basic.start.dynamic]]; that of local static 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
|
| 5172 |
+
statically, provided that
|
| 5173 |
+
|
| 5174 |
+
- the dynamic version of the initialization does not change the value of
|
| 5175 |
+
any other object of static or thread storage duration prior to its
|
| 5176 |
+
initialization, and
|
| 5177 |
+
- the static version of the initialization produces the same value in
|
| 5178 |
+
the initialized variable as would be produced by the dynamic
|
| 5179 |
+
initialization if all variables not required to be initialized
|
| 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` of namespace scope potentially requiring dynamic
|
| 5186 |
+
initialization and defined later in the same translation unit, it is
|
| 5187 |
+
unspecified whether the value of `obj2` used will be the value of the
|
| 5188 |
+
fully initialized `obj2` (because `obj2` was statically initialized) or
|
| 5189 |
+
will be the value of `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 |
+
// may be 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(); // may be initialized statically or dynamically to 1.0
|
| 5199 |
+
```
|
| 5200 |
+
|
| 5201 |
+
— *end note*]
|
| 5202 |
+
|
| 5203 |
+
#### Dynamic initialization of non-local variables <a id="basic.start.dynamic">[[basic.start.dynamic]]</a>
|
| 5204 |
+
|
| 5205 |
+
Dynamic initialization of a non-local 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*: An explicitly specialized non-inline static data member or
|
| 5212 |
+
variable template specialization has ordered
|
| 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-local variables `V` and `W` with static
|
| 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
|
| 5229 |
+
initialization, and for every definition `E` of `W` there exists a
|
| 5230 |
+
definition `D` of `V` such that `D` is appearance-ordered before `E`,
|
| 5231 |
+
then
|
| 5232 |
+
- if the program does not start a thread [[intro.multithread]] other
|
| 5233 |
+
than the main thread [[basic.start.main]] or `V` and `W` have
|
| 5234 |
+
ordered initialization and they are defined in the same translation
|
| 5235 |
+
unit, the initialization of `V` is sequenced before the
|
| 5236 |
+
initialization of `W`;
|
| 5237 |
+
- otherwise, the initialization of `V` strongly happens before the
|
| 5238 |
+
initialization of `W`.
|
| 5239 |
+
- Otherwise, if the program starts a thread other than the main thread
|
| 5240 |
+
before either `V` or `W` is initialized, it is unspecified in which
|
| 5241 |
+
threads the initializations of `V` and `W` occur; the initializations
|
| 5242 |
+
are unsequenced if they occur in the same thread.
|
| 5243 |
+
- Otherwise, the initializations of `V` and `W` are indeterminately
|
| 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 [[basic.def.odr]] not
|
| 5250 |
+
caused directly or indirectly by the initialization of a non-local
|
| 5251 |
+
static or thread storage duration variable.
|
| 5252 |
+
|
| 5253 |
+
It is *implementation-defined* whether the dynamic initialization of a
|
| 5254 |
+
non-local non-inline variable with static storage duration is sequenced
|
| 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. [^29] It is
|
| 5259 |
+
*implementation-defined* in which threads and at which points in the
|
| 5260 |
+
program such deferred dynamic initialization occurs.
|
| 5261 |
+
|
| 5262 |
+
[*Note 3*: Such points should be chosen in a way that allows the
|
| 5263 |
+
programmer to avoid deadlocks. — *end note*]
|
| 5264 |
|
| 5265 |
[*Example 1*:
|
| 5266 |
|
| 5267 |
``` cpp
|
| 5268 |
+
// - File 1 -
|
| 5269 |
+
#include "a.h"
|
| 5270 |
+
#include "b.h"
|
| 5271 |
+
B b;
|
| 5272 |
+
A::A(){
|
| 5273 |
+
b.Use();
|
| 5274 |
+
}
|
| 5275 |
+
|
| 5276 |
+
// - File 2 -
|
| 5277 |
+
#include "a.h"
|
| 5278 |
+
A a;
|
| 5279 |
+
|
| 5280 |
+
// - File 3 -
|
| 5281 |
+
#include "a.h"
|
| 5282 |
+
#include "b.h"
|
| 5283 |
+
extern A a;
|
| 5284 |
+
extern B b;
|
| 5285 |
+
|
| 5286 |
+
int main() {
|
| 5287 |
+
a.Use();
|
| 5288 |
+
b.Use();
|
| 5289 |
+
}
|
| 5290 |
```
|
| 5291 |
|
| 5292 |
+
It is *implementation-defined* whether either `a` or `b` is initialized
|
| 5293 |
+
before `main` is entered or whether the initializations are delayed
|
| 5294 |
+
until `a` is first odr-used in `main`. In particular, if `a` is
|
| 5295 |
+
initialized before `main` is entered, it is not guaranteed that `b` will
|
| 5296 |
+
be initialized before it is odr-used by the initialization of `a`, that
|
| 5297 |
+
is, before `A::A` is called. If, however, `a` is initialized at some
|
| 5298 |
+
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-local 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-local 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-local 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 |
+
|
| 5326 |
+
Constructed objects [[dcl.init]] with static storage duration are
|
| 5327 |
+
destroyed and functions registered with `std::atexit` are called as part
|
| 5328 |
+
of a call to `std::exit` [[support.start.term]]. The call to `std::exit`
|
| 5329 |
+
is sequenced before the destructions and the registered functions.
|
| 5330 |
+
|
| 5331 |
+
[*Note 1*: Returning from `main` invokes `std::exit`
|
| 5332 |
+
[[basic.start.main]]. — *end note*]
|
| 5333 |
+
|
| 5334 |
+
Constructed objects with thread storage duration within a given thread
|
| 5335 |
+
are destroyed as a result of returning from the initial function of that
|
| 5336 |
+
thread and as a result of that thread calling `std::exit`. The
|
| 5337 |
+
destruction of all constructed objects with thread storage duration
|
| 5338 |
+
within that thread strongly happens before destroying any object with
|
| 5339 |
+
static storage duration.
|
| 5340 |
+
|
| 5341 |
+
If the completion of the constructor or dynamic initialization of an
|
| 5342 |
+
object with static storage duration strongly happens before that of
|
| 5343 |
+
another, the completion of the destructor of the second is sequenced
|
| 5344 |
+
before the initiation of the destructor of the first. If the completion
|
| 5345 |
+
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-scope object with static
|
| 5352 |
+
storage duration initialized during the construction of the subobjects
|
| 5353 |
+
is destroyed. If the destruction of an object with static or thread
|
| 5354 |
+
storage duration exits via an exception, the function `std::terminate`
|
| 5355 |
+
is called [[except.terminate]].
|
| 5356 |
+
|
| 5357 |
+
If a function contains a block-scope object 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-scope object. Likewise, the
|
| 5362 |
+
behavior is undefined if the block-scope object is used indirectly
|
| 5363 |
+
(i.e., through a pointer) after its destruction.
|
| 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
|
| 5369 |
+
object. If a call to `std::atexit` strongly happens before the
|
| 5370 |
+
completion of the initialization of an object with static storage
|
| 5371 |
+
duration, the call to the destructor for the object is sequenced before
|
| 5372 |
+
the call to the function passed to `std::atexit`. If a call to
|
| 5373 |
+
`std::atexit` strongly happens before another call to `std::atexit`, the
|
| 5374 |
+
call to the function passed to the second `std::atexit` call is
|
| 5375 |
+
sequenced before the call to the function passed to the first
|
| 5376 |
+
`std::atexit` call.
|
| 5377 |
+
|
| 5378 |
+
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 2*: If there is a use of an object with static storage duration
|
| 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*]
|
| 5390 |
+
|
| 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]: utilities.md#allocator.members
|
| 5397 |
+
[allocator.traits.members]: utilities.md#allocator.traits.members
|
| 5398 |
+
[atomics]: atomics.md#atomics
|
| 5399 |
+
[atomics.flag]: atomics.md#atomics.flag
|
| 5400 |
+
[atomics.lockfree]: atomics.md#atomics.lockfree
|
| 5401 |
+
[atomics.order]: atomics.md#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.hiding]: #basic.scope.hiding
|
| 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
|
|
|
|
| 5446 |
[basic.stc.dynamic.safety]: #basic.stc.dynamic.safety
|
| 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 |
+
[bit.cast]: numerics.md#bit.cast
|
| 5454 |
+
[c.malloc]: utilities.md#c.malloc
|
| 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
|
| 5463 |
+
[class.copy.ctor]: class.md#class.copy.ctor
|
| 5464 |
+
[class.copy.elision]: class.md#class.copy.elision
|
| 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]: class.md#class.member.lookup
|
| 5473 |
[class.mfct]: class.md#class.mfct
|
| 5474 |
[class.mfct.non-static]: class.md#class.mfct.non-static
|
| 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
|
| 5491 |
+
[conv.lval]: expr.md#conv.lval
|
| 5492 |
+
[conv.mem]: expr.md#conv.mem
|
| 5493 |
+
[conv.prom]: expr.md#conv.prom
|
| 5494 |
+
[conv.ptr]: expr.md#conv.ptr
|
| 5495 |
+
[conv.rank]: #conv.rank
|
| 5496 |
+
[conv.rval]: expr.md#conv.rval
|
| 5497 |
[cpp.predefined]: cpp.md#cpp.predefined
|
| 5498 |
+
[cstddef.syn]: support.md#cstddef.syn
|
| 5499 |
+
[cstring.syn]: strings.md#cstring.syn
|
| 5500 |
[dcl.align]: dcl.md#dcl.align
|
| 5501 |
[dcl.array]: dcl.md#dcl.array
|
| 5502 |
+
[dcl.attr]: dcl.md#dcl.attr
|
| 5503 |
+
[dcl.attr.nouniqueaddr]: dcl.md#dcl.attr.nouniqueaddr
|
| 5504 |
+
[dcl.constexpr]: dcl.md#dcl.constexpr
|
| 5505 |
[dcl.dcl]: dcl.md#dcl.dcl
|
| 5506 |
[dcl.decl]: dcl.md#dcl.decl
|
| 5507 |
[dcl.enum]: dcl.md#dcl.enum
|
| 5508 |
[dcl.fct]: dcl.md#dcl.fct
|
| 5509 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 5510 |
+
[dcl.fct.def.coroutine]: dcl.md#dcl.fct.def.coroutine
|
| 5511 |
+
[dcl.fct.def.general]: dcl.md#dcl.fct.def.general
|
| 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
|
| 5523 |
[dcl.ptr]: dcl.md#dcl.ptr
|
| 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
|
| 5558 |
[expr.dynamic.cast]: expr.md#expr.dynamic.cast
|
| 5559 |
[expr.eq]: expr.md#expr.eq
|
| 5560 |
+
[expr.log.and]: expr.md#expr.log.and
|
| 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
|
| 5573 |
[expr.ref]: expr.md#expr.ref
|
| 5574 |
[expr.reinterpret.cast]: expr.md#expr.reinterpret.cast
|
| 5575 |
[expr.rel]: expr.md#expr.rel
|
| 5576 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 5577 |
[expr.static.cast]: expr.md#expr.static.cast
|
| 5578 |
[expr.sub]: expr.md#expr.sub
|
| 5579 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 5580 |
[expr.typeid]: expr.md#expr.typeid
|
| 5581 |
[expr.unary.op]: expr.md#expr.unary.op
|
| 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]: utilities.md#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.label]: stmt.md#stmt.label
|
| 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]: This refers to unqualified names that occur, for instance, in a
|
|
|
|
|
|
|
|
|
|
| 5664 |
type or default argument in the *parameter-declaration-clause* or
|
| 5665 |
used in the function body.
|
| 5666 |
|
| 5667 |
+
[^4]: This refers to unqualified names following the class name; such a
|
| 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 |
+
[^9]: The number of bits in a byte is reported by the macro `CHAR_BIT`
|
| 5688 |
+
in the header `<climits>`.
|
| 5689 |
|
| 5690 |
+
[^10]: Under the “as-if” rule an implementation is allowed to store two
|
| 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 |
+
[^11]: For example, before the construction of a global object that is
|
| 5695 |
+
initialized via a user-provided constructor [[class.cdtor]].
|
| 5696 |
+
|
| 5697 |
+
[^12]: That is, an object for which a destructor will be called
|
| 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 |
+
[^13]: Some implementations might define that copying an invalid pointer
|
| 5704 |
value causes a system-generated runtime fault.
|
| 5705 |
|
| 5706 |
+
[^14]: The intent is to have `operator new()` implementable by calling
|
| 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 |
+
[^15]: The global `operator delete(void*, std::size_t)` precludes use of
|
| 5712 |
an allocation function `void operator new(std::size_t, std::size_t)`
|
| 5713 |
+
as a placement allocation function ([[diff.cpp11.basic]]).
|
| 5714 |
|
| 5715 |
+
[^16]: This subclause does not impose restrictions on indirection
|
| 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 |
+
[^18]: By using, for example, the library functions [[headers]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5726 |
`std::memcpy` or `std::memmove`.
|
| 5727 |
|
| 5728 |
+
[^19]: By using, for example, the library functions [[headers]]
|
| 5729 |
`std::memcpy` or `std::memmove`.
|
| 5730 |
|
| 5731 |
+
[^20]: The intent is that the memory model of C++ is compatible with
|
| 5732 |
+
that of ISO/IEC 9899 Programming Language C.
|
| 5733 |
|
| 5734 |
[^21]: The size and layout of an instance of an incompletely-defined
|
| 5735 |
object type is unknown.
|
| 5736 |
|
| 5737 |
+
[^22]: This is also known as two’s complement representation.
|
|
|
|
| 5738 |
|
| 5739 |
+
[^23]: Static class members are objects or functions, and pointers to
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5740 |
them are ordinary pointers to objects or functions.
|
| 5741 |
|
| 5742 |
+
[^24]: For an object that is not within its lifetime, this is the first
|
| 5743 |
byte in memory that it will occupy or used to occupy.
|
| 5744 |
|
| 5745 |
+
[^25]: The same representation and alignment requirements are meant to
|
| 5746 |
imply interchangeability as arguments to functions, return values
|
| 5747 |
from functions, and non-static data members of unions.
|
| 5748 |
|
| 5749 |
+
[^26]: As specified in [[class.temporary]], after a full-expression is
|
| 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 |
+
[^27]: In other words, function executions do not interleave with each
|
| 5755 |
+
other.
|
| 5756 |
+
|
| 5757 |
+
[^28]: An object with automatic or thread storage duration [[basic.stc]]
|
| 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 |
+
[^29]: A non-local variable with static storage duration having
|
| 5763 |
+
initialization with side effects is initialized in this case, even
|
| 5764 |
+
if it is not itself odr-used ([[basic.def.odr]],
|
| 5765 |
+
[[basic.stc.static]]).
|