- tmp/tmp3g508ekq/{from.md → to.md} +1361 -838
tmp/tmp3g508ekq/{from.md → to.md}
RENAMED
|
@@ -1,27 +1,28 @@
|
|
| 1 |
# Basic concepts <a id="basic">[[basic]]</a>
|
| 2 |
|
| 3 |
-
This Clause presents the basic concepts of the C++language.
|
| 4 |
-
the difference between an
|
| 5 |
-
the value categories for expressions. It introduces the
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
This Clause does not cover concepts that affect only a
|
| 13 |
-
the language. Such concepts are discussed in the relevant
|
|
|
|
| 14 |
|
| 15 |
An *entity* is a value, object, reference, function, enumerator, type,
|
| 16 |
-
class member, template, template specialization, namespace,
|
| 17 |
-
|
| 18 |
|
| 19 |
A *name* is a use of an *identifier* ([[lex.name]]),
|
| 20 |
*operator-function-id* ([[over.oper]]), *literal-operator-id* (
|
| 21 |
[[over.literal]]), *conversion-function-id* ([[class.conv.fct]]), or
|
| 22 |
-
*template-id* ([[temp.names]]) that denotes an entity or
|
| 23 |
[[stmt.goto]], [[stmt.label]]).
|
| 24 |
|
| 25 |
Every name that denotes an entity is introduced by a *declaration*.
|
| 26 |
Every name that denotes a label is introduced either by a `goto`
|
| 27 |
statement ([[stmt.goto]]) or a *labeled-statement* ([[stmt.label]]).
|
|
@@ -58,30 +59,47 @@ declarations. If so, the declaration specifies the interpretation and
|
|
| 58 |
attributes of these names. A declaration may also have effects
|
| 59 |
including:
|
| 60 |
|
| 61 |
- a static assertion (Clause [[dcl.dcl]]),
|
| 62 |
- controlling template instantiation ([[temp.explicit]]),
|
|
|
|
|
|
|
| 63 |
- use of attributes (Clause [[dcl.dcl]]), and
|
| 64 |
- nothing (in the case of an *empty-declaration*).
|
| 65 |
|
| 66 |
-
A declaration is a *definition* unless
|
| 67 |
-
specifying the function’s body ([[dcl.fct.def]]), it contains the
|
| 68 |
-
`extern` specifier ([[dcl.stc]]) or a *linkage-specification*[^1] (
|
| 69 |
-
[[dcl.link]]) and neither an *initializer* nor a *function-body*, it
|
| 70 |
-
declares a static data member in a class definition ([[class.mem]],
|
| 71 |
-
[[class.static]]), it is a class name declaration ([[class.name]]), it
|
| 72 |
-
is an *opaque-enum-declaration* ([[dcl.enum]]), it is a
|
| 73 |
-
*template-parameter* ([[temp.param]]), it is a
|
| 74 |
-
*parameter-declaration* ([[dcl.fct]]) in a function declarator that is
|
| 75 |
-
not the *declarator* of a *function-definition*, or it is a `typedef`
|
| 76 |
-
declaration ([[dcl.typedef]]), an *alias-declaration* (
|
| 77 |
-
[[dcl.typedef]]), a *using-declaration* ([[namespace.udecl]]), a
|
| 78 |
-
*static_assert-declaration* (Clause [[dcl.dcl]]), an
|
| 79 |
-
*attribute-declaration* (Clause [[dcl.dcl]]), an *empty-declaration*
|
| 80 |
-
(Clause [[dcl.dcl]]), or a *using-directive* ([[namespace.udir]]).
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
``` cpp
|
| 85 |
int a; // defines a
|
| 86 |
extern const int c = 1; // defines c
|
| 87 |
int f(int x) { return x+a; } // defines f and defines x
|
|
@@ -108,15 +126,21 @@ struct S; // declares S
|
|
| 108 |
typedef int Int; // declares Int
|
| 109 |
extern X anotherX; // declares anotherX
|
| 110 |
using N::d; // declares d
|
| 111 |
```
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
``` cpp
|
| 120 |
#include <string>
|
| 121 |
|
| 122 |
struct C {
|
|
@@ -145,27 +169,31 @@ struct C {
|
|
| 145 |
// { s = std::move(x.s); return *this; }
|
| 146 |
~C() { }
|
| 147 |
};
|
| 148 |
```
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
| 152 |
|
| 153 |
A program is ill-formed if the definition of any object gives the object
|
| 154 |
an incomplete type ([[basic.types]]).
|
| 155 |
|
| 156 |
-
## One
|
| 157 |
|
| 158 |
No translation unit shall contain more than one definition of any
|
| 159 |
variable, function, class type, enumeration type, or template.
|
| 160 |
|
| 161 |
An expression is *potentially evaluated* unless it is an unevaluated
|
| 162 |
operand (Clause [[expr]]) or a subexpression thereof. The set of
|
| 163 |
*potential results* of an expression `e` is defined as follows:
|
| 164 |
|
| 165 |
-
- If `e` is an *id-expression* ([[expr.prim.
|
| 166 |
-
|
|
|
|
|
|
|
| 167 |
- If `e` is a class member access expression ([[expr.ref]]), the set
|
| 168 |
contains the potential results of the object expression.
|
| 169 |
- If `e` is a pointer-to-member expression ([[expr.mptr.oper]]) whose
|
| 170 |
second operand is a constant expression, the set contains the
|
| 171 |
potential results of the object expression.
|
|
@@ -176,23 +204,32 @@ operand (Clause [[expr]]) or a subexpression thereof. The set of
|
|
| 176 |
operands.
|
| 177 |
- If `e` is a comma expression ([[expr.comma]]), the set contains the
|
| 178 |
potential results of the right operand.
|
| 179 |
- Otherwise, the set is empty.
|
| 180 |
|
|
|
|
|
|
|
| 181 |
This set is a (possibly-empty) set of *id-expression*s, each of which is
|
| 182 |
-
either `e` or a subexpression of `e`.
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
``` cpp
|
| 187 |
struct S { static const int x = 0; };
|
| 188 |
const int &f(const int &r);
|
| 189 |
int n = b ? (1, S::x) // S::x is not odr-used here
|
| 190 |
-
: f(S::x); // S::x is odr-used here, so
|
| 191 |
-
// a definition is required
|
| 192 |
```
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
A variable `x` whose name appears as a potentially-evaluated expression
|
| 195 |
`ex` is *odr-used* by `ex` unless applying the lvalue-to-rvalue
|
| 196 |
conversion ([[conv.lval]]) to `x` yields a constant expression (
|
| 197 |
[[expr.const]]) that does not invoke any non-trivial functions and, if
|
| 198 |
`x` is an object, `ex` is an element of the set of potential results of
|
|
@@ -204,70 +241,82 @@ implicit transformation in the body of a non-static member function (
|
|
| 204 |
[[class.mfct.non-static]])). A virtual member function is odr-used if it
|
| 205 |
is not pure. A function whose name appears as a potentially-evaluated
|
| 206 |
expression is odr-used if it is the unique lookup result or the selected
|
| 207 |
member of a set of overloaded functions ([[basic.lookup]],
|
| 208 |
[[over.match]], [[over.over]]), unless it is a pure virtual function and
|
| 209 |
-
its name is not explicitly qualified
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
[[
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
[[class.
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
Every program shall contain exactly one definition of every non-inline
|
| 235 |
-
function or variable that is odr-used in that program
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
[[class.
|
| 240 |
-
|
|
|
|
|
|
|
| 241 |
|
| 242 |
Exactly one definition of a class is required in a translation unit if
|
| 243 |
the class is used in a way that requires the class type to be complete.
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
| 245 |
never defines `X`:
|
| 246 |
|
| 247 |
``` cpp
|
| 248 |
struct X; // declare X as a struct type
|
| 249 |
struct X* x1; // use X in pointer formation
|
| 250 |
X* x2; // use X in pointer formation
|
| 251 |
```
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
The rules for declarations and expressions describe in which contexts
|
| 254 |
complete class types are required. A class type `T` must be complete if:
|
| 255 |
|
| 256 |
- an object of type `T` is defined ([[basic.def]]), or
|
| 257 |
- a non-static class data member of type `T` is declared (
|
| 258 |
[[class.mem]]), or
|
| 259 |
-
- `T` is used as the
|
| 260 |
*new-expression* ([[expr.new]]), or
|
| 261 |
- an lvalue-to-rvalue conversion is applied to a glvalue referring to an
|
| 262 |
object of type `T` ([[conv.lval]]), or
|
| 263 |
- an expression is converted (either implicitly or explicitly) to type
|
| 264 |
`T` (Clause [[conv]], [[expr.type.conv]], [[expr.dynamic.cast]],
|
| 265 |
[[expr.static.cast]], [[expr.cast]]), or
|
| 266 |
- an expression that is not a null pointer constant, and has type other
|
| 267 |
-
than
|
| 268 |
-
|
| 269 |
`dynamic_cast` ([[expr.dynamic.cast]]) or a `static_cast` (
|
| 270 |
[[expr.static.cast]]), or
|
| 271 |
- a class member access operator is applied to an expression of type
|
| 272 |
`T` ([[expr.ref]]), or
|
| 273 |
- the `typeid` operator ([[expr.typeid]]) or the `sizeof` operator (
|
|
@@ -280,15 +329,18 @@ complete class types are required. A class type `T` must be complete if:
|
|
| 280 |
- the type `T` is the subject of an `alignof` expression (
|
| 281 |
[[expr.alignof]]), or
|
| 282 |
- an *exception-declaration* has type `T`, reference to `T`, or pointer
|
| 283 |
to `T` ([[except.handle]]).
|
| 284 |
|
|
|
|
|
|
|
| 285 |
There can be more than one definition of a class type (Clause
|
| 286 |
[[class]]), enumeration type ([[dcl.enum]]), inline function with
|
| 287 |
-
external linkage ([[dcl.
|
| 288 |
-
|
| 289 |
-
|
|
|
|
| 290 |
[[temp.mem.func]]), or template specialization for which some template
|
| 291 |
parameters are not specified ([[temp.spec]], [[temp.class.spec]]) in a
|
| 292 |
program provided that each definition appears in a different translation
|
| 293 |
unit, and provided the definitions satisfy the following requirements.
|
| 294 |
Given such an entity named `D` defined in more than one translation
|
|
@@ -298,54 +350,69 @@ unit, then
|
|
| 298 |
and
|
| 299 |
- in each definition of `D`, corresponding names, looked up according
|
| 300 |
to [[basic.lookup]], shall refer to an entity defined within the
|
| 301 |
definition of `D`, or shall refer to the same entity, after overload
|
| 302 |
resolution ([[over.match]]) and after matching of partial template
|
| 303 |
-
specialization ([[temp.over]]), except that a name can refer to
|
| 304 |
-
non-volatile `const` object with internal or no linkage if the
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
- in each definition of `D`, corresponding entities shall have the same
|
| 310 |
language linkage; and
|
| 311 |
- in each definition of `D`, the overloaded operators referred to, the
|
| 312 |
implicit calls to conversion functions, constructors, operator new
|
| 313 |
functions and operator delete functions, shall refer to the same
|
| 314 |
function, or to a function defined within the definition of `D`; and
|
| 315 |
- in each definition of `D`, a default argument used by an (implicit or
|
| 316 |
explicit) function call is treated as if its token sequence were
|
| 317 |
present in the definition of `D`; that is, the default argument is
|
| 318 |
-
subject to the
|
| 319 |
-
argument has
|
| 320 |
-
applies recursively).[^3]
|
| 321 |
- if `D` is a class with an implicitly-declared constructor (
|
| 322 |
[[class.ctor]]), it is as if the constructor was implicitly defined in
|
| 323 |
every translation unit where it is odr-used, and the implicit
|
| 324 |
definition in every translation unit shall call the same constructor
|
| 325 |
-
for a
|
|
|
|
| 326 |
``` cpp
|
| 327 |
// translation unit 1:
|
| 328 |
struct X {
|
| 329 |
-
X(int);
|
| 330 |
X(int, int);
|
|
|
|
| 331 |
};
|
| 332 |
-
X::X(int = 0) { }
|
| 333 |
-
class D
|
| 334 |
-
|
|
|
|
|
|
|
| 335 |
|
| 336 |
// translation unit 2:
|
| 337 |
struct X {
|
| 338 |
-
X(int);
|
| 339 |
X(int, int);
|
|
|
|
| 340 |
};
|
| 341 |
-
X::X(int = 0, int = 0) { }
|
| 342 |
-
class D
|
| 343 |
-
|
| 344 |
-
|
|
|
|
|
|
|
| 345 |
```
|
| 346 |
|
|
|
|
|
|
|
| 347 |
If `D` is a template and is defined in more than one translation unit,
|
| 348 |
then the preceding requirements shall apply both to names from the
|
| 349 |
template’s enclosing scope used in the template definition (
|
| 350 |
[[temp.nondep]]), and also to dependent names at the point of
|
| 351 |
instantiation ([[temp.dep]]). If the definitions of `D` satisfy all
|
|
@@ -368,11 +435,13 @@ scope* of a declaration. The scope of a declaration is the same as its
|
|
| 368 |
potential scope unless the potential scope contains another declaration
|
| 369 |
of the same name. In that case, the potential scope of the declaration
|
| 370 |
in the inner (contained) declarative region is excluded from the scope
|
| 371 |
of the declaration in the outer (containing) declarative region.
|
| 372 |
|
| 373 |
-
|
|
|
|
|
|
|
| 374 |
|
| 375 |
``` cpp
|
| 376 |
int j = 24;
|
| 377 |
int main() {
|
| 378 |
int i = j, j;
|
|
@@ -388,10 +457,12 @@ text between the `,` and the `}`. The declarative region of the second
|
|
| 388 |
declaration of `j` (the `j` immediately before the semicolon) includes
|
| 389 |
all the text between `{` and `}`, but its potential scope excludes the
|
| 390 |
declaration of `i`. The scope of the second declaration of `j` is the
|
| 391 |
same as its potential scope.
|
| 392 |
|
|
|
|
|
|
|
| 393 |
The names declared by a declaration are introduced into the scope in
|
| 394 |
which the declaration occurs, except that the presence of a `friend`
|
| 395 |
specifier ([[class.friend]]), certain uses of the
|
| 396 |
*elaborated-type-specifier* ([[dcl.type.elab]]), and
|
| 397 |
*using-directive*s ([[namespace.udir]]) alter this general behavior.
|
|
@@ -401,86 +472,110 @@ which specifies the same unqualified name,
|
|
| 401 |
|
| 402 |
- they shall all refer to the same entity, or all refer to functions and
|
| 403 |
function templates; or
|
| 404 |
- exactly one declaration shall declare a class name or enumeration name
|
| 405 |
that is not a typedef name and the other declarations shall all refer
|
| 406 |
-
to the same variable or enumerator, or all
|
| 407 |
-
function templates; in this case the class name
|
| 408 |
-
hidden ([[basic.scope.hiding]]).
|
| 409 |
-
name must be unique in its
|
| 410 |
-
|
|
|
|
| 411 |
|
| 412 |
-
These restrictions apply to the declarative region into
|
| 413 |
-
introduced, which is not necessarily the same as the
|
| 414 |
-
declaration occurs. In particular,
|
| 415 |
-
[[dcl.type.elab]]) and friend
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
introduce a (possibly not visible) name into an enclosing namespace;
|
| 417 |
-
these restrictions apply to
|
| 418 |
-
[[basic.link]]) may introduce a name into the declarative region where
|
| 419 |
-
the declaration appears and also introduce a (possibly not visible) name
|
| 420 |
-
into an enclosing namespace; these restrictions apply to both regions.
|
| 421 |
|
| 422 |
-
The name lookup rules are summarized in
|
|
|
|
| 423 |
|
| 424 |
### Point of declaration <a id="basic.scope.pdecl">[[basic.scope.pdecl]]</a>
|
| 425 |
|
| 426 |
The *point of declaration* for a name is immediately after its complete
|
| 427 |
declarator (Clause [[dcl.decl]]) and before its *initializer* (if any),
|
| 428 |
except as noted below.
|
| 429 |
|
|
|
|
|
|
|
| 430 |
``` cpp
|
| 431 |
unsigned char x = 12;
|
| 432 |
{ unsigned char x = x; }
|
| 433 |
```
|
| 434 |
|
| 435 |
Here the second `x` is initialized with its own (indeterminate) value.
|
| 436 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
a name from an outer scope remains visible up to the point of
|
| 438 |
declaration of the name that hides it.
|
| 439 |
|
|
|
|
|
|
|
| 440 |
``` cpp
|
| 441 |
const int i = 2;
|
| 442 |
{ int i[i]; }
|
| 443 |
```
|
| 444 |
|
| 445 |
declares a block-scope array of two integers.
|
| 446 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
The point of declaration for a class or class template first declared by
|
| 448 |
a *class-specifier* is immediately after the *identifier* or
|
| 449 |
*simple-template-id* (if any) in its *class-head* (Clause [[class]]).
|
| 450 |
The point of declaration for an enumeration is immediately after the
|
| 451 |
*identifier* (if any) in either its *enum-specifier* ([[dcl.enum]]) or
|
| 452 |
its first *opaque-enum-declaration* ([[dcl.enum]]), whichever comes
|
| 453 |
first. The point of declaration of an alias or alias template
|
| 454 |
immediately follows the *type-id* to which the alias refers.
|
| 455 |
|
| 456 |
-
The point of declaration of a *using-
|
| 457 |
-
constructor is immediately after the *using-
|
| 458 |
[[namespace.udecl]]).
|
| 459 |
|
| 460 |
The point of declaration for an enumerator is immediately after its
|
| 461 |
*enumerator-definition*.
|
| 462 |
|
|
|
|
|
|
|
| 463 |
``` cpp
|
| 464 |
const int x = 12;
|
| 465 |
{ enum { x = x }; }
|
| 466 |
```
|
| 467 |
|
| 468 |
Here, the enumerator `x` is initialized with the value of the constant
|
| 469 |
`x`, namely 12.
|
| 470 |
|
|
|
|
|
|
|
| 471 |
After the point of declaration of a class member, the member name can be
|
| 472 |
-
looked up in the scope of its class.
|
| 473 |
-
|
|
|
|
|
|
|
|
|
|
| 474 |
|
| 475 |
``` cpp
|
| 476 |
struct X {
|
| 477 |
enum E { z = 16 };
|
| 478 |
int b[X::z]; // OK
|
| 479 |
};
|
| 480 |
```
|
| 481 |
|
|
|
|
|
|
|
| 482 |
The point of declaration of a class first declared in an
|
| 483 |
*elaborated-type-specifier* is as follows:
|
| 484 |
|
| 485 |
- for a declaration of the form
|
| 486 |
``` bnf
|
|
@@ -497,14 +592,15 @@ The point of declaration of a class first declared in an
|
|
| 497 |
if the *elaborated-type-specifier* is used in the *decl-specifier-seq*
|
| 498 |
or *parameter-declaration-clause* of a function defined in namespace
|
| 499 |
scope, the *identifier* is declared as a *class-name* in the namespace
|
| 500 |
that contains the declaration; otherwise, except as a friend
|
| 501 |
declaration, the *identifier* is declared in the smallest namespace or
|
| 502 |
-
block scope that contains the declaration.
|
| 503 |
-
within templates.
|
| 504 |
-
|
| 505 |
-
*type-name*. See
|
|
|
|
| 506 |
|
| 507 |
The point of declaration for an *injected-class-name* (Clause
|
| 508 |
[[class]]) is immediately following the opening brace of the class
|
| 509 |
definition.
|
| 510 |
|
|
@@ -513,26 +609,32 @@ The point of declaration for a function-local predefined variable (
|
|
| 513 |
definition.
|
| 514 |
|
| 515 |
The point of declaration for a template parameter is immediately after
|
| 516 |
its complete *template-parameter*.
|
| 517 |
|
|
|
|
|
|
|
| 518 |
``` cpp
|
| 519 |
typedef unsigned char T;
|
| 520 |
template<class T
|
| 521 |
= T // lookup finds the typedef name of unsigned char
|
| 522 |
, T // lookup finds the template parameter
|
| 523 |
N = 0> struct A { };
|
| 524 |
```
|
| 525 |
|
| 526 |
-
|
| 527 |
-
the nearest enclosing namespace, but they do not introduce new names
|
| 528 |
-
into that namespace ([[namespace.memdef]]). Function declarations at
|
| 529 |
-
block scope and variable declarations with the `extern` specifier at
|
| 530 |
-
block scope refer to declarations that are members of an enclosing
|
| 531 |
-
namespace, but they do not introduce new names into that scope.
|
| 532 |
|
| 533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
|
| 535 |
### Block scope <a id="basic.scope.block">[[basic.scope.block]]</a>
|
| 536 |
|
| 537 |
A name declared in a block ([[stmt.block]]) is local to that block; it
|
| 538 |
has *block scope*. Its potential scope begins at its point of
|
|
@@ -552,14 +654,14 @@ in the outermost block of any handler associated with a
|
|
| 552 |
|
| 553 |
The name declared in an *exception-declaration* is local to the
|
| 554 |
*handler* and shall not be redeclared in the outermost block of the
|
| 555 |
*handler*.
|
| 556 |
|
| 557 |
-
Names declared in the *
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
condition of that statement nor in the outermost block (or, for the `if`
|
| 562 |
statement, any of the outermost blocks) of the controlled statement;
|
| 563 |
see [[stmt.select]].
|
| 564 |
|
| 565 |
### Function prototype scope <a id="basic.scope.proto">[[basic.scope.proto]]</a>
|
|
@@ -576,34 +678,32 @@ in the function in which they are declared. Only labels have function
|
|
| 576 |
scope.
|
| 577 |
|
| 578 |
### Namespace scope <a id="basic.scope.namespace">[[basic.scope.namespace]]</a>
|
| 579 |
|
| 580 |
The declarative region of a *namespace-definition* is its
|
| 581 |
-
*namespace-body*.
|
| 582 |
-
*
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
namespace
|
| 589 |
-
scope
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
*using-directive* that follows the member’s point of declaration.
|
| 594 |
|
| 595 |
``` cpp
|
| 596 |
namespace N {
|
| 597 |
int i;
|
| 598 |
int g(int a) { return a; }
|
| 599 |
int j();
|
| 600 |
void q();
|
| 601 |
}
|
| 602 |
namespace { int l=1; }
|
| 603 |
-
// the potential scope of l is from its point of declaration
|
| 604 |
-
// to the end of the translation unit
|
| 605 |
|
| 606 |
namespace N {
|
| 607 |
int g(char a) { // overloads N::g(int)
|
| 608 |
return l+a; // l is from unnamed namespace
|
| 609 |
}
|
|
@@ -616,14 +716,16 @@ namespace N {
|
|
| 616 |
}
|
| 617 |
int q(); // error: different return type
|
| 618 |
}
|
| 619 |
```
|
| 620 |
|
|
|
|
|
|
|
| 621 |
A namespace member can also be referred to after the `::` scope
|
| 622 |
resolution operator ([[expr.prim]]) applied to the name of its
|
| 623 |
namespace or the name of a namespace which nominates the member’s
|
| 624 |
-
namespace in a *using-directive
|
| 625 |
|
| 626 |
The outermost declarative region of a translation unit is also a
|
| 627 |
namespace, called the *global namespace*. A name declared in the global
|
| 628 |
namespace has *global namespace scope* (also called *global scope*). The
|
| 629 |
potential scope of such a name begins at its point of declaration (
|
|
@@ -631,11 +733,60 @@ potential scope of such a name begins at its point of declaration (
|
|
| 631 |
is its declarative region. A name with global namespace scope is said to
|
| 632 |
be a *global name*.
|
| 633 |
|
| 634 |
### Class scope <a id="basic.scope.class">[[basic.scope.class]]</a>
|
| 635 |
|
| 636 |
-
The
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 637 |
|
| 638 |
The name of a class member shall only be used as follows:
|
| 639 |
|
| 640 |
- in the scope of its class (as described above) or a class derived
|
| 641 |
(Clause [[class.derived]]) from its class,
|
|
@@ -664,10 +815,12 @@ Only template parameter names belong to this declarative region; any
|
|
| 664 |
other kind of name introduced by the *declaration* of a
|
| 665 |
*template-declaration* is instead introduced into the same declarative
|
| 666 |
region where it would be introduced as a result of a non-template
|
| 667 |
declaration of the same name.
|
| 668 |
|
|
|
|
|
|
|
| 669 |
``` cpp
|
| 670 |
namespace N {
|
| 671 |
template<class T> struct A { }; // #1
|
| 672 |
template<class U> void f(U) { } // #2
|
| 673 |
struct B {
|
|
@@ -675,58 +828,76 @@ namespace N {
|
|
| 675 |
};
|
| 676 |
}
|
| 677 |
```
|
| 678 |
|
| 679 |
The declarative regions of `T`, `U` and `V` are the
|
| 680 |
-
*template-declaration*s on lines
|
| 681 |
the names `A`, `f`, `g` and `C` all belong to the same declarative
|
| 682 |
region — namely, the *namespace-body* of `N`. (`g` is still considered
|
| 683 |
to belong to this declarative region in spite of its being hidden during
|
| 684 |
qualified and unqualified name lookup.)
|
| 685 |
|
|
|
|
|
|
|
| 686 |
The potential scope of a template parameter name begins at its point of
|
| 687 |
declaration ([[basic.scope.pdecl]]) and ends at the end of its
|
| 688 |
-
declarative region.
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 692 |
|
| 693 |
``` cpp
|
| 694 |
-
template<class T, T* p, class U = T> class X {
|
| 695 |
template<class T> void f(T* p = new T);
|
| 696 |
```
|
| 697 |
|
| 698 |
This also implies that a *template-parameter* can be used in the
|
| 699 |
specification of base classes. For example,
|
| 700 |
|
| 701 |
``` cpp
|
| 702 |
-
template<class T> class X : public Array<T> {
|
| 703 |
-
template<class T> class Y : public T {
|
| 704 |
```
|
| 705 |
|
| 706 |
The use of a template parameter as a base class implies that a class
|
| 707 |
used as a template argument must be defined and not just declared when
|
| 708 |
the class template is instantiated.
|
| 709 |
|
|
|
|
|
|
|
| 710 |
The declarative region of the name of a template parameter is nested
|
| 711 |
-
within the immediately-enclosing declarative region.
|
| 712 |
-
|
| 713 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 714 |
|
| 715 |
``` cpp
|
| 716 |
typedef int N;
|
| 717 |
template<N X, typename N, template<N Y> class T> struct A;
|
| 718 |
```
|
| 719 |
|
| 720 |
Here, `X` is a non-type template parameter of type `int` and `Y` is a
|
| 721 |
non-type template parameter of the same type as the second template
|
| 722 |
parameter of `A`.
|
| 723 |
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
| 727 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 728 |
|
| 729 |
### Name hiding <a id="basic.scope.hiding">[[basic.scope.hiding]]</a>
|
| 730 |
|
| 731 |
A name can be hidden by an explicit declaration of that same name in a
|
| 732 |
nested declarative region or derived class ([[class.member.lookup]]).
|
|
@@ -746,43 +917,42 @@ class (Clause [[class.derived]]) hides the declaration of a member of a
|
|
| 746 |
base class of the same name; see [[class.member.lookup]].
|
| 747 |
|
| 748 |
During the lookup of a name qualified by a namespace name, declarations
|
| 749 |
that would otherwise be made visible by a *using-directive* can be
|
| 750 |
hidden by declarations with the same name in the namespace containing
|
| 751 |
-
the *using-directive
|
| 752 |
|
| 753 |
If a name is in scope and is not hidden it is said to be *visible*.
|
| 754 |
|
| 755 |
## Name lookup <a id="basic.lookup">[[basic.lookup]]</a>
|
| 756 |
|
| 757 |
The name lookup rules apply uniformly to all names (including
|
| 758 |
-
*typedef-name*s ([[dcl.typedef]]), *namespace-name*s
|
| 759 |
[[basic.namespace]]), and *class-name*s ([[class.name]])) wherever the
|
| 760 |
grammar allows such names in the context discussed by a particular rule.
|
| 761 |
-
Name lookup associates the use of a name with a
|
| 762 |
-
[[basic.def]]) of that name.
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
[[
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
name’s declaration used further in expression processing (Clause
|
| 773 |
-
[[expr]]).
|
| 774 |
|
| 775 |
A name “looked up in the context of an expression” is looked up as an
|
| 776 |
unqualified name in the scope where the expression is found.
|
| 777 |
|
| 778 |
The injected-class-name of a class (Clause [[class]]) is also
|
| 779 |
considered to be a member of that class for the purposes of name hiding
|
| 780 |
and lookup.
|
| 781 |
|
| 782 |
-
[[basic.link]] discusses linkage issues. The notions of
|
| 783 |
-
declaration and name hiding are discussed in
|
|
|
|
| 784 |
|
| 785 |
### Unqualified name lookup <a id="basic.lookup.unqual">[[basic.lookup.unqual]]</a>
|
| 786 |
|
| 787 |
In all the cases listed in [[basic.lookup.unqual]], the scopes are
|
| 788 |
searched for a declaration in the order listed in each of the respective
|
|
@@ -795,12 +965,15 @@ become visible in a namespace enclosing the *using-directive*; see
|
|
| 795 |
described in [[basic.lookup.unqual]], the declarations from the
|
| 796 |
namespace nominated by the *using-directive* are considered members of
|
| 797 |
that enclosing namespace.
|
| 798 |
|
| 799 |
The lookup for an unqualified name used as the *postfix-expression* of a
|
| 800 |
-
function call is described in [[basic.lookup.argdep]].
|
| 801 |
-
|
|
|
|
|
|
|
|
|
|
| 802 |
*postfix-expression* for a function call, the usual name lookup rules
|
| 803 |
apply. The rules in [[basic.lookup.argdep]] have no effect on the
|
| 804 |
syntactic interpretation of an expression. For example,
|
| 805 |
|
| 806 |
``` cpp
|
|
@@ -808,37 +981,39 @@ typedef int f;
|
|
| 808 |
namespace N {
|
| 809 |
struct A {
|
| 810 |
friend void f(A &);
|
| 811 |
operator int();
|
| 812 |
void g(A a) {
|
| 813 |
-
int i = f(a);
|
| 814 |
-
// function: equivalent to int(a)
|
| 815 |
}
|
| 816 |
};
|
| 817 |
}
|
| 818 |
```
|
| 819 |
|
| 820 |
Because the expression is not a function call, the argument-dependent
|
| 821 |
name lookup ([[basic.lookup.argdep]]) does not apply and the friend
|
| 822 |
function `f` is not found.
|
| 823 |
|
|
|
|
|
|
|
| 824 |
A name used in global scope, outside of any function, class or
|
| 825 |
user-declared namespace, shall be declared before its use in global
|
| 826 |
scope.
|
| 827 |
|
| 828 |
A name used in a user-declared namespace outside of the definition of
|
| 829 |
any function or class shall be declared before its use in that namespace
|
| 830 |
or before its use in a namespace enclosing its namespace.
|
| 831 |
|
| 832 |
-
|
| 833 |
-
*declarator-id*[^4]
|
| 834 |
-
the
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
use in namespace `N` or, if `N` is a nested namespace, shall be declared
|
| 838 |
before its use in one of `N`’s enclosing namespaces.
|
| 839 |
|
|
|
|
|
|
|
| 840 |
``` cpp
|
| 841 |
namespace A {
|
| 842 |
namespace N {
|
| 843 |
void f();
|
| 844 |
}
|
|
@@ -851,30 +1026,34 @@ void A::N::f() {
|
|
| 851 |
// 3) scope of namespace A
|
| 852 |
// 4) global scope, before the definition of A::N::f
|
| 853 |
}
|
| 854 |
```
|
| 855 |
|
|
|
|
|
|
|
| 856 |
A name used in the definition of a class `X` outside of a member
|
| 857 |
-
function body, default argument, *
|
| 858 |
*brace-or-equal-initializer* of a non-static data member, or nested
|
| 859 |
class definition[^5] shall be declared in one of the following ways:
|
| 860 |
|
| 861 |
- before its use in class `X` or be a member of a base class of `X` (
|
| 862 |
[[class.member.lookup]]), or
|
| 863 |
- if `X` is a nested class of class `Y` ([[class.nest]]), before the
|
| 864 |
definition of `X` in `Y`, or shall be a member of a base class of `Y`
|
| 865 |
-
(this lookup applies in turn to `Y`
|
| 866 |
-
|
| 867 |
- if `X` is a local class ([[class.local]]) or is a nested class of a
|
| 868 |
local class, before the definition of class `X` in a block enclosing
|
| 869 |
the definition of class `X`, or
|
| 870 |
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 871 |
that is a member of `N`, or is a local class or a nested class within
|
| 872 |
a local class of a function that is a member of `N`, before the
|
| 873 |
definition of class `X` in namespace `N` or in one of `N`’s enclosing
|
| 874 |
namespaces.
|
| 875 |
|
|
|
|
|
|
|
| 876 |
``` cpp
|
| 877 |
namespace M {
|
| 878 |
class B { };
|
| 879 |
}
|
| 880 |
```
|
|
@@ -894,21 +1073,25 @@ namespace N {
|
|
| 894 |
// 3) scope of N::Y's base class M::B
|
| 895 |
// 4) scope of namespace N, before the definition of N::Y
|
| 896 |
// 5) global scope, before the definition of N
|
| 897 |
```
|
| 898 |
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 905 |
[[class.local]] further describes the restrictions on the use of names
|
| 906 |
-
in local class definitions.
|
| 907 |
|
| 908 |
For the members of a class `X`, a name used in a member function body,
|
| 909 |
-
in a default argument, in
|
| 910 |
*brace-or-equal-initializer* of a non-static data member (
|
| 911 |
[[class.mem]]), or in the definition of a class member outside of the
|
| 912 |
definition of `X`, following the member’s *declarator-id*[^7], shall be
|
| 913 |
declared in one of the following ways:
|
| 914 |
|
|
@@ -926,10 +1109,12 @@ declared in one of the following ways:
|
|
| 926 |
- if `X` is a member of namespace `N`, or is a nested class of a class
|
| 927 |
that is a member of `N`, or is a local class or a nested class within
|
| 928 |
a local class of a function that is a member of `N`, before the use of
|
| 929 |
the name, in namespace `N` or in one of `N`’s enclosing namespaces.
|
| 930 |
|
|
|
|
|
|
|
| 931 |
``` cpp
|
| 932 |
class B { };
|
| 933 |
namespace M {
|
| 934 |
namespace N {
|
| 935 |
class X : public B {
|
|
@@ -948,15 +1133,18 @@ void M::N::X::f() {
|
|
| 948 |
// 4) scope of namespace M::N
|
| 949 |
// 5) scope of namespace M
|
| 950 |
// 6) global scope, before the definition of M::N::X::f
|
| 951 |
```
|
| 952 |
|
| 953 |
-
|
| 954 |
-
|
| 955 |
-
|
| 956 |
-
|
| 957 |
-
the use of names in
|
|
|
|
|
|
|
|
|
|
| 958 |
|
| 959 |
Name lookup for a name used in the definition of a `friend` function (
|
| 960 |
[[class.friend]]) defined inline in the class granting friendship shall
|
| 961 |
proceed as described for lookup in member function definitions. If the
|
| 962 |
`friend` function is not defined in the class granting friendship, name
|
|
@@ -969,10 +1157,12 @@ function declarator and not part of a *template-argument* in the
|
|
| 969 |
class ([[class.member.lookup]]). If it is not found, or if the name is
|
| 970 |
part of a *template-argument* in the *declarator-id*, the look up is as
|
| 971 |
described for unqualified names in the definition of the class granting
|
| 972 |
friendship.
|
| 973 |
|
|
|
|
|
|
|
| 974 |
``` cpp
|
| 975 |
struct A {
|
| 976 |
typedef int AT;
|
| 977 |
void f1(AT);
|
| 978 |
void f2(float);
|
|
@@ -985,36 +1175,44 @@ struct B {
|
|
| 985 |
friend void A::f2(BT); // parameter type is B::BT
|
| 986 |
friend void A::f3<AT>(); // template argument is B::AT
|
| 987 |
};
|
| 988 |
```
|
| 989 |
|
|
|
|
|
|
|
| 990 |
During the lookup for a name used as a default argument (
|
| 991 |
[[dcl.fct.default]]) in a function *parameter-declaration-clause* or
|
| 992 |
used in the *expression* of a *mem-initializer* for a constructor (
|
| 993 |
[[class.base.init]]), the function parameter names are visible and hide
|
| 994 |
the names of entities declared in the block, class or namespace scopes
|
| 995 |
-
containing the function declaration.
|
| 996 |
-
|
| 997 |
-
[[
|
| 998 |
-
names in
|
|
|
|
|
|
|
| 999 |
|
| 1000 |
During the lookup of a name used in the *constant-expression* of an
|
| 1001 |
*enumerator-definition*, previously declared *enumerator*s of the
|
| 1002 |
enumeration are visible and hide the names of entities declared in the
|
| 1003 |
block, class, or namespace scopes containing the *enum-specifier*.
|
| 1004 |
|
| 1005 |
A name used in the definition of a `static` data member of class `X` (
|
| 1006 |
[[class.static.data]]) (after the *qualified-id* of the static member)
|
| 1007 |
is looked up as if the name was used in a member function of `X`.
|
| 1008 |
-
|
| 1009 |
-
|
|
|
|
|
|
|
| 1010 |
|
| 1011 |
If a variable member of a namespace is defined outside of the scope of
|
| 1012 |
its namespace then any name that appears in the definition of the member
|
| 1013 |
(after the *declarator-id*) is looked up as if the definition of the
|
| 1014 |
member occurred in its namespace.
|
| 1015 |
|
|
|
|
|
|
|
| 1016 |
``` cpp
|
| 1017 |
namespace N {
|
| 1018 |
int i = 4;
|
| 1019 |
extern int j;
|
| 1020 |
}
|
|
@@ -1022,21 +1220,24 @@ namespace N {
|
|
| 1022 |
int i = 2;
|
| 1023 |
|
| 1024 |
int N::j = i; // N::j == 4
|
| 1025 |
```
|
| 1026 |
|
|
|
|
|
|
|
| 1027 |
A name used in the handler for a *function-try-block* (Clause
|
| 1028 |
[[except]]) is looked up as if the name was used in the outermost block
|
| 1029 |
of the function definition. In particular, the function parameter names
|
| 1030 |
shall not be redeclared in the *exception-declaration* nor in the
|
| 1031 |
outermost block of a handler for the *function-try-block*. Names
|
| 1032 |
declared in the outermost block of the function definition are not found
|
| 1033 |
when looked up in the scope of a handler for the *function-try-block*.
|
| 1034 |
-
But function parameter names are found.
|
| 1035 |
|
| 1036 |
-
|
| 1037 |
-
|
|
|
|
|
|
|
| 1038 |
|
| 1039 |
### Argument-dependent name lookup <a id="basic.lookup.argdep">[[basic.lookup.argdep]]</a>
|
| 1040 |
|
| 1041 |
When the *postfix-expression* in a function call ([[expr.call]]) is an
|
| 1042 |
*unqualified-id*, other namespaces not considered during the usual
|
|
@@ -1045,27 +1246,30 @@ those namespaces, namespace-scope friend function or function template
|
|
| 1045 |
declarations ([[class.friend]]) not otherwise visible may be found.
|
| 1046 |
These modifications to the search depend on the types of the arguments
|
| 1047 |
(and for template template arguments, the namespace of the template
|
| 1048 |
argument).
|
| 1049 |
|
|
|
|
|
|
|
| 1050 |
``` cpp
|
| 1051 |
namespace N {
|
| 1052 |
struct S { };
|
| 1053 |
void f(S);
|
| 1054 |
}
|
| 1055 |
|
| 1056 |
void g() {
|
| 1057 |
N::S s;
|
| 1058 |
f(s); // OK: calls N::f
|
| 1059 |
-
(f)(s);
|
| 1060 |
-
// prevent argument-dependent lookup
|
| 1061 |
}
|
| 1062 |
```
|
| 1063 |
|
|
|
|
|
|
|
| 1064 |
For each argument type `T` in the function call, there is a set of zero
|
| 1065 |
-
or more associated namespaces and a set of zero or more associated
|
| 1066 |
-
classes to be considered. The sets of namespaces and classes
|
| 1067 |
determined entirely by the types of the function arguments (and the
|
| 1068 |
namespace of any template template argument). Typedef names and
|
| 1069 |
*using-declaration*s used to specify the types do not contribute to this
|
| 1070 |
set. The sets of namespaces and classes are determined in the following
|
| 1071 |
way:
|
|
@@ -1080,12 +1284,12 @@ way:
|
|
| 1080 |
and classes also include: the namespaces and classes associated with
|
| 1081 |
the types of the template arguments provided for template type
|
| 1082 |
parameters (excluding template template parameters); the namespaces of
|
| 1083 |
which any template template arguments are members; and the classes of
|
| 1084 |
which any member templates used as template template arguments are
|
| 1085 |
-
members. Non-type template arguments do not contribute to
|
| 1086 |
-
associated namespaces.
|
| 1087 |
- If `T` is an enumeration type, its associated namespace is the
|
| 1088 |
innermost enclosing namespace of its declaration. If it is a class
|
| 1089 |
member, its associated class is the member’s class; else it has no
|
| 1090 |
associated class.
|
| 1091 |
- If `T` is a pointer to `U` or an array of `U`, its associated
|
|
@@ -1118,18 +1322,22 @@ Let *X* be the lookup set produced by unqualified lookup (
|
|
| 1118 |
argument dependent lookup (defined as follows). If *X* contains
|
| 1119 |
|
| 1120 |
- a declaration of a class member, or
|
| 1121 |
- a block-scope function declaration that is not a *using-declaration*,
|
| 1122 |
or
|
| 1123 |
-
- a declaration that is neither a function
|
| 1124 |
|
| 1125 |
then *Y* is empty. Otherwise *Y* is the set of declarations found in the
|
| 1126 |
namespaces associated with the argument types as described below. The
|
| 1127 |
set of declarations found by the lookup of the name is the union of *X*
|
| 1128 |
-
and *Y*.
|
| 1129 |
-
|
| 1130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1131 |
|
| 1132 |
``` cpp
|
| 1133 |
namespace NS {
|
| 1134 |
class T { };
|
| 1135 |
void f(T);
|
|
@@ -1142,10 +1350,12 @@ int main() {
|
|
| 1142 |
extern void g(NS::T, float);
|
| 1143 |
g(parm, 1); // OK: calls g(NS::T, float)
|
| 1144 |
}
|
| 1145 |
```
|
| 1146 |
|
|
|
|
|
|
|
| 1147 |
When considering an associated namespace, the lookup is the same as the
|
| 1148 |
lookup performed when the associated namespace is used as a qualifier (
|
| 1149 |
[[namespace.qual]]) except that:
|
| 1150 |
|
| 1151 |
- Any *using-directive*s in the associated namespace are ignored.
|
|
@@ -1166,10 +1376,12 @@ enumeration. If a `::` scope resolution operator in a
|
|
| 1166 |
lookup of the name preceding that `::` considers only namespaces, types,
|
| 1167 |
and templates whose specializations are types. If the name found does
|
| 1168 |
not designate a namespace or a class, enumeration, or dependent type,
|
| 1169 |
the program is ill-formed.
|
| 1170 |
|
|
|
|
|
|
|
| 1171 |
``` cpp
|
| 1172 |
class A {
|
| 1173 |
public:
|
| 1174 |
static int n;
|
| 1175 |
};
|
|
@@ -1178,31 +1390,37 @@ int main() {
|
|
| 1178 |
A::n = 42; // OK
|
| 1179 |
A b; // ill-formed: A does not name a type
|
| 1180 |
}
|
| 1181 |
```
|
| 1182 |
|
| 1183 |
-
|
| 1184 |
-
|
| 1185 |
-
|
|
|
|
|
|
|
| 1186 |
|
| 1187 |
In a declaration in which the *declarator-id* is a *qualified-id*, names
|
| 1188 |
used before the *qualified-id* being declared are looked up in the
|
| 1189 |
defining namespace scope; names following the *qualified-id* are looked
|
| 1190 |
up in the scope of the member’s class or namespace.
|
| 1191 |
|
|
|
|
|
|
|
| 1192 |
``` cpp
|
| 1193 |
class X { };
|
| 1194 |
class C {
|
| 1195 |
class X { };
|
| 1196 |
static const int number = 50;
|
| 1197 |
static X arr[number];
|
| 1198 |
};
|
| 1199 |
X C::arr[number]; // ill-formed:
|
| 1200 |
-
// equivalent to
|
| 1201 |
-
// not to
|
| 1202 |
```
|
| 1203 |
|
|
|
|
|
|
|
| 1204 |
A name prefixed by the unary scope operator `::` ([[expr.prim]]) is
|
| 1205 |
looked up in global scope, in the translation unit where it is used. The
|
| 1206 |
name shall be declared in global namespace scope or shall be a name
|
| 1207 |
whose declaration is visible in global scope because of a
|
| 1208 |
*using-directive* ([[namespace.qual]]). The use of `::` allows a global
|
|
@@ -1221,20 +1439,21 @@ scope designated by the *nested-name-specifier*. Similarly, in a
|
|
| 1221 |
nested-name-specifierₒₚₜ class-name '::' '~' class-name
|
| 1222 |
```
|
| 1223 |
|
| 1224 |
the second *class-name* is looked up in the same scope as the first.
|
| 1225 |
|
|
|
|
|
|
|
| 1226 |
``` cpp
|
| 1227 |
struct C {
|
| 1228 |
typedef int I;
|
| 1229 |
};
|
| 1230 |
typedef int I1, I2;
|
| 1231 |
extern int* p;
|
| 1232 |
extern int* q;
|
| 1233 |
p->C::I::~I(); // I is looked up in the scope of C
|
| 1234 |
-
q->I1::~I2(); // I2 is looked up in the scope of
|
| 1235 |
-
// the postfix-expression
|
| 1236 |
|
| 1237 |
struct A {
|
| 1238 |
~A();
|
| 1239 |
};
|
| 1240 |
typedef A AB;
|
|
@@ -1242,25 +1461,30 @@ int main() {
|
|
| 1242 |
AB* p;
|
| 1243 |
p->AB::~AB(); // explicitly calls the destructor for A
|
| 1244 |
}
|
| 1245 |
```
|
| 1246 |
|
| 1247 |
-
|
| 1248 |
-
|
|
|
|
|
|
|
| 1249 |
|
| 1250 |
#### Class members <a id="class.qual">[[class.qual]]</a>
|
| 1251 |
|
| 1252 |
If the *nested-name-specifier* of a *qualified-id* nominates a class,
|
| 1253 |
the name specified after the *nested-name-specifier* is looked up in the
|
| 1254 |
scope of the class ([[class.member.lookup]]), except for the cases
|
| 1255 |
listed below. The name shall represent one or more members of that class
|
| 1256 |
-
or of one of its base classes (Clause [[class.derived]]).
|
| 1257 |
-
member can be referred to using a *qualified-id* at any point in its
|
| 1258 |
-
potential scope ([[basic.scope.class]]). The exceptions to the name
|
| 1259 |
-
lookup rule above are the following:
|
| 1260 |
|
| 1261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1262 |
- a *conversion-type-id* of a *conversion-function-id* is looked up in
|
| 1263 |
the same manner as a *conversion-type-id* in a class member access
|
| 1264 |
(see [[basic.lookup.classref]]);
|
| 1265 |
- the names in a *template-argument* of a *template-id* are looked up in
|
| 1266 |
the context in which the entire *postfix-expression* occurs.
|
|
@@ -1271,22 +1495,26 @@ lookup rule above are the following:
|
|
| 1271 |
In a lookup in which function names are not ignored[^9] and the
|
| 1272 |
*nested-name-specifier* nominates a class `C`:
|
| 1273 |
|
| 1274 |
- if the name specified after the *nested-name-specifier*, when looked
|
| 1275 |
up in `C`, is the injected-class-name of `C` (Clause [[class]]), or
|
| 1276 |
-
- in a *using-
|
| 1277 |
-
*member-declaration*, if the name
|
| 1278 |
-
*nested-name-specifier* is the same as the
|
| 1279 |
-
*simple-template-id*’s *template-name* in the last
|
| 1280 |
-
*nested-name-specifier*,
|
| 1281 |
-
|
| 1282 |
-
the name is instead considered to name the constructor of class `C`.
|
| 1283 |
-
|
| 1284 |
-
*
|
| 1285 |
-
|
| 1286 |
-
|
| 1287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1288 |
|
| 1289 |
``` cpp
|
| 1290 |
struct A { A(); };
|
| 1291 |
struct B: public A { B(); };
|
| 1292 |
|
|
@@ -1296,39 +1524,42 @@ B::B() { }
|
|
| 1296 |
B::A ba; // object of type A
|
| 1297 |
A::A a; // error, A::A is not a type name
|
| 1298 |
struct A::A a2; // object of type A
|
| 1299 |
```
|
| 1300 |
|
|
|
|
|
|
|
| 1301 |
A class member name hidden by a name in a nested declarative region or
|
| 1302 |
by the name of a derived class member can still be found if qualified by
|
| 1303 |
the name of its class followed by the `::` operator.
|
| 1304 |
|
| 1305 |
#### Namespace members <a id="namespace.qual">[[namespace.qual]]</a>
|
| 1306 |
|
| 1307 |
-
If the *nested-name-specifier* of a *qualified-id* nominates a
|
| 1308 |
-
|
| 1309 |
-
|
| 1310 |
-
|
| 1311 |
-
|
| 1312 |
-
|
| 1313 |
-
occurs.
|
| 1314 |
|
| 1315 |
For a namespace `X` and name `m`, the namespace-qualified lookup set
|
| 1316 |
S(X, m) is defined as follows: Let S'(X, m) be the set of all
|
| 1317 |
declarations of `m` in `X` and the inline namespace set of `X` (
|
| 1318 |
[[namespace.def]]). If S'(X, m) is not empty, S(X, m) is S'(X, m);
|
| 1319 |
otherwise, S(X, m) is the union of S(Nᵢ, m) for all namespaces Nᵢ
|
| 1320 |
-
nominated by *using-
|
| 1321 |
|
| 1322 |
Given `X::m` (where `X` is a user-declared namespace), or given `::m`
|
| 1323 |
(where X is the global namespace), if S(X, m) is the empty set, the
|
| 1324 |
program is ill-formed. Otherwise, if S(X, m) has exactly one member, or
|
| 1325 |
if the context of the reference is a *using-declaration* (
|
| 1326 |
[[namespace.udecl]]), S(X, m) is the required set of declarations of
|
| 1327 |
`m`. Otherwise if the use of `m` is not one that allows a unique
|
| 1328 |
declaration to be chosen from S(X, m), the program is ill-formed.
|
| 1329 |
|
|
|
|
|
|
|
| 1330 |
``` cpp
|
| 1331 |
int x;
|
| 1332 |
namespace Y {
|
| 1333 |
void f(float);
|
| 1334 |
void h(int);
|
|
@@ -1357,38 +1588,38 @@ namespace AB {
|
|
| 1357 |
void g();
|
| 1358 |
}
|
| 1359 |
|
| 1360 |
void h()
|
| 1361 |
{
|
| 1362 |
-
AB::g(); // g is declared directly in AB,
|
| 1363 |
-
|
| 1364 |
-
AB::f(1); // f is not declared directly in AB so the rules are
|
| 1365 |
-
//
|
| 1366 |
-
//
|
| 1367 |
-
|
| 1368 |
-
// S is { A::f(int), B::f(char) } and overload
|
| 1369 |
-
// resolution chooses A::f(int)
|
| 1370 |
AB::f('c'); // as above but resolution chooses B::f(char)
|
| 1371 |
|
| 1372 |
-
AB::x++; // x is not declared directly in AB, and
|
| 1373 |
-
//
|
| 1374 |
-
|
| 1375 |
-
|
| 1376 |
-
|
| 1377 |
-
|
| 1378 |
-
|
| 1379 |
-
// and
|
| 1380 |
-
|
| 1381 |
-
// not declared directly in A or B so the rules are
|
| 1382 |
-
// applied recursively to Y and Z,
|
| 1383 |
-
// S is { Y::h(int), Z::h(double) } and overload
|
| 1384 |
-
// resolution chooses Z::h(double)
|
| 1385 |
}
|
| 1386 |
```
|
| 1387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1388 |
The same declaration found more than once is not an ambiguity (because
|
| 1389 |
-
it is still a unique declaration).
|
|
|
|
|
|
|
| 1390 |
|
| 1391 |
``` cpp
|
| 1392 |
namespace A {
|
| 1393 |
int a;
|
| 1394 |
}
|
|
@@ -1406,11 +1637,11 @@ namespace BC {
|
|
| 1406 |
using namespace C;
|
| 1407 |
}
|
| 1408 |
|
| 1409 |
void f()
|
| 1410 |
{
|
| 1411 |
-
BC::a++; // OK: S is { A::a, A::a }
|
| 1412 |
}
|
| 1413 |
|
| 1414 |
namespace D {
|
| 1415 |
using A::a;
|
| 1416 |
}
|
|
@@ -1420,14 +1651,20 @@ namespace BD {
|
|
| 1420 |
using namespace D;
|
| 1421 |
}
|
| 1422 |
|
| 1423 |
void g()
|
| 1424 |
{
|
| 1425 |
-
BD::a++; // OK: S is {
|
| 1426 |
}
|
| 1427 |
```
|
| 1428 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1429 |
Because each referenced namespace is searched at most once, the
|
| 1430 |
following is well-defined:
|
| 1431 |
|
| 1432 |
``` cpp
|
| 1433 |
namespace B {
|
|
@@ -1443,25 +1680,29 @@ namespace B {
|
|
| 1443 |
using namespace A;
|
| 1444 |
}
|
| 1445 |
|
| 1446 |
void f()
|
| 1447 |
{
|
| 1448 |
-
A::a++; // OK: a declared directly in A, S is {A::a}
|
| 1449 |
-
B::a++; // OK: both A and B searched (once), S is {A::a}
|
| 1450 |
-
A::b++; // OK: both A and B searched (once), S is {B::b}
|
| 1451 |
-
B::b++; // OK: b declared directly in B, S is {B::b}
|
| 1452 |
}
|
| 1453 |
```
|
| 1454 |
|
|
|
|
|
|
|
| 1455 |
During the lookup of a qualified namespace member name, if the lookup
|
| 1456 |
finds more than one declaration of the member, and if one declaration
|
| 1457 |
introduces a class name or enumeration name and the other declarations
|
| 1458 |
either introduce the same variable, the same enumerator or a set of
|
| 1459 |
functions, the non-type name hides the class or enumeration name if and
|
| 1460 |
only if the declarations are from the same namespace; otherwise (the
|
| 1461 |
declarations are from different namespaces), the program is ill-formed.
|
| 1462 |
|
|
|
|
|
|
|
| 1463 |
``` cpp
|
| 1464 |
namespace A {
|
| 1465 |
struct x { };
|
| 1466 |
int x;
|
| 1467 |
int y;
|
|
@@ -1477,10 +1718,12 @@ namespace C {
|
|
| 1477 |
int i = C::x; // OK, A::x (of type int)
|
| 1478 |
int j = C::y; // ambiguous, A::y or B::y
|
| 1479 |
}
|
| 1480 |
```
|
| 1481 |
|
|
|
|
|
|
|
| 1482 |
In a declaration for a namespace member in which the *declarator-id* is
|
| 1483 |
a *qualified-id*, given that the *qualified-id* for the namespace member
|
| 1484 |
has the form
|
| 1485 |
|
| 1486 |
``` bnf
|
|
@@ -1489,24 +1732,30 @@ nested-name-specifier unqualified-id
|
|
| 1489 |
|
| 1490 |
the *unqualified-id* shall name a member of the namespace designated by
|
| 1491 |
the *nested-name-specifier* or of an element of the inline namespace
|
| 1492 |
set ([[namespace.def]]) of that namespace.
|
| 1493 |
|
|
|
|
|
|
|
| 1494 |
``` cpp
|
| 1495 |
namespace A {
|
| 1496 |
namespace B {
|
| 1497 |
void f1(int);
|
| 1498 |
}
|
| 1499 |
using namespace B;
|
| 1500 |
}
|
| 1501 |
void A::f1(int){ } // ill-formed, f1 is not a member of A
|
| 1502 |
```
|
| 1503 |
|
|
|
|
|
|
|
| 1504 |
However, in such namespace member declarations, the
|
| 1505 |
*nested-name-specifier* may rely on *using-directive*s to implicitly
|
| 1506 |
provide the initial part of the *nested-name-specifier*.
|
| 1507 |
|
|
|
|
|
|
|
| 1508 |
``` cpp
|
| 1509 |
namespace A {
|
| 1510 |
namespace B {
|
| 1511 |
void f1(int);
|
| 1512 |
}
|
|
@@ -1521,10 +1770,12 @@ namespace C {
|
|
| 1521 |
using namespace A;
|
| 1522 |
using namespace C::D;
|
| 1523 |
void B::f1(int){ } // OK, defines A::B::f1(int)
|
| 1524 |
```
|
| 1525 |
|
|
|
|
|
|
|
| 1526 |
### Elaborated type specifiers <a id="basic.lookup.elab">[[basic.lookup.elab]]</a>
|
| 1527 |
|
| 1528 |
An *elaborated-type-specifier* ([[dcl.type.elab]]) may be used to refer
|
| 1529 |
to a previously declared *class-name* or *enum-name* even though the
|
| 1530 |
name has been hidden by a non-type declaration (
|
|
@@ -1558,42 +1809,44 @@ If the *elaborated-type-specifier* has a *nested-name-specifier*,
|
|
| 1558 |
qualified name lookup is performed, as described in
|
| 1559 |
[[basic.lookup.qual]], but ignoring any non-type names that have been
|
| 1560 |
declared. If the name lookup does not find a previously declared
|
| 1561 |
*type-name*, the *elaborated-type-specifier* is ill-formed.
|
| 1562 |
|
|
|
|
|
|
|
| 1563 |
``` cpp
|
| 1564 |
struct Node {
|
| 1565 |
struct Node* Next; // OK: Refers to Node at global scope
|
| 1566 |
struct Data* Data; // OK: Declares type Data
|
| 1567 |
// at global scope and member Data
|
| 1568 |
};
|
| 1569 |
|
| 1570 |
struct Data {
|
| 1571 |
struct Node* Node; // OK: Refers to Node at global scope
|
| 1572 |
-
friend struct ::Glob; // error: Glob is not declared
|
| 1573 |
-
|
| 1574 |
-
|
| 1575 |
-
// at global scope.
|
| 1576 |
-
/* ... */
|
| 1577 |
};
|
| 1578 |
|
| 1579 |
struct Base {
|
| 1580 |
struct Data; // OK: Declares nested Data
|
| 1581 |
struct ::Data* thatData; // OK: Refers to ::Data
|
| 1582 |
struct Base::Data* thisData; // OK: Refers to nested Data
|
| 1583 |
friend class ::Data; // OK: global Data is a friend
|
| 1584 |
friend class Data; // OK: nested Data is a friend
|
| 1585 |
-
struct Data {
|
| 1586 |
};
|
| 1587 |
|
| 1588 |
struct Data; // OK: Redeclares Data at global scope
|
| 1589 |
struct ::Data; // error: cannot introduce a qualified type~([dcl.type.elab])
|
| 1590 |
struct Base::Data; // error: cannot introduce a qualified type~([dcl.type.elab])
|
| 1591 |
struct Base::Datum; // error: Datum undefined
|
| 1592 |
struct Base::Data* pBase; // OK: refers to nested Data
|
| 1593 |
```
|
| 1594 |
|
|
|
|
|
|
|
| 1595 |
### Class member access <a id="basic.lookup.classref">[[basic.lookup.classref]]</a>
|
| 1596 |
|
| 1597 |
In a class member access expression ([[expr.ref]]), if the `.` or `->`
|
| 1598 |
token is immediately followed by an *identifier* followed by a `<`, the
|
| 1599 |
identifier must be looked up to determine whether the `<` is the
|
|
@@ -1611,11 +1864,13 @@ looked up in the context of the complete *postfix-expression*.
|
|
| 1611 |
|
| 1612 |
If the *unqualified-id* is `~`*type-name*, the *type-name* is looked up
|
| 1613 |
in the context of the entire *postfix-expression*. If the type `T` of
|
| 1614 |
the object expression is of a class type `C`, the *type-name* is also
|
| 1615 |
looked up in the scope of class `C`. At least one of the lookups shall
|
| 1616 |
-
find a name that refers to
|
|
|
|
|
|
|
| 1617 |
|
| 1618 |
``` cpp
|
| 1619 |
struct A { };
|
| 1620 |
|
| 1621 |
struct B {
|
|
@@ -1626,22 +1881,34 @@ struct B {
|
|
| 1626 |
void B::f(::A* a) {
|
| 1627 |
a->~A(); // OK: lookup in *a finds the injected-class-name
|
| 1628 |
}
|
| 1629 |
```
|
| 1630 |
|
|
|
|
|
|
|
| 1631 |
If the *id-expression* in a class member access is a *qualified-id* of
|
| 1632 |
the form
|
| 1633 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1634 |
the *class-name-or-namespace-name* following the `.` or `->` operator is
|
| 1635 |
first looked up in the class of the object expression and the name, if
|
| 1636 |
found, is used. Otherwise it is looked up in the context of the entire
|
| 1637 |
-
*postfix-expression*.
|
| 1638 |
-
|
| 1639 |
-
|
|
|
|
|
|
|
| 1640 |
|
| 1641 |
If the *qualified-id* has the form
|
| 1642 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1643 |
the *class-name-or-namespace-name* is looked up in global scope as a
|
| 1644 |
*class-name* or *namespace-name*.
|
| 1645 |
|
| 1646 |
If the *nested-name-specifier* contains a *simple-template-id* (
|
| 1647 |
[[temp.names]]), the names in its *template-argument*s are looked up in
|
|
@@ -1652,10 +1919,12 @@ If the *id-expression* is a *conversion-function-id*, its
|
|
| 1652 |
expression and the name, if found, is used. Otherwise it is looked up in
|
| 1653 |
the context of the entire *postfix-expression*. In each of these
|
| 1654 |
lookups, only names that denote types or templates whose specializations
|
| 1655 |
are types are considered.
|
| 1656 |
|
|
|
|
|
|
|
| 1657 |
``` cpp
|
| 1658 |
struct A { };
|
| 1659 |
namespace N {
|
| 1660 |
struct A {
|
| 1661 |
void g() { }
|
|
@@ -1667,10 +1936,12 @@ int main() {
|
|
| 1667 |
N::A a;
|
| 1668 |
a.operator A(); // calls N::A::operator N::A
|
| 1669 |
}
|
| 1670 |
```
|
| 1671 |
|
|
|
|
|
|
|
| 1672 |
### Using-directives and namespace aliases <a id="basic.lookup.udir">[[basic.lookup.udir]]</a>
|
| 1673 |
|
| 1674 |
In a *using-directive* or *namespace-alias-definition*, during the
|
| 1675 |
lookup for a *namespace-name* or for a name in a *nested-name-specifier*
|
| 1676 |
only namespace names are considered.
|
|
@@ -1701,13 +1972,13 @@ introduced by a declaration in another scope:
|
|
| 1701 |
A name having namespace scope ([[basic.scope.namespace]]) has internal
|
| 1702 |
linkage if it is the name of
|
| 1703 |
|
| 1704 |
- a variable, function or function template that is explicitly declared
|
| 1705 |
`static`; or,
|
| 1706 |
-
- a non-
|
| 1707 |
-
|
| 1708 |
-
|
| 1709 |
- a data member of an anonymous union.
|
| 1710 |
|
| 1711 |
An unnamed namespace or a namespace declared directly or indirectly
|
| 1712 |
within an unnamed namespace has internal linkage. All other namespaces
|
| 1713 |
have external linkage. A name having namespace scope that has not been
|
|
@@ -1720,54 +1991,59 @@ namespace if it is the name of
|
|
| 1720 |
typedef declaration in which the class has the typedef name for
|
| 1721 |
linkage purposes ([[dcl.typedef]]); or
|
| 1722 |
- a named enumeration ([[dcl.enum]]), or an unnamed enumeration defined
|
| 1723 |
in a typedef declaration in which the enumeration has the typedef name
|
| 1724 |
for linkage purposes ([[dcl.typedef]]); or
|
| 1725 |
-
- an enumerator belonging to an enumeration with linkage; or
|
| 1726 |
- a template.
|
| 1727 |
|
| 1728 |
In addition, a member function, static data member, a named class or
|
| 1729 |
enumeration of class scope, or an unnamed class or enumeration defined
|
| 1730 |
in a class-scope typedef declaration such that the class or enumeration
|
| 1731 |
-
has the typedef name for linkage purposes ([[dcl.typedef]]), has
|
| 1732 |
-
|
| 1733 |
|
| 1734 |
The name of a function declared in block scope and the name of a
|
| 1735 |
variable declared by a block scope `extern` declaration have linkage. If
|
| 1736 |
there is a visible declaration of an entity with linkage having the same
|
| 1737 |
name and type, ignoring entities declared outside the innermost
|
| 1738 |
enclosing namespace scope, the block scope declaration declares that
|
| 1739 |
same entity and receives the linkage of the previous declaration. If
|
| 1740 |
there is more than one such matching entity, the program is ill-formed.
|
| 1741 |
Otherwise, if no matching entity is found, the block scope entity
|
| 1742 |
-
receives external linkage.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1743 |
|
| 1744 |
``` cpp
|
| 1745 |
static void f();
|
| 1746 |
static int i = 0; // #1
|
| 1747 |
void g() {
|
| 1748 |
extern void f(); // internal linkage
|
| 1749 |
-
int i; // #2 i has no linkage
|
| 1750 |
{
|
| 1751 |
extern void f(); // internal linkage
|
| 1752 |
-
extern int i; // #3 external linkage
|
| 1753 |
}
|
| 1754 |
}
|
| 1755 |
```
|
| 1756 |
|
| 1757 |
-
|
| 1758 |
-
|
| 1759 |
-
|
| 1760 |
-
|
| 1761 |
-
|
| 1762 |
-
|
| 1763 |
|
| 1764 |
When a block scope declaration of an entity with linkage is not found to
|
| 1765 |
refer to some other declaration, then that entity is a member of the
|
| 1766 |
innermost enclosing namespace. However such a declaration does not
|
| 1767 |
introduce the member name in its namespace scope.
|
| 1768 |
|
|
|
|
|
|
|
| 1769 |
``` cpp
|
| 1770 |
namespace X {
|
| 1771 |
void p() {
|
| 1772 |
q(); // error: q not yet declared
|
| 1773 |
extern void q(); // q is a member of namespace X
|
|
@@ -1775,24 +2051,26 @@ namespace X {
|
|
| 1775 |
|
| 1776 |
void middle() {
|
| 1777 |
q(); // error: q not yet declared
|
| 1778 |
}
|
| 1779 |
|
| 1780 |
-
void q() {
|
| 1781 |
}
|
| 1782 |
|
| 1783 |
-
void q() {
|
| 1784 |
```
|
| 1785 |
|
|
|
|
|
|
|
| 1786 |
Names not covered by these rules have no linkage. Moreover, except as
|
| 1787 |
noted, a name declared at block scope ([[basic.scope.block]]) has no
|
| 1788 |
linkage. A type is said to have linkage if and only if:
|
| 1789 |
|
| 1790 |
- it is a class or enumeration type that is named (or has a name for
|
| 1791 |
linkage purposes ([[dcl.typedef]])) and the name has linkage; or
|
| 1792 |
-
- it is an unnamed class or enumeration
|
| 1793 |
-
or
|
| 1794 |
- it is a specialization of a class template (Clause [[temp]])[^10]; or
|
| 1795 |
- it is a fundamental type ([[basic.fundamental]]); or
|
| 1796 |
- it is a compound type ([[basic.compound]]) other than a class or
|
| 1797 |
enumeration, compounded exclusively from types that have linkage; or
|
| 1798 |
- it is a cv-qualified ([[basic.type.qualifier]]) version of a type
|
|
@@ -1805,18 +2083,20 @@ function with external linkage unless
|
|
| 1805 |
- the entity is declared within an unnamed namespace (
|
| 1806 |
[[namespace.def]]), or
|
| 1807 |
- the entity is not odr-used ([[basic.def.odr]]) or is defined in the
|
| 1808 |
same translation unit.
|
| 1809 |
|
| 1810 |
-
In other words, a type without linkage contains a class or
|
| 1811 |
-
that cannot be named outside its translation unit. An entity
|
| 1812 |
-
external linkage declared using such a type could not correspond to
|
| 1813 |
-
other entity in another translation unit of the program and thus
|
| 1814 |
-
defined in the translation unit if it is odr-used. Also note
|
| 1815 |
-
classes with linkage may contain members whose types do not have
|
| 1816 |
linkage, and that typedef names are ignored in the determination of
|
| 1817 |
-
whether a type has linkage.
|
|
|
|
|
|
|
| 1818 |
|
| 1819 |
``` cpp
|
| 1820 |
template <class T> struct B {
|
| 1821 |
void g(T) { }
|
| 1822 |
void h(T);
|
|
@@ -1831,13 +2111,15 @@ void f() {
|
|
| 1831 |
ba.h(a); // error: B<A>::h(A) not defined in the translation unit
|
| 1832 |
i(ba, a); // OK
|
| 1833 |
}
|
| 1834 |
```
|
| 1835 |
|
|
|
|
|
|
|
| 1836 |
Two names that are the same (Clause [[basic]]) and that are declared in
|
| 1837 |
different scopes shall denote the same variable, function, type,
|
| 1838 |
-
|
| 1839 |
|
| 1840 |
- both names have external linkage or else both names have internal
|
| 1841 |
linkage and are declared in the same translation unit; and
|
| 1842 |
- both names refer to members of the same namespace or to members, not
|
| 1843 |
by inheritance, of the same class; and
|
|
@@ -1852,30 +2134,35 @@ declarations referring to a given variable or function shall be
|
|
| 1852 |
identical, except that declarations for an array object can specify
|
| 1853 |
array types that differ by the presence or absence of a major array
|
| 1854 |
bound ([[dcl.array]]). A violation of this rule on type identity does
|
| 1855 |
not require a diagnostic.
|
| 1856 |
|
| 1857 |
-
Linkage to non-C++declarations can be achieved using a
|
| 1858 |
-
*linkage-specification* ([[dcl.link]]).
|
| 1859 |
|
| 1860 |
## Start and termination <a id="basic.start">[[basic.start]]</a>
|
| 1861 |
|
| 1862 |
-
###
|
| 1863 |
|
| 1864 |
-
A program shall contain a global function called `main`
|
| 1865 |
-
|
| 1866 |
-
|
| 1867 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1868 |
*implementation-defined*; start-up contains the execution of
|
| 1869 |
constructors for objects of namespace scope with static storage
|
| 1870 |
duration; termination contains the execution of destructors for objects
|
| 1871 |
-
with static storage duration.
|
| 1872 |
|
| 1873 |
An implementation shall not predefine the `main` function. This function
|
| 1874 |
-
shall not be overloaded.
|
| 1875 |
-
`int`, but otherwise its type
|
| 1876 |
-
implementation shall allow both
|
| 1877 |
|
| 1878 |
- a function of `()` returning `int` and
|
| 1879 |
- a function of `(int`, pointer to pointer to `char)` returning `int`
|
| 1880 |
|
| 1881 |
as the type of `main` ([[dcl.fct]]). In the latter form, for purposes
|
|
@@ -1886,107 +2173,85 @@ the program is run. If `argc` is nonzero these arguments shall be
|
|
| 1886 |
supplied in `argv[0]` through `argv[argc-1]` as pointers to the initial
|
| 1887 |
characters of null-terminated multibyte strings (NTMBS s) (
|
| 1888 |
[[multibyte.strings]]) and `argv[0]` shall be the pointer to the initial
|
| 1889 |
character of a NTMBSthat represents the name used to invoke the program
|
| 1890 |
or `""`. The value of `argc` shall be non-negative. The value of
|
| 1891 |
-
`argv[argc]` shall be 0.
|
| 1892 |
-
|
|
|
|
|
|
|
| 1893 |
|
| 1894 |
The function `main` shall not be used within a program. The linkage (
|
| 1895 |
[[basic.link]]) of `main` is *implementation-defined*. A program that
|
| 1896 |
defines `main` as deleted or that declares `main` to be `inline`,
|
| 1897 |
-
`static`, or `constexpr` is ill-formed. The
|
| 1898 |
-
|
| 1899 |
-
`main`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1900 |
|
| 1901 |
Terminating the program without leaving the current block (e.g., by
|
| 1902 |
calling the function `std::exit(int)` ([[support.start.term]])) does
|
| 1903 |
not destroy any objects with automatic storage duration (
|
| 1904 |
[[class.dtor]]). If `std::exit` is called to end a program during the
|
| 1905 |
destruction of an object with static or thread storage duration, the
|
| 1906 |
program has undefined behavior.
|
| 1907 |
|
| 1908 |
A return statement in `main` has the effect of leaving the main function
|
| 1909 |
(destroying any objects with automatic storage duration) and calling
|
| 1910 |
-
`std::exit` with the return value as the argument. If control
|
| 1911 |
-
the end of
|
| 1912 |
-
|
| 1913 |
|
| 1914 |
-
|
| 1915 |
-
return 0;
|
| 1916 |
-
```
|
| 1917 |
|
| 1918 |
-
|
| 1919 |
-
|
| 1920 |
-
There are two broad classes of named non-local variables: those with
|
| 1921 |
-
static storage duration ([[basic.stc.static]]) and those with thread
|
| 1922 |
-
storage duration ([[basic.stc.thread]]). Non-local variables with
|
| 1923 |
-
static storage duration are initialized as a consequence of program
|
| 1924 |
-
initiation. Non-local variables with thread storage duration are
|
| 1925 |
initialized as a consequence of thread execution. Within each of these
|
| 1926 |
phases of initiation, initialization occurs as follows.
|
| 1927 |
|
| 1928 |
-
|
| 1929 |
-
|
| 1930 |
-
|
| 1931 |
-
|
| 1932 |
-
|
| 1933 |
-
`o` and its subobjects even if those objects are of non-literal class
|
| 1934 |
-
types such a class may have a non-trivial destructor . *Constant
|
| 1935 |
-
initialization* is performed:
|
| 1936 |
|
| 1937 |
-
|
| 1938 |
-
|
| 1939 |
-
duration is a constant expression ([[expr.const]]) and the reference
|
| 1940 |
-
is bound to an lvalue designating an object with static storage
|
| 1941 |
-
duration, to a temporary (see [[class.temporary]]), or to a function;
|
| 1942 |
-
- if an object with static or thread storage duration is initialized by
|
| 1943 |
-
a constructor call, and if the initialization full-expression is a
|
| 1944 |
-
constant initializer for the object;
|
| 1945 |
-
- if an object with static or thread storage duration is not initialized
|
| 1946 |
-
by a constructor call and if either the object is value-initialized or
|
| 1947 |
-
every full-expression that appears in its initializer is a constant
|
| 1948 |
-
expression.
|
| 1949 |
|
| 1950 |
-
|
| 1951 |
-
|
| 1952 |
-
|
| 1953 |
-
|
| 1954 |
-
|
| 1955 |
-
|
| 1956 |
-
|
| 1957 |
-
|
| 1958 |
-
|
| 1959 |
-
|
| 1960 |
-
|
| 1961 |
-
|
| 1962 |
-
|
| 1963 |
-
[[thread.threads]]), the subsequent initialization of a variable is
|
| 1964 |
-
unsequenced with respect to the initialization of a variable defined in
|
| 1965 |
-
a different translation unit. Otherwise, the initialization of a
|
| 1966 |
-
variable is indeterminately sequenced with respect to the initialization
|
| 1967 |
-
of a variable defined in a different translation unit. If a program
|
| 1968 |
-
starts a thread, the subsequent unordered initialization of a variable
|
| 1969 |
-
is unsequenced with respect to every other dynamic initialization.
|
| 1970 |
-
Otherwise, the unordered initialization of a variable is indeterminately
|
| 1971 |
-
sequenced with respect to every other dynamic initialization. This
|
| 1972 |
-
definition permits initialization of a sequence of ordered variables
|
| 1973 |
-
concurrently with another sequence. The initialization of local static
|
| 1974 |
-
variables is described in [[stmt.dcl]].
|
| 1975 |
|
| 1976 |
An implementation is permitted to perform the initialization of a
|
| 1977 |
-
|
| 1978 |
initialization even if such initialization is not required to be done
|
| 1979 |
statically, provided that
|
| 1980 |
|
| 1981 |
- the dynamic version of the initialization does not change the value of
|
| 1982 |
-
any other object of
|
|
|
|
| 1983 |
- the static version of the initialization produces the same value in
|
| 1984 |
the initialized variable as would be produced by the dynamic
|
| 1985 |
initialization if all variables not required to be initialized
|
| 1986 |
statically were initialized dynamically.
|
| 1987 |
|
|
|
|
|
|
|
| 1988 |
As a consequence, if the initialization of an object `obj1` refers to an
|
| 1989 |
object `obj2` of namespace scope potentially requiring dynamic
|
| 1990 |
initialization and defined later in the same translation unit, it is
|
| 1991 |
unspecified whether the value of `obj2` used will be the value of the
|
| 1992 |
fully initialized `obj2` (because `obj2` was statically initialized) or
|
|
@@ -2000,16 +2265,65 @@ double d2 = d1; // unspecified:
|
|
| 2000 |
// dynamically initialized to 0.0 if d1 is
|
| 2001 |
// dynamically initialized, or 1.0 otherwise
|
| 2002 |
double d1 = fd(); // may be initialized statically or dynamically to 1.0
|
| 2003 |
```
|
| 2004 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2005 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 2006 |
-
non-local variable with static storage duration is
|
| 2007 |
-
statement of `main`
|
| 2008 |
-
|
| 2009 |
-
|
| 2010 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2011 |
|
| 2012 |
``` cpp
|
| 2013 |
// - File 1 -
|
| 2014 |
#include "a.h"
|
| 2015 |
#include "b.h"
|
|
@@ -2032,133 +2346,162 @@ int main() {
|
|
| 2032 |
a.Use();
|
| 2033 |
b.Use();
|
| 2034 |
}
|
| 2035 |
```
|
| 2036 |
|
| 2037 |
-
It is implementation-defined whether either `a` or `b` is initialized
|
| 2038 |
before `main` is entered or whether the initializations are delayed
|
| 2039 |
until `a` is first odr-used in `main`. In particular, if `a` is
|
| 2040 |
initialized before `main` is entered, it is not guaranteed that `b` will
|
| 2041 |
be initialized before it is odr-used by the initialization of `a`, that
|
| 2042 |
is, before `A::A` is called. If, however, `a` is initialized at some
|
| 2043 |
point after the first statement of `main`, `b` will be initialized prior
|
| 2044 |
to its use in `A::A`.
|
| 2045 |
|
|
|
|
|
|
|
| 2046 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 2047 |
-
non-local variable with static
|
| 2048 |
-
the first statement of
|
| 2049 |
-
|
| 2050 |
-
|
| 2051 |
-
|
| 2052 |
-
|
| 2053 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2054 |
|
| 2055 |
If the initialization of a non-local variable with static or thread
|
| 2056 |
storage duration exits via an exception, `std::terminate` is called (
|
| 2057 |
[[except.terminate]]).
|
| 2058 |
|
| 2059 |
### Termination <a id="basic.start.term">[[basic.start.term]]</a>
|
| 2060 |
|
| 2061 |
Destructors ([[class.dtor]]) for initialized objects (that is, objects
|
| 2062 |
-
whose lifetime ([[basic.life]]) has begun) with static storage
|
| 2063 |
-
|
| 2064 |
-
|
| 2065 |
-
|
| 2066 |
-
|
| 2067 |
-
|
| 2068 |
-
|
| 2069 |
-
|
| 2070 |
-
|
| 2071 |
-
|
| 2072 |
-
|
| 2073 |
-
|
| 2074 |
-
|
| 2075 |
-
|
| 2076 |
-
|
| 2077 |
-
|
| 2078 |
-
|
| 2079 |
-
If
|
| 2080 |
-
|
| 2081 |
-
|
| 2082 |
-
before
|
| 2083 |
-
|
| 2084 |
-
|
| 2085 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2086 |
|
| 2087 |
If a function contains a block-scope object of static or thread storage
|
| 2088 |
duration that has been destroyed and the function is called during the
|
| 2089 |
destruction of an object with static or thread storage duration, the
|
| 2090 |
program has undefined behavior if the flow of control passes through the
|
| 2091 |
definition of the previously destroyed block-scope object. Likewise, the
|
| 2092 |
behavior is undefined if the block-scope object is used indirectly
|
| 2093 |
(i.e., through a pointer) after its destruction.
|
| 2094 |
|
| 2095 |
If the completion of the initialization of an object with static storage
|
| 2096 |
-
duration
|
| 2097 |
-
[[support.start.term]]), the call to the function passed
|
| 2098 |
-
`std::atexit` is sequenced before the call to the destructor for the
|
| 2099 |
-
object. If a call to `std::atexit`
|
| 2100 |
-
the initialization of an object with static storage
|
| 2101 |
-
to the destructor for the object is sequenced before
|
| 2102 |
-
function passed to `std::atexit`. If a call to
|
| 2103 |
-
|
| 2104 |
-
passed to the second `std::atexit` call is
|
| 2105 |
-
the function passed to the first
|
|
|
|
| 2106 |
|
| 2107 |
If there is a use of a standard library object or function not permitted
|
| 2108 |
within signal handlers ([[support.runtime]]) that does not happen
|
| 2109 |
before ([[intro.multithread]]) completion of destruction of objects
|
| 2110 |
with static storage duration and execution of `std::atexit` registered
|
| 2111 |
functions ([[support.start.term]]), the program has undefined behavior.
|
| 2112 |
-
|
| 2113 |
-
|
| 2114 |
-
|
| 2115 |
-
|
| 2116 |
-
|
| 2117 |
-
|
|
|
|
| 2118 |
|
| 2119 |
Calling the function `std::abort()` declared in `<cstdlib>` terminates
|
| 2120 |
the program without executing any destructors and without calling the
|
| 2121 |
functions passed to `std::atexit()` or `std::at_quick_exit()`.
|
| 2122 |
|
| 2123 |
## Storage duration <a id="basic.stc">[[basic.stc]]</a>
|
| 2124 |
|
| 2125 |
-
|
| 2126 |
-
potential lifetime of the storage containing the object. The
|
| 2127 |
-
duration is determined by the construct used to create the
|
| 2128 |
-
one of the following:
|
| 2129 |
|
| 2130 |
- static storage duration
|
| 2131 |
- thread storage duration
|
| 2132 |
- automatic storage duration
|
| 2133 |
- dynamic storage duration
|
| 2134 |
|
| 2135 |
Static, thread, and automatic storage durations are associated with
|
| 2136 |
objects introduced by declarations ([[basic.def]]) and implicitly
|
| 2137 |
created by the implementation ([[class.temporary]]). The dynamic
|
| 2138 |
-
storage duration is associated with objects created
|
| 2139 |
-
|
| 2140 |
|
| 2141 |
-
The storage duration categories apply to references as well.
|
| 2142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2143 |
|
| 2144 |
### Static storage duration <a id="basic.stc.static">[[basic.stc.static]]</a>
|
| 2145 |
|
| 2146 |
All variables which do not have dynamic storage duration, do not have
|
| 2147 |
thread storage duration, and are not local have *static storage
|
| 2148 |
duration*. The storage for these entities shall last for the duration of
|
| 2149 |
-
the program ([[basic.start.
|
| 2150 |
|
| 2151 |
If a variable with static storage duration has initialization or a
|
| 2152 |
destructor with side effects, it shall not be eliminated even if it
|
| 2153 |
appears to be unused, except that a class object or its copy/move may be
|
| 2154 |
eliminated as specified in [[class.copy]].
|
| 2155 |
|
| 2156 |
The keyword `static` can be used to declare a local variable with static
|
| 2157 |
-
storage duration.
|
| 2158 |
-
|
| 2159 |
-
local `static`
|
|
|
|
|
|
|
| 2160 |
|
| 2161 |
The keyword `static` applied to a class data member in a class
|
| 2162 |
definition gives the data member static storage duration.
|
| 2163 |
|
| 2164 |
### Thread storage duration <a id="basic.stc.thread">[[basic.stc.thread]]</a>
|
|
@@ -2173,23 +2516,22 @@ A variable with thread storage duration shall be initialized before its
|
|
| 2173 |
first odr-use ([[basic.def.odr]]) and, if constructed, shall be
|
| 2174 |
destroyed on thread exit.
|
| 2175 |
|
| 2176 |
### Automatic storage duration <a id="basic.stc.auto">[[basic.stc.auto]]</a>
|
| 2177 |
|
| 2178 |
-
Block-scope variables explicitly declared `
|
| 2179 |
-
|
| 2180 |
-
|
| 2181 |
-
created exits.
|
| 2182 |
|
| 2183 |
-
These variables are initialized and destroyed as described
|
| 2184 |
-
[[stmt.dcl]].
|
| 2185 |
|
| 2186 |
If a variable with automatic storage duration has initialization or a
|
| 2187 |
-
destructor with side effects,
|
| 2188 |
-
of its block
|
| 2189 |
-
appears to be unused, except that a class object or its copy/move may
|
| 2190 |
-
eliminated as specified in [[class.copy]].
|
| 2191 |
|
| 2192 |
### Dynamic storage duration <a id="basic.stc.dynamic">[[basic.stc.dynamic]]</a>
|
| 2193 |
|
| 2194 |
Objects can be created dynamically during program execution (
|
| 2195 |
[[intro.execution]]), using *new-expression*s ([[expr.new]]), and
|
|
@@ -2197,10 +2539,14 @@ destroyed using *delete-expression*s ([[expr.delete]]). A
|
|
| 2197 |
C++implementation provides access to, and management of, dynamic storage
|
| 2198 |
via the global *allocation functions* `operator new` and `operator
|
| 2199 |
new[]` and the global *deallocation functions* `operator
|
| 2200 |
delete` and `operator delete[]`.
|
| 2201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2202 |
The library provides default definitions for the global allocation and
|
| 2203 |
deallocation functions. Some global allocation and deallocation
|
| 2204 |
functions are replaceable ([[new.delete]]). A C++program shall provide
|
| 2205 |
at most one definition of a replaceable allocation or deallocation
|
| 2206 |
function. Any such function definition replaces the default version
|
|
@@ -2209,27 +2555,41 @@ allocation and deallocation functions ([[support.dynamic]]) are
|
|
| 2209 |
implicitly declared in global scope in each translation unit of a
|
| 2210 |
program.
|
| 2211 |
|
| 2212 |
``` cpp
|
| 2213 |
void* operator new(std::size_t);
|
| 2214 |
-
void* operator new
|
|
|
|
| 2215 |
void operator delete(void*) noexcept;
|
| 2216 |
-
void operator delete[](void*) noexcept;
|
| 2217 |
void operator delete(void*, std::size_t) noexcept;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2218 |
void operator delete[](void*, std::size_t) noexcept;
|
|
|
|
|
|
|
| 2219 |
```
|
| 2220 |
|
| 2221 |
These implicit declarations introduce only the function names `operator`
|
| 2222 |
`new`, `operator` `new[]`, `operator` `delete`, and `operator`
|
| 2223 |
-
`delete[]`.
|
| 2224 |
-
|
| 2225 |
-
|
| 2226 |
-
|
| 2227 |
-
|
| 2228 |
-
|
| 2229 |
-
|
| 2230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2231 |
|
| 2232 |
Any allocation and/or deallocation functions defined in a C++program,
|
| 2233 |
including the default versions in the library, shall conform to the
|
| 2234 |
semantics specified in [[basic.stc.dynamic.allocation]] and
|
| 2235 |
[[basic.stc.dynamic.deallocation]].
|
|
@@ -2254,102 +2614,91 @@ storage. If it is successful, it shall return the address of the start
|
|
| 2254 |
of a block of storage whose length in bytes shall be at least as large
|
| 2255 |
as the requested size. There are no constraints on the contents of the
|
| 2256 |
allocated storage on return from the allocation function. The order,
|
| 2257 |
contiguity, and initial value of storage allocated by successive calls
|
| 2258 |
to an allocation function are unspecified. The pointer returned shall be
|
| 2259 |
-
suitably aligned so that it can be converted to a pointer
|
| 2260 |
-
complete object type
|
| 2261 |
-
|
| 2262 |
-
|
| 2263 |
-
|
| 2264 |
-
|
| 2265 |
-
|
| 2266 |
-
|
| 2267 |
-
|
| 2268 |
-
|
| 2269 |
-
|
|
|
|
|
|
|
|
|
|
| 2270 |
|
| 2271 |
An allocation function that fails to allocate storage can invoke the
|
| 2272 |
-
currently installed new-handler function ([[new.handler]]), if any.
|
| 2273 |
-
|
| 2274 |
-
|
| 2275 |
-
|
| 2276 |
-
|
| 2277 |
-
|
| 2278 |
-
|
| 2279 |
-
|
| 2280 |
-
|
|
|
|
|
|
|
|
|
|
| 2281 |
|
| 2282 |
A global allocation function is only called as the result of a new
|
| 2283 |
expression ([[expr.new]]), or called directly using the function call
|
| 2284 |
syntax ([[expr.call]]), or called indirectly through calls to the
|
| 2285 |
-
functions in the C++standard library.
|
| 2286 |
-
|
| 2287 |
-
|
| 2288 |
-
|
| 2289 |
-
|
| 2290 |
-
[[
|
|
|
|
|
|
|
| 2291 |
|
| 2292 |
#### Deallocation functions <a id="basic.stc.dynamic.deallocation">[[basic.stc.dynamic.deallocation]]</a>
|
| 2293 |
|
| 2294 |
Deallocation functions shall be class member functions or global
|
| 2295 |
functions; a program is ill-formed if deallocation functions are
|
| 2296 |
declared in a namespace scope other than global scope or declared static
|
| 2297 |
in global scope.
|
| 2298 |
|
| 2299 |
Each deallocation function shall return `void` and its first parameter
|
| 2300 |
-
shall be `void*`. A deallocation function
|
| 2301 |
-
parameter.
|
| 2302 |
-
|
| 2303 |
-
|
| 2304 |
-
|
| 2305 |
-
|
| 2306 |
-
|
| 2307 |
-
parameters, the
|
| 2308 |
-
|
| 2309 |
-
|
| 2310 |
-
|
| 2311 |
-
|
| 2312 |
-
|
| 2313 |
-
|
| 2314 |
-
|
| 2315 |
-
deallocation function named `operator` `delete[]` with exactly one
|
| 2316 |
-
parameter, then that function is a usual (non-placement) deallocation
|
| 2317 |
-
function. If class `T` does not declare such an `operator` `delete[]`
|
| 2318 |
-
but does declare a member deallocation function named `operator`
|
| 2319 |
-
`delete[]` with exactly two parameters, the second of which has type
|
| 2320 |
-
`std::size_t`, then this function is a usual deallocation function. A
|
| 2321 |
-
deallocation function can be an instance of a function template. Neither
|
| 2322 |
-
the first parameter nor the return type shall depend on a template
|
| 2323 |
-
parameter. That is, a deallocation function template shall have a first
|
| 2324 |
parameter of type `void*` and a return type of `void` (as specified
|
| 2325 |
-
above).
|
|
|
|
|
|
|
| 2326 |
parameters. A template instance is never a usual deallocation function,
|
| 2327 |
regardless of its signature.
|
| 2328 |
|
| 2329 |
If a deallocation function terminates by throwing an exception, the
|
| 2330 |
behavior is undefined. The value of the first argument supplied to a
|
| 2331 |
deallocation function may be a null pointer value; if so, and if the
|
| 2332 |
deallocation function is one supplied in the standard library, the call
|
| 2333 |
-
has no effect.
|
| 2334 |
-
supplied to `operator` `delete(void*)` in the standard library is not
|
| 2335 |
-
one of the values returned by a previous invocation of either `operator`
|
| 2336 |
-
`new(std::size_t)` or `operator` `new(std::size_t,` `const`
|
| 2337 |
-
`std::nothrow_t&)` in the standard library, and the behavior is
|
| 2338 |
-
undefined if the value supplied to `operator` `delete[](void*)` in the
|
| 2339 |
-
standard library is not one of the values returned by a previous
|
| 2340 |
-
invocation of either `operator` `new[](std::size_t)` or `operator`
|
| 2341 |
-
`new[](std::size_t,` `const` `std::nothrow_t&)` in the standard library.
|
| 2342 |
|
| 2343 |
If the argument given to a deallocation function in the standard library
|
| 2344 |
is a pointer that is not the null pointer value ([[conv.ptr]]), the
|
| 2345 |
deallocation function shall deallocate the storage referenced by the
|
| 2346 |
-
pointer,
|
| 2347 |
-
deallocated storage. Indirection through an invalid pointer value and
|
| 2348 |
-
passing an invalid pointer value to a deallocation function have
|
| 2349 |
-
undefined behavior. Any other use of an invalid pointer value has
|
| 2350 |
-
implementation-defined behavior.[^14]
|
| 2351 |
|
| 2352 |
#### Safely-derived pointers <a id="basic.stc.dynamic.safety">[[basic.stc.dynamic.safety]]</a>
|
| 2353 |
|
| 2354 |
A *traceable pointer object* is
|
| 2355 |
|
|
@@ -2362,11 +2711,12 @@ A *traceable pointer object* is
|
|
| 2362 |
|
| 2363 |
A pointer value is a *safely-derived pointer* to a dynamic object only
|
| 2364 |
if it has an object pointer type and it is one of the following:
|
| 2365 |
|
| 2366 |
- the value returned by a call to the C++standard library implementation
|
| 2367 |
-
of `::operator new(std::size_t)`
|
|
|
|
| 2368 |
- the result of taking the address of an object (or one of its
|
| 2369 |
subobjects) designated by an lvalue resulting from indirection through
|
| 2370 |
a safely-derived pointer value;
|
| 2371 |
- the result of well-defined pointer arithmetic ([[expr.add]]) using a
|
| 2372 |
safely-derived pointer value;
|
|
@@ -2377,13 +2727,13 @@ if it has an object pointer type and it is one of the following:
|
|
| 2377 |
safely-derived pointer value;
|
| 2378 |
- the value of an object whose value was copied from a traceable pointer
|
| 2379 |
object, where at the time of the copy the source object contained a
|
| 2380 |
copy of a safely-derived pointer value.
|
| 2381 |
|
| 2382 |
-
An integer value is an
|
| 2383 |
-
|
| 2384 |
-
|
| 2385 |
|
| 2386 |
- the result of a `reinterpret_cast` of a safely-derived pointer value;
|
| 2387 |
- the result of a valid conversion of an integer representation of a
|
| 2388 |
safely-derived pointer value;
|
| 2389 |
- the value of an object whose value was copied from a traceable pointer
|
|
@@ -2400,57 +2750,72 @@ validity of a pointer value does not depend on whether it is a
|
|
| 2400 |
safely-derived pointer value. Alternatively, an implementation may have
|
| 2401 |
*strict pointer safety*, in which case a pointer value referring to an
|
| 2402 |
object with dynamic storage duration that is not a safely-derived
|
| 2403 |
pointer value is an invalid pointer value unless the referenced complete
|
| 2404 |
object has previously been declared reachable (
|
| 2405 |
-
[[util.dynamic.safety]]).
|
| 2406 |
-
|
|
|
|
|
|
|
| 2407 |
[[basic.stc.dynamic.deallocation]]. This is true even if the
|
| 2408 |
unsafely-derived pointer value might compare equal to some
|
| 2409 |
-
safely-derived pointer value.
|
| 2410 |
-
|
|
|
|
|
|
|
| 2411 |
|
| 2412 |
### Duration of subobjects <a id="basic.stc.inherit">[[basic.stc.inherit]]</a>
|
| 2413 |
|
| 2414 |
-
The storage duration of
|
| 2415 |
-
|
| 2416 |
|
| 2417 |
## Object lifetime <a id="basic.life">[[basic.life]]</a>
|
| 2418 |
|
| 2419 |
-
The *lifetime* of an object is a runtime property of the
|
| 2420 |
-
object is said to have non-
|
| 2421 |
-
aggregate type and it or one of
|
| 2422 |
-
|
| 2423 |
-
|
| 2424 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2425 |
|
| 2426 |
- storage with the proper alignment and size for type `T` is obtained,
|
| 2427 |
and
|
| 2428 |
-
- if the object has non-
|
| 2429 |
-
complete
|
| 2430 |
|
| 2431 |
-
|
|
|
|
|
|
|
|
|
|
| 2432 |
|
| 2433 |
- if `T` is a class type with a non-trivial destructor (
|
| 2434 |
[[class.dtor]]), the destructor call starts, or
|
| 2435 |
-
- the storage which the object occupies is
|
| 2436 |
-
|
| 2437 |
-
|
| 2438 |
-
|
| 2439 |
-
|
| 2440 |
-
|
| 2441 |
-
|
| 2442 |
-
|
| 2443 |
-
|
| 2444 |
-
|
| 2445 |
-
|
| 2446 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2447 |
[[class.cdtor]]. Also, the behavior of an object under construction and
|
| 2448 |
destruction might not be the same as the behavior of an object whose
|
| 2449 |
lifetime has started and not ended. [[class.base.init]] and
|
| 2450 |
[[class.cdtor]] describe the behavior of objects during the construction
|
| 2451 |
-
and destruction phases.
|
| 2452 |
|
| 2453 |
A program may end the lifetime of any object by reusing the storage
|
| 2454 |
which the object occupies or by explicitly calling the destructor for an
|
| 2455 |
object of a class type with a non-trivial destructor. For an object of a
|
| 2456 |
class type with a non-trivial destructor, the program is not required to
|
|
@@ -2462,32 +2827,37 @@ called and any program that depends on the side effects produced by the
|
|
| 2462 |
destructor has undefined behavior.
|
| 2463 |
|
| 2464 |
Before the lifetime of an object has started but after the storage which
|
| 2465 |
the object will occupy has been allocated[^16] or, after the lifetime of
|
| 2466 |
an object has ended and before the storage which the object occupied is
|
| 2467 |
-
reused or released, any pointer that
|
| 2468 |
-
where the object will be or was located may be used but
|
| 2469 |
-
ways. For an object under construction or destruction,
|
| 2470 |
-
[[class.cdtor]]. Otherwise, such a pointer refers to allocated
|
| 2471 |
-
[[basic.stc.dynamic.deallocation]]), and using the pointer as
|
| 2472 |
-
pointer were of type `void*`, is well-defined. Indirection
|
| 2473 |
-
a pointer is permitted but the resulting lvalue may only be
|
| 2474 |
-
limited ways, as described below. The program has undefined
|
|
|
|
| 2475 |
|
| 2476 |
- the object will be or was of a class type with a non-trivial
|
| 2477 |
destructor and the pointer is used as the operand of a
|
| 2478 |
*delete-expression*,
|
| 2479 |
- the pointer is used to access a non-static data member or call a
|
| 2480 |
non-static member function of the object, or
|
| 2481 |
- the pointer is implicitly converted ([[conv.ptr]]) to a pointer to a
|
| 2482 |
virtual base class, or
|
| 2483 |
- the pointer is used as the operand of a `static_cast` (
|
| 2484 |
[[expr.static.cast]]), except when the conversion is to pointer to
|
| 2485 |
-
|
| 2486 |
-
|
|
|
|
| 2487 |
- the pointer is used as the operand of a `dynamic_cast` (
|
| 2488 |
[[expr.dynamic.cast]]).
|
|
|
|
|
|
|
|
|
|
| 2489 |
``` cpp
|
| 2490 |
#include <cstdlib>
|
| 2491 |
|
| 2492 |
struct B {
|
| 2493 |
virtual void f();
|
|
@@ -2506,16 +2876,18 @@ limited ways, as described below. The program has undefined behavior if:
|
|
| 2506 |
|
| 2507 |
void g() {
|
| 2508 |
void* p = std::malloc(sizeof(D1) + sizeof(D2));
|
| 2509 |
B* pb = new (p) D1;
|
| 2510 |
pb->mutate();
|
| 2511 |
-
|
| 2512 |
void* q = pb; // OK: pb points to valid memory
|
| 2513 |
pb->f(); // undefined behavior, lifetime of *pb has ended
|
| 2514 |
}
|
| 2515 |
```
|
| 2516 |
|
|
|
|
|
|
|
| 2517 |
Similarly, before the lifetime of an object has started but after the
|
| 2518 |
storage which the object will occupy has been allocated or, after the
|
| 2519 |
lifetime of an object has ended and before the storage which the object
|
| 2520 |
occupied is reused or released, any glvalue that refers to the original
|
| 2521 |
object may be used but only in limited ways. For an object under
|
|
@@ -2523,14 +2895,13 @@ construction or destruction, see [[class.cdtor]]. Otherwise, such a
|
|
| 2523 |
glvalue refers to allocated storage (
|
| 2524 |
[[basic.stc.dynamic.deallocation]]), and using the properties of the
|
| 2525 |
glvalue that do not depend on its value is well-defined. The program has
|
| 2526 |
undefined behavior if:
|
| 2527 |
|
| 2528 |
-
-
|
| 2529 |
-
|
| 2530 |
-
|
| 2531 |
-
non-static member function of the object, or
|
| 2532 |
- the glvalue is bound to a reference to a virtual base class (
|
| 2533 |
[[dcl.init.ref]]), or
|
| 2534 |
- the glvalue is used as the operand of a `dynamic_cast` (
|
| 2535 |
[[expr.dynamic.cast]]) or as the operand of `typeid`.
|
| 2536 |
|
|
@@ -2550,10 +2921,13 @@ started, can be used to manipulate the new object, if:
|
|
| 2550 |
class type, does not contain any non-static data member whose type is
|
| 2551 |
const-qualified or a reference type, and
|
| 2552 |
- the original object was a most derived object ([[intro.object]]) of
|
| 2553 |
type `T` and the new object is a most derived object of type `T` (that
|
| 2554 |
is, they are not base class subobjects).
|
|
|
|
|
|
|
|
|
|
| 2555 |
``` cpp
|
| 2556 |
struct C {
|
| 2557 |
int i;
|
| 2558 |
void f();
|
| 2559 |
const C& operator=( const C& );
|
|
@@ -2572,18 +2946,26 @@ started, can be used to manipulate the new object, if:
|
|
| 2572 |
C c2;
|
| 2573 |
c1 = c2; // well-defined
|
| 2574 |
c1.f(); // well-defined; c1 refers to a new object of type C
|
| 2575 |
```
|
| 2576 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2577 |
If a program ends the lifetime of an object of type `T` with static (
|
| 2578 |
[[basic.stc.static]]), thread ([[basic.stc.thread]]), or automatic (
|
| 2579 |
[[basic.stc.auto]]) storage duration and if `T` has a non-trivial
|
| 2580 |
destructor,[^17] the program must ensure that an object of the original
|
| 2581 |
type occupies that same storage location when the implicit destructor
|
| 2582 |
call takes place; otherwise the behavior of the program is undefined.
|
| 2583 |
This is true even if the block is exited with an exception.
|
| 2584 |
|
|
|
|
|
|
|
| 2585 |
``` cpp
|
| 2586 |
class T { };
|
| 2587 |
struct B {
|
| 2588 |
~B();
|
| 2589 |
};
|
|
@@ -2592,14 +2974,18 @@ void h() {
|
|
| 2592 |
B b;
|
| 2593 |
new (&b) T;
|
| 2594 |
} // undefined behavior at block exit
|
| 2595 |
```
|
| 2596 |
|
| 2597 |
-
|
| 2598 |
-
|
| 2599 |
-
|
| 2600 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2601 |
|
| 2602 |
``` cpp
|
| 2603 |
struct B {
|
| 2604 |
B();
|
| 2605 |
~B();
|
|
@@ -2611,84 +2997,96 @@ void h() {
|
|
| 2611 |
b.~B();
|
| 2612 |
new (const_cast<B*>(&b)) const B; // undefined behavior
|
| 2613 |
}
|
| 2614 |
```
|
| 2615 |
|
|
|
|
|
|
|
| 2616 |
In this section, “before” and “after” refer to the “happens before”
|
| 2617 |
-
relation ([[intro.multithread]]).
|
| 2618 |
-
|
| 2619 |
-
|
|
|
|
|
|
|
| 2620 |
|
| 2621 |
## Types <a id="basic.types">[[basic.types]]</a>
|
| 2622 |
|
| 2623 |
-
[[basic.types]] and the subclauses thereof impose
|
| 2624 |
-
implementations regarding the representation of types.
|
| 2625 |
-
kinds of types: fundamental types and compound types.
|
| 2626 |
-
objects ([[intro.object]]), references ([[dcl.ref]]),
|
| 2627 |
-
[[dcl.fct]]).
|
| 2628 |
|
| 2629 |
For any object (other than a base-class subobject) of trivially copyable
|
| 2630 |
type `T`, whether or not the object holds a valid value of type `T`, the
|
| 2631 |
underlying bytes ([[intro.memory]]) making up the object can be copied
|
| 2632 |
-
into an array of `char`
|
| 2633 |
-
|
| 2634 |
-
object shall subsequently hold its original value.
|
|
|
|
|
|
|
| 2635 |
|
| 2636 |
``` cpp
|
| 2637 |
#define N sizeof(T)
|
| 2638 |
char buf[N];
|
| 2639 |
T obj; // obj initialized to its original value
|
| 2640 |
-
std::memcpy(buf, &obj, N); // between these two calls to std::memcpy,
|
| 2641 |
-
|
| 2642 |
-
std::memcpy(&obj, buf, N); // at this point, each subobject of obj of scalar type
|
| 2643 |
-
// holds its original value
|
| 2644 |
```
|
| 2645 |
|
|
|
|
|
|
|
| 2646 |
For any trivially copyable type `T`, if two pointers to `T` point to
|
| 2647 |
distinct `T` objects `obj1` and `obj2`, where neither `obj1` nor `obj2`
|
| 2648 |
is a base-class subobject, if the underlying bytes ([[intro.memory]])
|
| 2649 |
making up `obj1` are copied into `obj2`,[^19] `obj2` shall subsequently
|
| 2650 |
hold the same value as `obj1`.
|
| 2651 |
|
|
|
|
|
|
|
| 2652 |
``` cpp
|
| 2653 |
T* t1p;
|
| 2654 |
T* t2p;
|
| 2655 |
// provided that t2p points to an initialized object ...
|
| 2656 |
std::memcpy(t1p, t2p, sizeof(T));
|
| 2657 |
// at this point, every subobject of trivially copyable type in *t1p contains
|
| 2658 |
// the same value as the corresponding subobject in *t2p
|
| 2659 |
```
|
| 2660 |
|
|
|
|
|
|
|
| 2661 |
The *object representation* of an object of type `T` is the sequence of
|
| 2662 |
-
*N* `unsigned
|
| 2663 |
*N* equals `sizeof(T)`. The *value representation* of an object is the
|
| 2664 |
set of bits that hold the value of type `T`. For trivially copyable
|
| 2665 |
types, the value representation is a set of bits in the object
|
| 2666 |
representation that determines a *value*, which is one discrete element
|
| 2667 |
of an *implementation-defined* set of values.[^20]
|
| 2668 |
|
| 2669 |
A class that has been declared but not defined, an enumeration type in
|
| 2670 |
-
certain contexts ([[dcl.enum]]), or an array of unknown
|
| 2671 |
incomplete element type, is an *incompletely-defined object type*. [^21]
|
| 2672 |
-
Incompletely-defined object types and
|
| 2673 |
-
|
| 2674 |
incomplete type.
|
| 2675 |
|
| 2676 |
A class type (such as “`class X`”) might be incomplete at one point in a
|
| 2677 |
translation unit and complete later on; the type “`class X`” is the same
|
| 2678 |
type at both points. The declared type of an array object might be an
|
| 2679 |
array of incomplete class type and therefore incomplete; if the class
|
| 2680 |
type is completed later on in the translation unit, the array type
|
| 2681 |
becomes complete; the array type at those two points is the same type.
|
| 2682 |
-
The declared type of an array object might be an array of unknown
|
| 2683 |
and therefore be incomplete at one point in a translation unit and
|
| 2684 |
complete later on; the array types at those two points (“array of
|
| 2685 |
unknown bound of `T`” and “array of `N` `T`”) are different types. The
|
| 2686 |
-
type of a pointer to array of unknown
|
| 2687 |
-
`typedef` declaration to be an array of unknown
|
| 2688 |
completed.
|
| 2689 |
|
|
|
|
|
|
|
| 2690 |
``` cpp
|
| 2691 |
class X; // X is an incomplete type
|
| 2692 |
extern X* xp; // xp is a pointer to an incomplete type
|
| 2693 |
extern int arr[]; // the type of arr is incomplete
|
| 2694 |
typedef int UNKA[]; // UNKA is an incomplete type
|
|
@@ -2711,52 +3109,62 @@ void bar() {
|
|
| 2711 |
xp++; // OK: X is complete
|
| 2712 |
arrp++; // ill-formed: UNKA can't be completed
|
| 2713 |
}
|
| 2714 |
```
|
| 2715 |
|
| 2716 |
-
|
| 2717 |
-
|
|
|
|
|
|
|
| 2718 |
|
| 2719 |
An *object type* is a (possibly cv-qualified) type that is not a
|
| 2720 |
-
function type, not a reference type, and not
|
| 2721 |
|
| 2722 |
Arithmetic types ([[basic.fundamental]]), enumeration types, pointer
|
| 2723 |
types, pointer to member types ([[basic.compound]]), `std::nullptr_t`,
|
| 2724 |
-
and cv-qualified
|
| 2725 |
collectively called *scalar types*. Scalar types, POD classes (Clause
|
| 2726 |
-
[[class]]), arrays of such types and
|
| 2727 |
-
types
|
| 2728 |
-
|
| 2729 |
-
|
| 2730 |
-
|
| 2731 |
-
|
| 2732 |
-
|
| 2733 |
-
|
| 2734 |
-
|
| 2735 |
-
|
| 2736 |
-
[[basic.type.qualifier]]) are collectively called *standard-layout
|
| 2737 |
-
types*.
|
| 2738 |
|
| 2739 |
A type is a *literal type* if it is:
|
| 2740 |
|
| 2741 |
-
- `void`; or
|
| 2742 |
- a scalar type; or
|
| 2743 |
- a reference type; or
|
| 2744 |
- an array of literal type; or
|
| 2745 |
-
- a class type (Clause [[class]]) that has all of
|
| 2746 |
-
properties:
|
| 2747 |
- it has a trivial destructor,
|
| 2748 |
-
- it is
|
| 2749 |
-
|
| 2750 |
-
or
|
| 2751 |
-
|
| 2752 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2753 |
|
| 2754 |
-
|
| 2755 |
-
|
| 2756 |
-
|
| 2757 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2758 |
|
| 2759 |
### Fundamental types <a id="basic.fundamental">[[basic.fundamental]]</a>
|
| 2760 |
|
| 2761 |
Objects declared as characters (`char`) shall be large enough to store
|
| 2762 |
any member of the implementation’s basic character set. If a character
|
|
@@ -2768,47 +3176,54 @@ declared `unsigned` or `signed`. Plain `char`, `signed char`, and
|
|
| 2768 |
`unsigned char` are three distinct types, collectively called *narrow
|
| 2769 |
character types*. A `char`, a `signed char`, and an `unsigned char`
|
| 2770 |
occupy the same amount of storage and have the same alignment
|
| 2771 |
requirements ([[basic.align]]); that is, they have the same object
|
| 2772 |
representation. For narrow character types, all bits of the object
|
| 2773 |
-
representation participate in the value representation.
|
| 2774 |
-
|
| 2775 |
-
|
| 2776 |
-
|
| 2777 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2778 |
char`; which one is *implementation-defined*. For each value *i* of type
|
| 2779 |
`unsigned char` in the range 0 to 255 inclusive, there exists a value
|
| 2780 |
*j* of type `char` such that the result of an integral conversion (
|
| 2781 |
[[conv.integral]]) from *i* to `char` is *j*, and the result of an
|
| 2782 |
integral conversion from *j* to `unsigned char` is *i*.
|
| 2783 |
|
| 2784 |
There are five *standard signed integer types* : “`signed char`”,
|
| 2785 |
-
“`short int`”, “`int`”, “`long int`”, and “`long
|
| 2786 |
list, each type provides at least as much storage as those preceding it
|
| 2787 |
in the list. There may also be *implementation-defined* *extended signed
|
| 2788 |
integer types*. The standard and extended signed integer types are
|
| 2789 |
collectively called *signed integer types*. Plain `int`s have the
|
| 2790 |
-
natural size suggested by the architecture of the execution
|
| 2791 |
-
|
| 2792 |
-
|
| 2793 |
|
| 2794 |
For each of the standard signed integer types, there exists a
|
| 2795 |
corresponding (but different) *standard unsigned integer type*:
|
| 2796 |
“`unsigned char`”, “`unsigned short int`”, “`unsigned int`”,
|
| 2797 |
-
“`unsigned long int`”, and “`unsigned
|
| 2798 |
-
|
| 2799 |
requirements ([[basic.align]]) as the corresponding signed integer
|
| 2800 |
type[^23]; that is, each signed integer type has the same object
|
| 2801 |
representation as its corresponding unsigned integer type. Likewise, for
|
| 2802 |
each of the extended signed integer types there exists a corresponding
|
| 2803 |
*extended unsigned integer type* with the same amount of storage and
|
| 2804 |
alignment requirements. The standard and extended unsigned integer types
|
| 2805 |
are collectively called *unsigned integer types*. The range of
|
| 2806 |
-
non-negative values of a
|
| 2807 |
-
corresponding
|
| 2808 |
-
each
|
| 2809 |
-
|
|
|
|
| 2810 |
collectively called the *standard integer types*, and the extended
|
| 2811 |
signed integer types and extended unsigned integer types are
|
| 2812 |
collectively called the *extended integer types*. The signed and
|
| 2813 |
unsigned integer types shall satisfy the constraints given in the C
|
| 2814 |
standard, section 5.2.4.2.1.
|
|
@@ -2824,63 +3239,76 @@ same size, signedness, and alignment requirements ([[basic.align]]) as
|
|
| 2824 |
one of the other integral types, called its *underlying type*. Types
|
| 2825 |
`char16_t` and `char32_t` denote distinct types with the same size,
|
| 2826 |
signedness, and alignment as `uint_least16_t` and `uint_least32_t`,
|
| 2827 |
respectively, in `<cstdint>`, called the underlying types.
|
| 2828 |
|
| 2829 |
-
Values of type `bool` are either `true` or `false`.[^25]
|
| 2830 |
-
|
| 2831 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2832 |
|
| 2833 |
Types `bool`, `char`, `char16_t`, `char32_t`, `wchar_t`, and the signed
|
| 2834 |
and unsigned integer types are collectively called *integral*
|
| 2835 |
types.[^26] A synonym for integral type is *integer type*. The
|
| 2836 |
representations of integral types shall define values by use of a pure
|
| 2837 |
-
binary numeration system.[^27]
|
| 2838 |
-
complement, 1’s complement and signed magnitude representations for
|
| 2839 |
-
integral types.
|
| 2840 |
|
| 2841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2842 |
`long double`. The type `double` provides at least as much precision as
|
| 2843 |
`float`, and the type `long double` provides at least as much precision
|
| 2844 |
as `double`. The set of values of the type `float` is a subset of the
|
| 2845 |
set of values of the type `double`; the set of values of the type
|
| 2846 |
-
`double` is a subset of the set of values of the type `long
|
| 2847 |
-
|
| 2848 |
-
*implementation-defined*.
|
| 2849 |
-
collectively called *arithmetic* types. Specializations of the standard
|
| 2850 |
-
template `std::numeric_limits` ([[support.limits]]) shall specify the
|
| 2851 |
-
maximum and minimum values of each arithmetic type for an
|
| 2852 |
-
implementation.
|
| 2853 |
|
| 2854 |
-
|
| 2855 |
-
|
| 2856 |
-
|
| 2857 |
-
|
| 2858 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2859 |
[[stmt.expr]]), as an operand of a comma expression ([[expr.comma]]),
|
| 2860 |
as a second or third operand of `?:` ([[expr.cond]]), as the operand of
|
| 2861 |
`typeid`, `noexcept`, or `decltype`, as the expression in a return
|
| 2862 |
-
statement ([[stmt.return]]) for a function with the return type
|
| 2863 |
-
or as the operand of an explicit conversion to type
|
|
|
|
| 2864 |
|
| 2865 |
A value of type `std::nullptr_t` is a null pointer constant (
|
| 2866 |
[[conv.ptr]]). Such values participate in the pointer and the pointer to
|
| 2867 |
member conversions ([[conv.ptr]], [[conv.mem]]).
|
| 2868 |
`sizeof(std::nullptr_t)` shall be equal to `sizeof(void*)`.
|
| 2869 |
|
| 2870 |
-
Even if the implementation defines two or more basic types
|
| 2871 |
-
same value representation, they are nevertheless different
|
|
|
|
| 2872 |
|
| 2873 |
### Compound types <a id="basic.compound">[[basic.compound]]</a>
|
| 2874 |
|
| 2875 |
Compound types can be constructed in the following ways:
|
| 2876 |
|
| 2877 |
- *arrays* of objects of a given type, [[dcl.array]];
|
| 2878 |
- *functions*, which have parameters of given types and return `void` or
|
| 2879 |
references or objects of a given type, [[dcl.fct]];
|
| 2880 |
-
- *pointers* to `void` or objects or functions (including static
|
| 2881 |
-
of classes) of a given type, [[dcl.ptr]];
|
| 2882 |
- *references* to objects or functions of a given type, [[dcl.ref]].
|
| 2883 |
There are two types of references:
|
| 2884 |
- *lvalue reference*
|
| 2885 |
- *rvalue reference*
|
| 2886 |
- *classes* containing a sequence of objects of various types (Clause
|
|
@@ -2891,50 +3319,88 @@ Compound types can be constructed in the following ways:
|
|
| 2891 |
- *unions*, which are classes capable of containing objects of different
|
| 2892 |
types at different times, [[class.union]];
|
| 2893 |
- *enumerations*, which comprise a set of named constant values. Each
|
| 2894 |
distinct enumeration constitutes a different *enumerated type*,
|
| 2895 |
[[dcl.enum]];
|
| 2896 |
-
- *pointers to non-static
|
| 2897 |
of a given type within objects of a given class, [[dcl.mptr]].
|
| 2898 |
|
| 2899 |
These methods of constructing types can be applied recursively;
|
| 2900 |
restrictions are mentioned in [[dcl.ptr]], [[dcl.array]], [[dcl.fct]],
|
| 2901 |
and [[dcl.ref]]. Constructing a type such that the number of bytes in
|
| 2902 |
its object representation exceeds the maximum value representable in the
|
| 2903 |
type `std::size_t` ([[support.types]]) is ill-formed.
|
| 2904 |
|
| 2905 |
-
The type of a pointer to `void` or a pointer to an object type is
|
| 2906 |
-
an *object pointer type*.
|
| 2907 |
-
|
|
|
|
|
|
|
|
|
|
| 2908 |
The type of a pointer that can designate a function is called a
|
| 2909 |
*function pointer type*. A pointer to objects of type `T` is referred to
|
| 2910 |
-
as a “pointer to `T`
|
| 2911 |
-
|
| 2912 |
-
|
| 2913 |
-
|
| 2914 |
-
to
|
| 2915 |
-
|
| 2916 |
-
|
| 2917 |
-
|
| 2918 |
-
|
| 2919 |
-
|
| 2920 |
-
|
| 2921 |
-
|
| 2922 |
-
|
| 2923 |
-
|
| 2924 |
-
|
| 2925 |
-
|
| 2926 |
-
|
| 2927 |
-
|
| 2928 |
-
|
| 2929 |
-
|
| 2930 |
-
|
| 2931 |
-
|
| 2932 |
-
|
| 2933 |
-
|
| 2934 |
-
|
| 2935 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2936 |
|
| 2937 |
A pointer to cv-qualified ([[basic.type.qualifier]]) or cv-unqualified
|
| 2938 |
`void` can be used to point to objects of unknown type. Such a pointer
|
| 2939 |
shall be able to hold any object pointer. An object of type cv `void*`
|
| 2940 |
shall have the same representation and alignment requirements as
|
|
@@ -2945,12 +3411,12 @@ cv `char*`.
|
|
| 2945 |
A type mentioned in [[basic.fundamental]] and [[basic.compound]] is a
|
| 2946 |
*cv-unqualified type*. Each type which is a cv-unqualified complete or
|
| 2947 |
incomplete object type or is `void` ([[basic.types]]) has three
|
| 2948 |
corresponding cv-qualified versions of its type: a *const-qualified*
|
| 2949 |
version, a *volatile-qualified* version, and a
|
| 2950 |
-
*const-volatile-qualified* version. The
|
| 2951 |
-
[[intro.object]]) includes the cv-
|
| 2952 |
*decl-specifier-seq* ([[dcl.spec]]), *declarator* (Clause
|
| 2953 |
[[dcl.decl]]), *type-id* ([[dcl.name]]), or *new-type-id* (
|
| 2954 |
[[expr.new]]) when the object is created.
|
| 2955 |
|
| 2956 |
- A *const object* is an object of type `const T` or a non-mutable
|
|
@@ -2962,16 +3428,16 @@ version, a *volatile-qualified* version, and a
|
|
| 2962 |
volatile object, or a non-mutable volatile subobject of a const
|
| 2963 |
object.
|
| 2964 |
|
| 2965 |
The cv-qualified or cv-unqualified versions of a type are distinct
|
| 2966 |
types; however, they shall have the same representation and alignment
|
| 2967 |
-
requirements ([[basic.align]]).[^
|
| 2968 |
|
| 2969 |
A compound type ([[basic.compound]]) is not cv-qualified by the
|
| 2970 |
cv-qualifiers (if any) of the types from which it is compounded. Any
|
| 2971 |
-
cv-qualifiers applied to an array type affect the array element type
|
| 2972 |
-
|
| 2973 |
|
| 2974 |
See [[dcl.fct]] and [[class.this]] regarding function types that have
|
| 2975 |
*cv-qualifier*s.
|
| 2976 |
|
| 2977 |
There is a partial ordering on cv-qualifiers, so that a type can be said
|
|
@@ -2988,110 +3454,136 @@ constitute this ordering.
|
|
| 2988 |
| no cv-qualifier | < | `const volatile` |
|
| 2989 |
| `const` | < | `const volatile` |
|
| 2990 |
| `volatile` | < | `const volatile` |
|
| 2991 |
|
| 2992 |
|
| 2993 |
-
In this International Standard, the notation
|
| 2994 |
-
|
| 2995 |
cv-qualifiers, i.e., one of {`const`}, {`volatile`}, {`const`,
|
| 2996 |
-
`volatile`}, or the empty set.
|
| 2997 |
-
|
| 2998 |
-
|
| 2999 |
-
|
| 3000 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3001 |
|
| 3002 |
``` cpp
|
| 3003 |
typedef char CA[5];
|
| 3004 |
typedef const char CC;
|
| 3005 |
CC arr1[5] = { 0 };
|
| 3006 |
const CA arr2 = { 0 };
|
| 3007 |
```
|
| 3008 |
|
| 3009 |
-
The type of both `arr1` and `arr2` is “array of 5 `const char`
|
| 3010 |
-
array type is considered to be
|
|
|
|
|
|
|
| 3011 |
|
| 3012 |
## Lvalues and rvalues <a id="basic.lval">[[basic.lval]]</a>
|
| 3013 |
|
| 3014 |
Expressions are categorized according to the taxonomy in Figure
|
| 3015 |
[[fig:categories]].
|
| 3016 |
|
| 3017 |
<a id="fig:categories"></a>
|
| 3018 |
|
| 3019 |
![Expression category taxonomy \[fig:categories\]](images/valuecategories.svg)
|
| 3020 |
|
| 3021 |
-
-
|
| 3022 |
-
|
| 3023 |
-
|
| 3024 |
-
|
| 3025 |
-
|
| 3026 |
-
|
| 3027 |
-
|
| 3028 |
-
|
| 3029 |
-
|
| 3030 |
-
|
| 3031 |
-
|
| 3032 |
-
|
| 3033 |
-
-
|
| 3034 |
-
|
| 3035 |
-
|
| 3036 |
-
|
| 3037 |
-
|
| 3038 |
-
|
| 3039 |
-
|
| 3040 |
-
|
| 3041 |
-
also a prvalue.
|
| 3042 |
|
| 3043 |
Every expression belongs to exactly one of the fundamental
|
| 3044 |
classifications in this taxonomy: lvalue, xvalue, or prvalue. This
|
| 3045 |
-
property of an expression is called its *value category*.
|
| 3046 |
-
|
| 3047 |
-
|
| 3048 |
-
|
| 3049 |
-
|
| 3050 |
-
|
| 3051 |
-
|
| 3052 |
-
|
| 3053 |
-
|
| 3054 |
-
|
| 3055 |
-
|
| 3056 |
-
|
| 3057 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3058 |
|
| 3059 |
The discussion of reference initialization in [[dcl.init.ref]] and of
|
| 3060 |
temporaries in [[class.temporary]] indicates the behavior of lvalues
|
| 3061 |
and rvalues in other significant contexts.
|
| 3062 |
|
| 3063 |
-
Unless otherwise indicated ([[expr.call]]),
|
| 3064 |
-
complete
|
| 3065 |
-
|
| 3066 |
-
cv-qualified types; other prvalues always have cv-unqualified types. See
|
| 3067 |
-
Clause [[expr]].
|
| 3068 |
|
| 3069 |
-
|
| 3070 |
-
|
| 3071 |
-
|
| 3072 |
-
object ([[class.mfct]]) can modify the object.
|
| 3073 |
|
| 3074 |
-
|
| 3075 |
-
|
| 3076 |
|
| 3077 |
-
|
| 3078 |
-
|
| 3079 |
-
|
| 3080 |
-
|
| 3081 |
-
The referent of a `const`-qualified expression shall not be modified
|
| 3082 |
-
(through that expression), except that if it is of class type and has a
|
| 3083 |
-
`mutable` component, that component can be modified ([[dcl.type.cv]]).
|
| 3084 |
-
|
| 3085 |
-
If an expression can be used to modify the object to which it refers,
|
| 3086 |
-
the expression is called *modifiable*. A program that attempts to modify
|
| 3087 |
-
an object through a nonmodifiable lvalue or rvalue expression is
|
| 3088 |
-
ill-formed.
|
| 3089 |
|
| 3090 |
If a program attempts to access the stored value of an object through a
|
| 3091 |
glvalue of other than one of the following types the behavior is
|
| 3092 |
-
undefined:[^
|
| 3093 |
|
| 3094 |
- the dynamic type of the object,
|
| 3095 |
- a cv-qualified version of the dynamic type of the object,
|
| 3096 |
- a type similar (as defined in [[conv.qual]]) to the dynamic type of
|
| 3097 |
the object,
|
|
@@ -3103,11 +3595,11 @@ undefined:[^30]
|
|
| 3103 |
types among its elements or non-static data members (including,
|
| 3104 |
recursively, an element or non-static data member of a subaggregate or
|
| 3105 |
contained union),
|
| 3106 |
- a type that is a (possibly cv-qualified) base class type of the
|
| 3107 |
dynamic type of the object,
|
| 3108 |
-
- a `char`
|
| 3109 |
|
| 3110 |
## Alignment <a id="basic.align">[[basic.align]]</a>
|
| 3111 |
|
| 3112 |
Object types have *alignment requirements* ([[basic.fundamental]],
|
| 3113 |
[[basic.compound]]) which place restrictions on the addresses at which
|
|
@@ -3123,30 +3615,40 @@ equal to the greatest alignment supported by the implementation in all
|
|
| 3123 |
contexts, which is equal to `alignof(std::max_align_t)` (
|
| 3124 |
[[support.types]]). The alignment required for a type might be different
|
| 3125 |
when it is used as the type of a complete object and when it is used as
|
| 3126 |
the type of a subobject.
|
| 3127 |
|
|
|
|
|
|
|
| 3128 |
``` cpp
|
| 3129 |
struct B { long double d; };
|
| 3130 |
-
struct D : virtual B { char c; }
|
| 3131 |
```
|
| 3132 |
|
| 3133 |
When `D` is the type of a complete object, it will have a subobject of
|
| 3134 |
type `B`, so it must be aligned appropriately for a `long double`. If
|
| 3135 |
`D` appears as a subobject of another object that also has `B` as a
|
| 3136 |
virtual base class, the `B` subobject might be part of a different
|
| 3137 |
-
subobject, reducing the alignment requirements on the `D` subobject.
|
| 3138 |
-
|
| 3139 |
-
|
|
|
|
|
|
|
|
|
|
| 3140 |
|
| 3141 |
An *extended alignment* is represented by an alignment greater than
|
| 3142 |
-
`alignof(std::max_align_t)`. It is implementation-defined whether any
|
| 3143 |
extended alignments are supported and the contexts in which they are
|
| 3144 |
supported ([[dcl.align]]). A type having an extended alignment
|
| 3145 |
-
requirement is an *over-aligned type*.
|
| 3146 |
-
|
| 3147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3148 |
|
| 3149 |
Alignments are represented as values of the type `std::size_t`. Valid
|
| 3150 |
alignments include only those values returned by an `alignof` expression
|
| 3151 |
for the fundamental types plus an additional *implementation-defined*
|
| 3152 |
set of values, which may be empty. Every alignment value shall be a
|
|
@@ -3158,30 +3660,30 @@ that satisfies an alignment requirement also satisfies any weaker valid
|
|
| 3158 |
alignment requirement.
|
| 3159 |
|
| 3160 |
The alignment requirement of a complete type can be queried using an
|
| 3161 |
`alignof` expression ([[expr.alignof]]). Furthermore, the narrow
|
| 3162 |
character types ([[basic.fundamental]]) shall have the weakest
|
| 3163 |
-
alignment requirement.
|
| 3164 |
-
|
|
|
|
|
|
|
|
|
|
| 3165 |
|
| 3166 |
Comparing alignments is meaningful and provides the obvious results:
|
| 3167 |
|
| 3168 |
- Two alignments are equal when their numeric values are equal.
|
| 3169 |
- Two alignments are different when their numeric values are not equal.
|
| 3170 |
- When an alignment is larger than another it represents a stricter
|
| 3171 |
alignment.
|
| 3172 |
|
| 3173 |
-
The runtime pointer alignment function ([[ptr.align]]) can
|
| 3174 |
-
obtain an aligned pointer within a buffer; the
|
| 3175 |
-
in the library ([[meta.trans.other]]) can be
|
| 3176 |
-
storage.
|
| 3177 |
|
| 3178 |
If a request for a specific extended alignment in a specific context is
|
| 3179 |
not supported by an implementation, the program is ill-formed.
|
| 3180 |
-
Additionally, a request for runtime allocation of dynamic storage for
|
| 3181 |
-
which the requested alignment cannot be honored shall be treated as an
|
| 3182 |
-
allocation failure.
|
| 3183 |
|
| 3184 |
<!-- Link reference definitions -->
|
| 3185 |
[bad.alloc]: language.md#bad.alloc
|
| 3186 |
[basic]: #basic
|
| 3187 |
[basic.align]: #basic.align
|
|
@@ -3210,12 +3712,13 @@ allocation failure.
|
|
| 3210 |
[basic.scope.namespace]: #basic.scope.namespace
|
| 3211 |
[basic.scope.pdecl]: #basic.scope.pdecl
|
| 3212 |
[basic.scope.proto]: #basic.scope.proto
|
| 3213 |
[basic.scope.temp]: #basic.scope.temp
|
| 3214 |
[basic.start]: #basic.start
|
| 3215 |
-
[basic.start.
|
| 3216 |
[basic.start.main]: #basic.start.main
|
|
|
|
| 3217 |
[basic.start.term]: #basic.start.term
|
| 3218 |
[basic.stc]: #basic.stc
|
| 3219 |
[basic.stc.auto]: #basic.stc.auto
|
| 3220 |
[basic.stc.dynamic]: #basic.stc.dynamic
|
| 3221 |
[basic.stc.dynamic.allocation]: #basic.stc.dynamic.allocation
|
|
@@ -3227,10 +3730,11 @@ allocation failure.
|
|
| 3227 |
[basic.type.qualifier]: #basic.type.qualifier
|
| 3228 |
[basic.types]: #basic.types
|
| 3229 |
[class]: class.md#class
|
| 3230 |
[class.access]: class.md#class.access
|
| 3231 |
[class.base.init]: special.md#class.base.init
|
|
|
|
| 3232 |
[class.cdtor]: special.md#class.cdtor
|
| 3233 |
[class.conv.fct]: special.md#class.conv.fct
|
| 3234 |
[class.copy]: special.md#class.copy
|
| 3235 |
[class.ctor]: special.md#class.ctor
|
| 3236 |
[class.derived]: class.md#class.derived
|
|
@@ -3257,33 +3761,36 @@ allocation failure.
|
|
| 3257 |
[conv.lval]: conv.md#conv.lval
|
| 3258 |
[conv.mem]: conv.md#conv.mem
|
| 3259 |
[conv.prom]: conv.md#conv.prom
|
| 3260 |
[conv.ptr]: conv.md#conv.ptr
|
| 3261 |
[conv.qual]: conv.md#conv.qual
|
|
|
|
|
|
|
|
|
|
| 3262 |
[dcl.align]: dcl.md#dcl.align
|
| 3263 |
[dcl.array]: dcl.md#dcl.array
|
| 3264 |
[dcl.dcl]: dcl.md#dcl.dcl
|
| 3265 |
[dcl.decl]: dcl.md#dcl.decl
|
| 3266 |
[dcl.enum]: dcl.md#dcl.enum
|
| 3267 |
[dcl.fct]: dcl.md#dcl.fct
|
| 3268 |
[dcl.fct.def]: dcl.md#dcl.fct.def
|
| 3269 |
[dcl.fct.default]: dcl.md#dcl.fct.default
|
| 3270 |
-
[dcl.fct.spec]: dcl.md#dcl.fct.spec
|
| 3271 |
[dcl.init]: dcl.md#dcl.init
|
| 3272 |
[dcl.init.aggr]: dcl.md#dcl.init.aggr
|
| 3273 |
[dcl.init.ref]: dcl.md#dcl.init.ref
|
|
|
|
| 3274 |
[dcl.link]: dcl.md#dcl.link
|
| 3275 |
[dcl.mptr]: dcl.md#dcl.mptr
|
| 3276 |
[dcl.name]: dcl.md#dcl.name
|
| 3277 |
[dcl.ptr]: dcl.md#dcl.ptr
|
| 3278 |
[dcl.ref]: dcl.md#dcl.ref
|
| 3279 |
[dcl.spec]: dcl.md#dcl.spec
|
| 3280 |
[dcl.stc]: dcl.md#dcl.stc
|
| 3281 |
-
[dcl.type.cv]: dcl.md#dcl.type.cv
|
| 3282 |
[dcl.type.elab]: dcl.md#dcl.type.elab
|
| 3283 |
[dcl.type.simple]: dcl.md#dcl.type.simple
|
| 3284 |
[dcl.typedef]: dcl.md#dcl.typedef
|
|
|
|
| 3285 |
[diff.cpp11.basic]: compatibility.md#diff.cpp11.basic
|
| 3286 |
[except]: except.md#except
|
| 3287 |
[except.handle]: except.md#except.handle
|
| 3288 |
[except.spec]: except.md#except.spec
|
| 3289 |
[except.terminate]: except.md#except.terminate
|
|
@@ -3297,26 +3804,35 @@ allocation failure.
|
|
| 3297 |
[expr.comma]: expr.md#expr.comma
|
| 3298 |
[expr.cond]: expr.md#expr.cond
|
| 3299 |
[expr.const]: expr.md#expr.const
|
| 3300 |
[expr.delete]: expr.md#expr.delete
|
| 3301 |
[expr.dynamic.cast]: expr.md#expr.dynamic.cast
|
|
|
|
| 3302 |
[expr.mptr.oper]: expr.md#expr.mptr.oper
|
| 3303 |
[expr.new]: expr.md#expr.new
|
|
|
|
|
|
|
| 3304 |
[expr.prim]: expr.md#expr.prim
|
| 3305 |
-
[expr.prim.
|
|
|
|
| 3306 |
[expr.pseudo]: expr.md#expr.pseudo
|
| 3307 |
[expr.ref]: expr.md#expr.ref
|
|
|
|
|
|
|
| 3308 |
[expr.sizeof]: expr.md#expr.sizeof
|
| 3309 |
[expr.static.cast]: expr.md#expr.static.cast
|
|
|
|
| 3310 |
[expr.type.conv]: expr.md#expr.type.conv
|
| 3311 |
[expr.typeid]: expr.md#expr.typeid
|
|
|
|
| 3312 |
[fig:categories]: #fig:categories
|
| 3313 |
[headers]: library.md#headers
|
| 3314 |
[intro.execution]: intro.md#intro.execution
|
| 3315 |
[intro.memory]: intro.md#intro.memory
|
| 3316 |
[intro.multithread]: intro.md#intro.multithread
|
| 3317 |
[intro.object]: intro.md#intro.object
|
|
|
|
| 3318 |
[lex]: lex.md#lex
|
| 3319 |
[lex.name]: lex.md#lex.name
|
| 3320 |
[locale]: localization.md#locale
|
| 3321 |
[meta.trans.other]: utilities.md#meta.trans.other
|
| 3322 |
[multibyte.strings]: library.md#multibyte.strings
|
|
@@ -3325,10 +3841,13 @@ allocation failure.
|
|
| 3325 |
[namespace.memdef]: dcl.md#namespace.memdef
|
| 3326 |
[namespace.qual]: #namespace.qual
|
| 3327 |
[namespace.udecl]: dcl.md#namespace.udecl
|
| 3328 |
[namespace.udir]: dcl.md#namespace.udir
|
| 3329 |
[new.delete]: language.md#new.delete
|
|
|
|
|
|
|
|
|
|
| 3330 |
[new.handler]: language.md#new.handler
|
| 3331 |
[over]: over.md#over
|
| 3332 |
[over.literal]: over.md#over.literal
|
| 3333 |
[over.load]: over.md#over.load
|
| 3334 |
[over.match]: over.md#over.match
|
|
@@ -3339,24 +3858,25 @@ allocation failure.
|
|
| 3339 |
[set.new.handler]: language.md#set.new.handler
|
| 3340 |
[stmt.block]: stmt.md#stmt.block
|
| 3341 |
[stmt.dcl]: stmt.md#stmt.dcl
|
| 3342 |
[stmt.expr]: stmt.md#stmt.expr
|
| 3343 |
[stmt.goto]: stmt.md#stmt.goto
|
|
|
|
| 3344 |
[stmt.label]: stmt.md#stmt.label
|
| 3345 |
[stmt.return]: stmt.md#stmt.return
|
| 3346 |
[stmt.select]: stmt.md#stmt.select
|
| 3347 |
[support.dynamic]: language.md#support.dynamic
|
| 3348 |
[support.limits]: language.md#support.limits
|
| 3349 |
[support.runtime]: language.md#support.runtime
|
| 3350 |
[support.start.term]: language.md#support.start.term
|
| 3351 |
[support.types]: language.md#support.types
|
| 3352 |
[tab:relations.on.const.and.volatile]: #tab:relations.on.const.and.volatile
|
| 3353 |
[temp]: temp.md#temp
|
| 3354 |
-
[temp.arg.nontype]: temp.md#temp.arg.nontype
|
| 3355 |
-
[temp.arg.type]: temp.md#temp.arg.type
|
| 3356 |
[temp.class.spec]: temp.md#temp.class.spec
|
|
|
|
| 3357 |
[temp.dep]: temp.md#temp.dep
|
|
|
|
| 3358 |
[temp.explicit]: temp.md#temp.explicit
|
| 3359 |
[temp.fct]: temp.md#temp.fct
|
| 3360 |
[temp.local]: temp.md#temp.local
|
| 3361 |
[temp.mem.func]: temp.md#temp.mem.func
|
| 3362 |
[temp.names]: temp.md#temp.names
|
|
@@ -3395,39 +3915,39 @@ allocation failure.
|
|
| 3395 |
`Y`’s definition or whether `X`’s definition appears in a namespace
|
| 3396 |
scope enclosing `Y`’s definition ([[class.nest]]).
|
| 3397 |
|
| 3398 |
[^7]: That is, an unqualified name that occurs, for instance, in a type
|
| 3399 |
in the *parameter-declaration-clause* or in the
|
| 3400 |
-
*
|
| 3401 |
|
| 3402 |
[^8]: This lookup applies whether the member function is defined within
|
| 3403 |
the definition of class `X` or whether the member function is
|
| 3404 |
defined in a namespace scope enclosing `X`’s definition.
|
| 3405 |
|
| 3406 |
[^9]: Lookups in which function names are ignored include names
|
| 3407 |
appearing in a *nested-name-specifier*, an
|
| 3408 |
*elaborated-type-specifier*, or a *base-specifier*.
|
| 3409 |
|
| 3410 |
-
[^10]: A class template
|
| 3411 |
-
|
| 3412 |
-
that the template arguments will also have appropriate linkage.
|
| 3413 |
|
| 3414 |
[^11]: A non-local variable with static storage duration having
|
| 3415 |
-
initialization with side
|
| 3416 |
-
not odr-used ([[basic.def.odr]],
|
|
|
|
| 3417 |
|
| 3418 |
-
[^12]:
|
|
|
|
|
|
|
|
|
|
| 3419 |
`std::malloc()` or `std::calloc()`, so the rules are substantially
|
| 3420 |
the same. C++differs from C in requiring a zero request to return a
|
| 3421 |
non-null pointer.
|
| 3422 |
|
| 3423 |
-
[^
|
| 3424 |
-
function `void operator new(std::size_t, std::size_t)`
|
| 3425 |
-
placement allocation function ([[diff.cpp11.basic]]).
|
| 3426 |
-
|
| 3427 |
-
[^14]: Some implementations might define that copying an invalid pointer
|
| 3428 |
-
value causes a system-generated runtime fault.
|
| 3429 |
|
| 3430 |
[^15]: This section does not impose restrictions on indirection through
|
| 3431 |
pointers to memory not allocated by `::operator new`. This maintains
|
| 3432 |
the ability of many C++implementations to use binary libraries and
|
| 3433 |
components written in other languages. In particular, this applies
|
|
@@ -3453,12 +3973,12 @@ allocation failure.
|
|
| 3453 |
of ISO/IEC 9899 Programming Language C.
|
| 3454 |
|
| 3455 |
[^21]: The size and layout of an instance of an incompletely-defined
|
| 3456 |
object type is unknown.
|
| 3457 |
|
| 3458 |
-
[^22]:
|
| 3459 |
-
`INT_MIN`
|
| 3460 |
|
| 3461 |
[^23]: See [[dcl.type.simple]] regarding the correspondence between
|
| 3462 |
types and the sequences of *type-specifier*s that designate them.
|
| 3463 |
|
| 3464 |
[^24]: This implies that unsigned arithmetic does not overflow because a
|
|
@@ -3466,11 +3986,11 @@ allocation failure.
|
|
| 3466 |
type is reduced modulo the number that is one greater than the
|
| 3467 |
largest value that can be represented by the resulting unsigned
|
| 3468 |
integer type.
|
| 3469 |
|
| 3470 |
[^25]: Using a `bool` value in ways described by this International
|
| 3471 |
-
Standard as “undefined
|
| 3472 |
uninitialized automatic object, might cause it to behave as if it is
|
| 3473 |
neither `true` nor `false`.
|
| 3474 |
|
| 3475 |
[^26]: Therefore, enumerations ([[dcl.enum]]) are not integral;
|
| 3476 |
however, enumerations can be promoted to integral types as specified
|
|
@@ -3484,11 +4004,14 @@ allocation failure.
|
|
| 3484 |
Information Processing Systems*.)
|
| 3485 |
|
| 3486 |
[^28]: Static class members are objects or functions, and pointers to
|
| 3487 |
them are ordinary pointers to objects or functions.
|
| 3488 |
|
| 3489 |
-
[^29]:
|
|
|
|
|
|
|
|
|
|
| 3490 |
imply interchangeability as arguments to functions, return values
|
| 3491 |
from functions, and non-static data members of unions.
|
| 3492 |
|
| 3493 |
-
[^
|
| 3494 |
which an object may or may not be aliased.
|
|
|
|
| 1 |
# Basic concepts <a id="basic">[[basic]]</a>
|
| 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
|
| 7 |
+
type, scope, linkage, and storage duration. The mechanisms for starting
|
| 8 |
+
and terminating a program are discussed. Finally, this Clause presents
|
| 9 |
+
the fundamental types of the language and lists the ways of constructing
|
| 10 |
+
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, function, enumerator, type,
|
| 17 |
+
class member, bit-field, template, template specialization, namespace,
|
| 18 |
+
or parameter pack.
|
| 19 |
|
| 20 |
A *name* is a use of an *identifier* ([[lex.name]]),
|
| 21 |
*operator-function-id* ([[over.oper]]), *literal-operator-id* (
|
| 22 |
[[over.literal]]), *conversion-function-id* ([[class.conv.fct]]), or
|
| 23 |
+
*template-id* ([[temp.names]]) that denotes an entity or label (
|
| 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 ([[stmt.goto]]) or a *labeled-statement* ([[stmt.label]]).
|
|
|
|
| 59 |
attributes of these names. A declaration may also have effects
|
| 60 |
including:
|
| 61 |
|
| 62 |
- a static assertion (Clause [[dcl.dcl]]),
|
| 63 |
- controlling template instantiation ([[temp.explicit]]),
|
| 64 |
+
- guiding template argument deduction for constructors (
|
| 65 |
+
[[temp.deduct.guide]]),
|
| 66 |
- use of attributes (Clause [[dcl.dcl]]), and
|
| 67 |
- nothing (in the case of an *empty-declaration*).
|
| 68 |
|
| 69 |
+
A declaration is a *definition* unless
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
- it declares a function without specifying the function’s body (
|
| 72 |
+
[[dcl.fct.def]]),
|
| 73 |
+
- it contains the `extern` specifier ([[dcl.stc]]) or a
|
| 74 |
+
*linkage-specification*[^1] ([[dcl.link]]) and neither an
|
| 75 |
+
*initializer* nor a *function-body*,
|
| 76 |
+
- it declares a non-inline static data member in a class definition (
|
| 77 |
+
[[class.mem]], [[class.static]]),
|
| 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.static_constexpr]]),
|
| 81 |
+
- it is a class name declaration ([[class.name]]),
|
| 82 |
+
- it is an *opaque-enum-declaration* ([[dcl.enum]]),
|
| 83 |
+
- it is a *template-parameter* ([[temp.param]]),
|
| 84 |
+
- it is a *parameter-declaration* ([[dcl.fct]]) in a function
|
| 85 |
+
declarator that is not the *declarator* of a *function-definition*,
|
| 86 |
+
- it is a `typedef` declaration ([[dcl.typedef]]),
|
| 87 |
+
- it is an *alias-declaration* ([[dcl.typedef]]),
|
| 88 |
+
- it is a *using-declaration* ([[namespace.udecl]]),
|
| 89 |
+
- it is a *deduction-guide* ([[temp.deduct.guide]]),
|
| 90 |
+
- it is a *static_assert-declaration* (Clause [[dcl.dcl]]),
|
| 91 |
+
- it is an *attribute-declaration* (Clause [[dcl.dcl]]),
|
| 92 |
+
- it is an *empty-declaration* (Clause [[dcl.dcl]]),
|
| 93 |
+
- it is a *using-directive* ([[namespace.udir]]),
|
| 94 |
+
- it is an explicit instantiation declaration ([[temp.explicit]]), or
|
| 95 |
+
- it is an explicit specialization ([[temp.expl.spec]]) whose
|
| 96 |
+
*declaration* is not a definition.
|
| 97 |
+
|
| 98 |
+
[*Example 1*:
|
| 99 |
+
|
| 100 |
+
All but one of the following are definitions:
|
| 101 |
|
| 102 |
``` cpp
|
| 103 |
int a; // defines a
|
| 104 |
extern const int c = 1; // defines c
|
| 105 |
int f(int x) { return x+a; } // defines f and defines x
|
|
|
|
| 126 |
typedef int Int; // declares Int
|
| 127 |
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 define
|
| 134 |
+
the default constructor ([[class.ctor]]), copy constructor (
|
| 135 |
+
[[class.copy]]), move constructor ([[class.copy]]), copy assignment
|
| 136 |
+
operator ([[class.copy]]), move assignment operator ([[class.copy]]),
|
| 137 |
+
or destructor ([[class.dtor]]) member functions. — *end note*]
|
| 138 |
+
|
| 139 |
+
[*Example 2*:
|
| 140 |
+
|
| 141 |
+
Given
|
| 142 |
|
| 143 |
``` cpp
|
| 144 |
#include <string>
|
| 145 |
|
| 146 |
struct C {
|
|
|
|
| 169 |
// { s = std::move(x.s); return *this; }
|
| 170 |
~C() { }
|
| 171 |
};
|
| 172 |
```
|
| 173 |
|
| 174 |
+
— *end example*]
|
| 175 |
+
|
| 176 |
+
[*Note 2*: A class name can also be implicitly declared by an
|
| 177 |
+
*elaborated-type-specifier* ([[dcl.type.elab]]). — *end note*]
|
| 178 |
|
| 179 |
A program is ill-formed if the definition of any object gives the object
|
| 180 |
an incomplete type ([[basic.types]]).
|
| 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, or template.
|
| 186 |
|
| 187 |
An expression is *potentially evaluated* unless it is an unevaluated
|
| 188 |
operand (Clause [[expr]]) or a subexpression thereof. The set of
|
| 189 |
*potential results* of an expression `e` is defined as follows:
|
| 190 |
|
| 191 |
+
- If `e` is an *id-expression* ([[expr.prim.id]]), the set contains
|
| 192 |
+
only `e`.
|
| 193 |
+
- If `e` is a subscripting operation ([[expr.sub]]) with an array
|
| 194 |
+
operand, the set contains the potential results of that operand.
|
| 195 |
- If `e` is a class member access expression ([[expr.ref]]), the set
|
| 196 |
contains the potential results of the object expression.
|
| 197 |
- If `e` is a pointer-to-member expression ([[expr.mptr.oper]]) whose
|
| 198 |
second operand is a constant expression, the set contains the
|
| 199 |
potential results of the object expression.
|
|
|
|
| 204 |
operands.
|
| 205 |
- If `e` is a comma expression ([[expr.comma]]), the set contains the
|
| 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 `e` or a subexpression of `e`.
|
| 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
|
| 218 |
+
second `S::x` subexpression.
|
| 219 |
|
| 220 |
``` cpp
|
| 221 |
struct S { static const int x = 0; };
|
| 222 |
const int &f(const int &r);
|
| 223 |
int n = b ? (1, S::x) // S::x is not odr-used here
|
| 224 |
+
: f(S::x); // S::x is odr-used here, so a definition is required
|
|
|
|
| 225 |
```
|
| 226 |
|
| 227 |
+
— *end example*]
|
| 228 |
+
|
| 229 |
+
— *end note*]
|
| 230 |
+
|
| 231 |
A variable `x` whose name appears as a potentially-evaluated expression
|
| 232 |
`ex` is *odr-used* by `ex` unless applying the lvalue-to-rvalue
|
| 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
|
|
|
|
| 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 |
+
[*Note 2*: This covers calls to named functions ([[expr.call]]),
|
| 250 |
+
operator overloading (Clause [[over]]), user-defined conversions (
|
| 251 |
+
[[class.conv.fct]]), allocation functions for placement
|
| 252 |
+
*new-expression*s ([[expr.new]]), as well as non-default
|
| 253 |
+
initialization ([[dcl.init]]). A constructor selected to copy or move
|
| 254 |
+
an object of class type is odr-used even if the call is actually elided
|
| 255 |
+
by the implementation ([[class.copy]]). — *end note*]
|
| 256 |
+
|
| 257 |
+
An allocation or deallocation function for a class is odr-used by a
|
| 258 |
+
*new-expression* appearing in a potentially-evaluated expression as
|
| 259 |
+
specified in [[expr.new]] and [[class.free]]. A deallocation function
|
| 260 |
+
for a class is odr-used by a delete expression appearing in a
|
| 261 |
+
potentially-evaluated expression as specified in [[expr.delete]] and
|
| 262 |
+
[[class.free]]. A non-placement allocation or deallocation function for
|
| 263 |
+
a class is odr-used by the definition of a constructor of that class. A
|
| 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]]).[^2] An assignment operator function in a class is
|
| 268 |
+
odr-used by an implicitly-defined copy-assignment or move-assignment
|
| 269 |
+
function for another class as specified in [[class.copy]]. A
|
| 270 |
+
constructor for a class is odr-used as specified in [[dcl.init]]. A
|
| 271 |
+
destructor for a class is odr-used if it is potentially invoked (
|
| 272 |
+
[[class.dtor]]).
|
| 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 ([[stmt.if]]); no diagnostic required. The
|
| 277 |
+
definition can appear explicitly in the program, it can be found in the
|
| 278 |
+
standard or a user-defined library, or (when appropriate) it is
|
| 279 |
+
implicitly defined (see [[class.ctor]], [[class.dtor]] and
|
| 280 |
+
[[class.copy]]). An inline function or variable shall be defined in
|
| 281 |
+
every translation unit in which it is odr-used outside of a discarded
|
| 282 |
+
statement.
|
| 283 |
|
| 284 |
Exactly one definition of a class is required in a translation unit if
|
| 285 |
the class is used in a way that requires the class type to be complete.
|
| 286 |
+
|
| 287 |
+
[*Example 2*:
|
| 288 |
+
|
| 289 |
+
The following complete translation unit is well-formed, even though it
|
| 290 |
never defines `X`:
|
| 291 |
|
| 292 |
``` cpp
|
| 293 |
struct X; // declare X as a struct type
|
| 294 |
struct X* x1; // use X in pointer formation
|
| 295 |
X* x2; // use X in pointer formation
|
| 296 |
```
|
| 297 |
|
| 298 |
+
— *end example*]
|
| 299 |
+
|
| 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 ([[basic.def]]), or
|
| 306 |
- a non-static class data member of type `T` is declared (
|
| 307 |
[[class.mem]]), or
|
| 308 |
+
- `T` is used as the allocated type or array element type in a
|
| 309 |
*new-expression* ([[expr.new]]), or
|
| 310 |
- an lvalue-to-rvalue conversion is applied to a glvalue referring to an
|
| 311 |
object of type `T` ([[conv.lval]]), or
|
| 312 |
- an expression is converted (either implicitly or explicitly) to type
|
| 313 |
`T` (Clause [[conv]], [[expr.type.conv]], [[expr.dynamic.cast]],
|
| 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 (Clause [[conv]]), a
|
| 318 |
`dynamic_cast` ([[expr.dynamic.cast]]) or a `static_cast` (
|
| 319 |
[[expr.static.cast]]), or
|
| 320 |
- a class member access operator is applied to an expression of type
|
| 321 |
`T` ([[expr.ref]]), or
|
| 322 |
- the `typeid` operator ([[expr.typeid]]) or the `sizeof` operator (
|
|
|
|
| 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` ([[except.handle]]).
|
| 333 |
|
| 334 |
+
— *end note*]
|
| 335 |
+
|
| 336 |
There can be more than one definition of a class type (Clause
|
| 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
|
|
|
|
| 350 |
and
|
| 351 |
- in each definition of `D`, corresponding names, looked up according
|
| 352 |
to [[basic.lookup]], shall refer to an entity defined within the
|
| 353 |
definition of `D`, or shall refer to the same entity, after overload
|
| 354 |
resolution ([[over.match]]) and after matching of partial template
|
| 355 |
+
specialization ([[temp.over]]), except that a name can refer to
|
| 356 |
+
- a non-volatile `const` object with internal or no linkage if the
|
| 357 |
+
object
|
| 358 |
+
- has the same literal type in all definitions of `D`,
|
| 359 |
+
- is initialized with a constant expression ([[expr.const]]),
|
| 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 |
+
and
|
| 369 |
- in each definition of `D`, corresponding entities shall have the same
|
| 370 |
language linkage; and
|
| 371 |
- in each definition of `D`, the overloaded operators referred to, the
|
| 372 |
implicit calls to conversion functions, constructors, operator new
|
| 373 |
functions and operator delete functions, shall refer to the same
|
| 374 |
function, or to a function defined within the definition of `D`; and
|
| 375 |
- in each definition of `D`, a default argument used by an (implicit or
|
| 376 |
explicit) function call is treated as if its token sequence were
|
| 377 |
present in the definition of `D`; that is, the default argument is
|
| 378 |
+
subject to the requirements described in this paragraph (and, if the
|
| 379 |
+
default argument has subexpressions with default arguments, this
|
| 380 |
+
requirement applies recursively).[^3]
|
| 381 |
- if `D` is a class with an implicitly-declared constructor (
|
| 382 |
[[class.ctor]]), it is as if the constructor was implicitly defined in
|
| 383 |
every translation unit where it is odr-used, and the implicit
|
| 384 |
definition in every translation unit shall call the same constructor
|
| 385 |
+
for a subobject of `D`.
|
| 386 |
+
\[*Example 3*:
|
| 387 |
``` cpp
|
| 388 |
// translation unit 1:
|
| 389 |
struct X {
|
|
|
|
| 390 |
X(int, int);
|
| 391 |
+
X(int, int, int);
|
| 392 |
};
|
| 393 |
+
X::X(int, int = 0) { }
|
| 394 |
+
class D {
|
| 395 |
+
X x = 0;
|
| 396 |
+
};
|
| 397 |
+
D d1; // X(int, int) called by D()
|
| 398 |
|
| 399 |
// translation unit 2:
|
| 400 |
struct X {
|
|
|
|
| 401 |
X(int, int);
|
| 402 |
+
X(int, int, int);
|
| 403 |
};
|
| 404 |
+
X::X(int, int = 0, int = 0) { }
|
| 405 |
+
class D {
|
| 406 |
+
X x = 0;
|
| 407 |
+
};
|
| 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]]), and also to dependent names at the point of
|
| 418 |
instantiation ([[temp.dep]]). If the definitions of `D` satisfy all
|
|
|
|
| 435 |
potential scope unless the potential scope contains another declaration
|
| 436 |
of the same name. In that case, the potential scope of the declaration
|
| 437 |
in the inner (contained) declarative region is excluded from the scope
|
| 438 |
of the declaration in the outer (containing) declarative region.
|
| 439 |
|
| 440 |
+
[*Example 1*:
|
| 441 |
+
|
| 442 |
+
In
|
| 443 |
|
| 444 |
``` cpp
|
| 445 |
int j = 24;
|
| 446 |
int main() {
|
| 447 |
int i = j, j;
|
|
|
|
| 457 |
declaration of `j` (the `j` immediately before the semicolon) includes
|
| 458 |
all the text between `{` and `}`, but its potential scope excludes the
|
| 459 |
declaration of `i`. The scope of the second declaration of `j` is the
|
| 460 |
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 ([[class.friend]]), certain uses of the
|
| 467 |
*elaborated-type-specifier* ([[dcl.type.elab]]), and
|
| 468 |
*using-directive*s ([[namespace.udir]]) alter this general behavior.
|
|
|
|
| 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 ([[basic.scope.hiding]]). \[*Note 1*: A
|
| 480 |
+
namespace name or a class template name must be unique in its
|
| 481 |
+
declarative region ([[namespace.alias]], Clause
|
| 482 |
+
[[temp]]). — *end note*]
|
| 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 ([[dcl.type.elab]]) and friend
|
| 488 |
+
declarations ([[class.friend]]) may introduce a (possibly not visible)
|
| 489 |
+
name into an enclosing namespace; these restrictions apply to that
|
| 490 |
+
region. Local extern declarations ([[basic.link]]) may introduce a name
|
| 491 |
+
into the declarative region where the declaration appears and also
|
| 492 |
introduce a (possibly not visible) name into an enclosing namespace;
|
| 493 |
+
these restrictions apply to both regions. — *end note*]
|
|
|
|
|
|
|
|
|
|
| 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 (Clause [[dcl.decl]]) and before its *initializer* (if any),
|
| 502 |
except as noted below.
|
| 503 |
|
| 504 |
+
[*Example 1*:
|
| 505 |
+
|
| 506 |
``` cpp
|
| 507 |
unsigned char x = 12;
|
| 508 |
{ unsigned char x = x; }
|
| 509 |
```
|
| 510 |
|
| 511 |
Here the second `x` is initialized with its own (indeterminate) value.
|
| 512 |
|
| 513 |
+
— *end example*]
|
| 514 |
+
|
| 515 |
+
[*Note 1*:
|
| 516 |
+
|
| 517 |
a name from an outer scope remains visible up to the point of
|
| 518 |
declaration of the name that hides it.
|
| 519 |
|
| 520 |
+
[*Example 2*:
|
| 521 |
+
|
| 522 |
``` cpp
|
| 523 |
const int i = 2;
|
| 524 |
{ int i[i]; }
|
| 525 |
```
|
| 526 |
|
| 527 |
declares a block-scope array of two integers.
|
| 528 |
|
| 529 |
+
— *end example*]
|
| 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* (Clause [[class]]).
|
| 536 |
The point of declaration for an enumeration is immediately after the
|
| 537 |
*identifier* (if any) in either its *enum-specifier* ([[dcl.enum]]) or
|
| 538 |
its first *opaque-enum-declaration* ([[dcl.enum]]), whichever comes
|
| 539 |
first. The point of declaration of an alias or alias template
|
| 540 |
immediately follows the *type-id* to which the alias refers.
|
| 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*:
|
| 550 |
+
|
| 551 |
``` cpp
|
| 552 |
const int x = 12;
|
| 553 |
{ enum { x = x }; }
|
| 554 |
```
|
| 555 |
|
| 556 |
Here, the enumerator `x` is initialized with the value of the constant
|
| 557 |
`x`, namely 12.
|
| 558 |
|
| 559 |
+
— *end example*]
|
| 560 |
+
|
| 561 |
After the point of declaration of a class member, the member name can be
|
| 562 |
+
looked up in the scope of its class.
|
| 563 |
+
|
| 564 |
+
[*Note 2*:
|
| 565 |
+
|
| 566 |
+
This is true even if the class is an incomplete class. For example,
|
| 567 |
|
| 568 |
``` cpp
|
| 569 |
struct X {
|
| 570 |
enum E { z = 16 };
|
| 571 |
int b[X::z]; // OK
|
| 572 |
};
|
| 573 |
```
|
| 574 |
|
| 575 |
+
— *end note*]
|
| 576 |
+
|
| 577 |
The point of declaration of a class first declared in an
|
| 578 |
*elaborated-type-specifier* is as follows:
|
| 579 |
|
| 580 |
- for a declaration of the form
|
| 581 |
``` bnf
|
|
|
|
| 592 |
if the *elaborated-type-specifier* is used in the *decl-specifier-seq*
|
| 593 |
or *parameter-declaration-clause* of a function defined in namespace
|
| 594 |
scope, the *identifier* is declared as a *class-name* in the namespace
|
| 595 |
that contains the declaration; otherwise, except as a friend
|
| 596 |
declaration, the *identifier* is declared in the smallest namespace or
|
| 597 |
+
block scope that contains the declaration.
|
| 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 *injected-class-name* (Clause
|
| 604 |
[[class]]) is immediately following the opening brace of the class
|
| 605 |
definition.
|
| 606 |
|
|
|
|
| 609 |
definition.
|
| 610 |
|
| 611 |
The point of declaration for a template parameter is immediately after
|
| 612 |
its complete *template-parameter*.
|
| 613 |
|
| 614 |
+
[*Example 4*:
|
| 615 |
+
|
| 616 |
``` cpp
|
| 617 |
typedef unsigned char T;
|
| 618 |
template<class T
|
| 619 |
= T // lookup finds the typedef name of unsigned char
|
| 620 |
, T // lookup finds the template parameter
|
| 621 |
N = 0> struct A { };
|
| 622 |
```
|
| 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 ([[namespace.memdef]]). Function
|
| 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 ([[stmt.block]]) is local to that block; it
|
| 640 |
has *block scope*. Its potential scope begins at its point of
|
|
|
|
| 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 |
### Function prototype scope <a id="basic.scope.proto">[[basic.scope.proto]]</a>
|
|
|
|
| 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 ([[basic.scope.pdecl]]) onwards; and for each
|
| 689 |
+
*using-directive* ([[namespace.udir]]) that nominates the member’s
|
| 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*:
|
|
|
|
| 695 |
|
| 696 |
``` cpp
|
| 697 |
namespace N {
|
| 698 |
int i;
|
| 699 |
int g(int a) { return a; }
|
| 700 |
int j();
|
| 701 |
void q();
|
| 702 |
}
|
| 703 |
namespace { int l=1; }
|
| 704 |
+
// the potential scope of l is from its point of declaration to the end of the translation unit
|
|
|
|
| 705 |
|
| 706 |
namespace N {
|
| 707 |
int g(char a) { // overloads N::g(int)
|
| 708 |
return l+a; // l is from unnamed namespace
|
| 709 |
}
|
|
|
|
| 716 |
}
|
| 717 |
int q(); // error: different return type
|
| 718 |
}
|
| 719 |
```
|
| 720 |
|
| 721 |
+
— *end example*]
|
| 722 |
+
|
| 723 |
A namespace member can also be referred to after the `::` scope
|
| 724 |
resolution operator ([[expr.prim]]) applied to the name of its
|
| 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 (
|
|
|
|
| 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 function bodies, default arguments, *noexcept-specifier*s,
|
| 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 the end of
|
| 753 |
+
a class definition also extends to the regions defined by its member
|
| 754 |
+
definitions, even if the members are defined lexically outside the class
|
| 755 |
+
(this includes static data member definitions, nested class definitions,
|
| 756 |
+
and member function definitions, including the member function body and
|
| 757 |
+
any portion of the declarator part of such definitions which follows the
|
| 758 |
+
*declarator-id*, including a *parameter-declaration-clause* and any
|
| 759 |
+
default arguments ([[dcl.fct.default]])).
|
| 760 |
+
|
| 761 |
+
[*Example 1*:
|
| 762 |
+
|
| 763 |
+
``` cpp
|
| 764 |
+
typedef int c;
|
| 765 |
+
enum { i = 1 };
|
| 766 |
+
|
| 767 |
+
class X {
|
| 768 |
+
char v[i]; // error: i refers to ::i but when reevaluated is X::i
|
| 769 |
+
int f() { return sizeof(c); } // OK: X::c
|
| 770 |
+
char c;
|
| 771 |
+
enum { i = 2 };
|
| 772 |
+
};
|
| 773 |
+
|
| 774 |
+
typedef char* T;
|
| 775 |
+
struct Y {
|
| 776 |
+
T a; // error: T refers to ::T but when reevaluated is Y::T
|
| 777 |
+
typedef long T;
|
| 778 |
+
T b;
|
| 779 |
+
};
|
| 780 |
+
|
| 781 |
+
typedef int I;
|
| 782 |
+
class D {
|
| 783 |
+
typedef I I; // error, even though no reordering involved
|
| 784 |
+
};
|
| 785 |
+
```
|
| 786 |
+
|
| 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 |
(Clause [[class.derived]]) from its class,
|
|
|
|
| 815 |
other kind of name introduced by the *declaration* of a
|
| 816 |
*template-declaration* is instead introduced into the same declarative
|
| 817 |
region where it would be introduced as a result of a non-template
|
| 818 |
declaration of the same name.
|
| 819 |
|
| 820 |
+
[*Example 1*:
|
| 821 |
+
|
| 822 |
``` cpp
|
| 823 |
namespace N {
|
| 824 |
template<class T> struct A { }; // #1
|
| 825 |
template<class U> void f(U) { } // #2
|
| 826 |
struct B {
|
|
|
|
| 828 |
};
|
| 829 |
}
|
| 830 |
```
|
| 831 |
|
| 832 |
The declarative regions of `T`, `U` and `V` are the
|
| 833 |
+
*template-declaration*s on lines \#1, \#2, and \#3, respectively. But
|
| 834 |
the names `A`, `f`, `g` and `C` all belong to the same declarative
|
| 835 |
region — namely, the *namespace-body* of `N`. (`g` is still considered
|
| 836 |
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 ([[basic.scope.pdecl]]) and ends at the end of its
|
| 843 |
+
declarative region.
|
| 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
|
| 849 |
+
cannot be used in preceding *template-parameter*s or their default
|
| 850 |
+
arguments. For example,
|
| 851 |
|
| 852 |
``` cpp
|
| 853 |
+
template<class T, T* p, class U = T> class X { ... };
|
| 854 |
template<class T> void f(T* p = new T);
|
| 855 |
```
|
| 856 |
|
| 857 |
This also implies that a *template-parameter* can be used in the
|
| 858 |
specification of base classes. For example,
|
| 859 |
|
| 860 |
``` cpp
|
| 861 |
+
template<class T> class X : public Array<T> { ... };
|
| 862 |
+
template<class T> class Y : public T { ... };
|
| 863 |
```
|
| 864 |
|
| 865 |
The use of a template parameter as a base class implies that a class
|
| 866 |
used as a template argument must be defined and not just declared when
|
| 867 |
the class template is instantiated.
|
| 868 |
|
| 869 |
+
— *end note*]
|
| 870 |
+
|
| 871 |
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 ([[basic.scope.hiding]]).
|
| 878 |
+
|
| 879 |
+
[*Example 2*:
|
| 880 |
|
| 881 |
``` cpp
|
| 882 |
typedef int N;
|
| 883 |
template<N X, typename N, template<N Y> class T> struct A;
|
| 884 |
```
|
| 885 |
|
| 886 |
Here, `X` is a non-type template parameter of type `int` and `Y` is a
|
| 887 |
non-type template parameter of the same type as the second template
|
| 888 |
parameter of `A`.
|
| 889 |
|
| 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 ([[temp.local]]), a template
|
| 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 name can be hidden by an explicit declaration of that same name in a
|
| 903 |
nested declarative region or derived class ([[class.member.lookup]]).
|
|
|
|
| 917 |
base class of the same name; see [[class.member.lookup]].
|
| 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]].
|
| 923 |
|
| 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 ([[dcl.typedef]]), *namespace-name*s (
|
| 930 |
[[basic.namespace]]), and *class-name*s ([[class.name]])) wherever the
|
| 931 |
grammar allows such names in the context discussed by a particular rule.
|
| 932 |
+
Name lookup associates the use of a name with a set of declarations (
|
| 933 |
+
[[basic.def]]) of that name. The declarations found by name lookup shall
|
| 934 |
+
either all declare the same entity or shall all declare functions; in
|
| 935 |
+
the latter case, the declarations are said to form a set of overloaded
|
| 936 |
+
functions ([[over.load]]). Overload resolution ([[over.match]]) takes
|
| 937 |
+
place after name lookup has succeeded. The access rules (Clause
|
| 938 |
+
[[class.access]]) are considered only once name lookup and function
|
| 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 attributes introduced by the name’s declaration
|
| 942 |
+
used further in expression processing (Clause [[expr]]).
|
|
|
|
|
|
|
| 943 |
|
| 944 |
A name “looked up in the context of an expression” is looked up as an
|
| 945 |
unqualified name in the scope where the expression is found.
|
| 946 |
|
| 947 |
The injected-class-name of a class (Clause [[class]]) is also
|
| 948 |
considered to be a member of that class for the purposes of name hiding
|
| 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 |
|
| 955 |
### Unqualified name lookup <a id="basic.lookup.unqual">[[basic.lookup.unqual]]</a>
|
| 956 |
|
| 957 |
In all the cases listed in [[basic.lookup.unqual]], the scopes are
|
| 958 |
searched for a declaration in the order listed in each of the respective
|
|
|
|
| 965 |
described in [[basic.lookup.unqual]], the declarations from the
|
| 966 |
namespace nominated by the *using-directive* are considered members of
|
| 967 |
that enclosing namespace.
|
| 968 |
|
| 969 |
The lookup for an unqualified name used as the *postfix-expression* of a
|
| 970 |
+
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. The rules in [[basic.lookup.argdep]] have no effect on the
|
| 977 |
syntactic interpretation of an expression. For example,
|
| 978 |
|
| 979 |
``` cpp
|
|
|
|
| 981 |
namespace N {
|
| 982 |
struct A {
|
| 983 |
friend void f(A &);
|
| 984 |
operator int();
|
| 985 |
void g(A a) {
|
| 986 |
+
int i = f(a); // f is the typedef, not the friend function: equivalent to int(a)
|
|
|
|
| 987 |
}
|
| 988 |
};
|
| 989 |
}
|
| 990 |
```
|
| 991 |
|
| 992 |
Because the expression is not a function call, the argument-dependent
|
| 993 |
name lookup ([[basic.lookup.argdep]]) does not apply and the friend
|
| 994 |
function `f` is not found.
|
| 995 |
|
| 996 |
+
— *end note*]
|
| 997 |
+
|
| 998 |
A name used in global scope, outside of any function, class or
|
| 999 |
user-declared namespace, shall be declared before its use in global
|
| 1000 |
scope.
|
| 1001 |
|
| 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*[^4] shall be declared
|
| 1008 |
+
before its use in the block in which it is used or in one of its
|
| 1009 |
+
enclosing blocks ([[stmt.block]]) or shall be declared before its use
|
| 1010 |
+
in namespace `N` or, if `N` is a nested namespace, shall be declared
|
|
|
|
| 1011 |
before its use in one of `N`’s enclosing namespaces.
|
| 1012 |
|
| 1013 |
+
[*Example 1*:
|
| 1014 |
+
|
| 1015 |
``` cpp
|
| 1016 |
namespace A {
|
| 1017 |
namespace N {
|
| 1018 |
void f();
|
| 1019 |
}
|
|
|
|
| 1026 |
// 3) scope of namespace A
|
| 1027 |
// 4) global scope, before the definition of A::N::f
|
| 1028 |
}
|
| 1029 |
```
|
| 1030 |
|
| 1031 |
+
— *end example*]
|
| 1032 |
+
|
| 1033 |
A name used in the definition of a class `X` outside of a member
|
| 1034 |
+
function body, default argument, *noexcept-specifier*,
|
| 1035 |
*brace-or-equal-initializer* of a non-static data member, or nested
|
| 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]]), or
|
| 1040 |
- if `X` is a nested class of class `Y` ([[class.nest]]), before the
|
| 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),[^6] or
|
| 1044 |
- if `X` is a local class ([[class.local]]) or is a nested class of a
|
| 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
|
| 1050 |
definition of class `X` in namespace `N` or in one of `N`’s enclosing
|
| 1051 |
namespaces.
|
| 1052 |
|
| 1053 |
+
[*Example 2*:
|
| 1054 |
+
|
| 1055 |
``` cpp
|
| 1056 |
namespace M {
|
| 1057 |
class B { };
|
| 1058 |
}
|
| 1059 |
```
|
|
|
|
| 1073 |
// 3) scope of N::Y's base class M::B
|
| 1074 |
// 4) scope of namespace N, before the definition of N::Y
|
| 1075 |
// 5) global scope, before the definition of 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 `friend` declaration, scopes outside of the innermost
|
| 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 member function body,
|
| 1092 |
+
in a default argument, in a *noexcept-specifier*, in the
|
| 1093 |
*brace-or-equal-initializer* of a non-static data member (
|
| 1094 |
[[class.mem]]), or in the definition of a class member outside of the
|
| 1095 |
definition of `X`, following the member’s *declarator-id*[^7], shall be
|
| 1096 |
declared in one of the following ways:
|
| 1097 |
|
|
|
|
| 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
|
| 1112 |
the name, in namespace `N` or in one of `N`’s enclosing namespaces.
|
| 1113 |
|
| 1114 |
+
[*Example 3*:
|
| 1115 |
+
|
| 1116 |
``` cpp
|
| 1117 |
class B { };
|
| 1118 |
namespace M {
|
| 1119 |
namespace N {
|
| 1120 |
class X : public B {
|
|
|
|
| 1133 |
// 4) scope of namespace M::N
|
| 1134 |
// 5) scope of namespace M
|
| 1135 |
// 6) global scope, before the definition of M::N::X::f
|
| 1136 |
```
|
| 1137 |
|
| 1138 |
+
— *end example*]
|
| 1139 |
+
|
| 1140 |
+
[*Note 4*: [[class.mfct]] and [[class.static]] further describe the
|
| 1141 |
+
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 `friend` function (
|
| 1148 |
[[class.friend]]) defined inline in the class granting friendship shall
|
| 1149 |
proceed as described for lookup in member function definitions. If the
|
| 1150 |
`friend` function is not defined in the class granting friendship, name
|
|
|
|
| 1157 |
class ([[class.member.lookup]]). If it is not found, or if the name is
|
| 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*:
|
| 1163 |
+
|
| 1164 |
``` cpp
|
| 1165 |
struct A {
|
| 1166 |
typedef int AT;
|
| 1167 |
void f1(AT);
|
| 1168 |
void f2(float);
|
|
|
|
| 1175 |
friend void A::f2(BT); // parameter type is B::BT
|
| 1176 |
friend void A::f3<AT>(); // template argument is B::AT
|
| 1177 |
};
|
| 1178 |
```
|
| 1179 |
|
| 1180 |
+
— *end example*]
|
| 1181 |
+
|
| 1182 |
During the lookup for a name used as a default argument (
|
| 1183 |
[[dcl.fct.default]]) in a function *parameter-declaration-clause* or
|
| 1184 |
used in the *expression* of a *mem-initializer* for a constructor (
|
| 1185 |
[[class.base.init]]), the function parameter names are visible and hide
|
| 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
|
| 1191 |
+
describes the restrictions on the use of names in a
|
| 1192 |
+
*ctor-initializer*. — *end note*]
|
| 1193 |
|
| 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]]) (after the *qualified-id* of the static member)
|
| 1201 |
is looked up as if the name was used in a member function of `X`.
|
| 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 |
|
| 1207 |
If a variable member of a namespace is defined outside of the scope of
|
| 1208 |
its namespace then any name that appears in the definition of the member
|
| 1209 |
(after the *declarator-id*) is looked up as if the definition of the
|
| 1210 |
member occurred in its namespace.
|
| 1211 |
|
| 1212 |
+
[*Example 5*:
|
| 1213 |
+
|
| 1214 |
``` cpp
|
| 1215 |
namespace N {
|
| 1216 |
int i = 4;
|
| 1217 |
extern int j;
|
| 1218 |
}
|
|
|
|
| 1220 |
int i = 2;
|
| 1221 |
|
| 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* (Clause
|
| 1228 |
[[except]]) is looked up as if the name was used in the outermost block
|
| 1229 |
of the function definition. In particular, the function parameter names
|
| 1230 |
shall not be redeclared in the *exception-declaration* nor in the
|
| 1231 |
outermost block of a handler for the *function-try-block*. Names
|
| 1232 |
declared in the outermost block of the function definition are not found
|
| 1233 |
when looked up in the scope of a handler for the *function-try-block*.
|
|
|
|
| 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 ([[expr.call]]) is an
|
| 1243 |
*unqualified-id*, other namespaces not considered during the usual
|
|
|
|
| 1246 |
declarations ([[class.friend]]) not otherwise visible may be found.
|
| 1247 |
These modifications to the search depend on the types of the arguments
|
| 1248 |
(and for template template arguments, the namespace of the template
|
| 1249 |
argument).
|
| 1250 |
|
| 1251 |
+
[*Example 1*:
|
| 1252 |
+
|
| 1253 |
``` cpp
|
| 1254 |
namespace N {
|
| 1255 |
struct S { };
|
| 1256 |
void f(S);
|
| 1257 |
}
|
| 1258 |
|
| 1259 |
void g() {
|
| 1260 |
N::S s;
|
| 1261 |
f(s); // OK: calls N::f
|
| 1262 |
+
(f)(s); // error: N::f not considered; parentheses prevent argument-dependent lookup
|
|
|
|
| 1263 |
}
|
| 1264 |
```
|
| 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 |
+
classes* to be considered. The sets of namespaces and classes are
|
| 1271 |
determined entirely by the types of the function arguments (and the
|
| 1272 |
namespace of any template template argument). Typedef names and
|
| 1273 |
*using-declaration*s used to specify the types do not contribute to this
|
| 1274 |
set. The sets of namespaces and classes are determined in the following
|
| 1275 |
way:
|
|
|
|
| 1284 |
and classes also include: the namespaces and classes associated with
|
| 1285 |
the types of the template arguments provided for template type
|
| 1286 |
parameters (excluding template template parameters); the namespaces of
|
| 1287 |
which any template template arguments are members; and the classes of
|
| 1288 |
which any member templates used as template template arguments are
|
| 1289 |
+
members. \[*Note 1*: Non-type template arguments do not contribute to
|
| 1290 |
+
the set of associated namespaces. — *end note*]
|
| 1291 |
- If `T` is an enumeration type, its associated namespace is the
|
| 1292 |
innermost enclosing namespace of its declaration. If it is a class
|
| 1293 |
member, its associated class is the member’s class; else it has no
|
| 1294 |
associated class.
|
| 1295 |
- If `T` is a pointer to `U` or an array of `U`, its associated
|
|
|
|
| 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
|
| 1327 |
+
- a declaration that is neither a function nor a function template
|
| 1328 |
|
| 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 classes associated with the argument
|
| 1335 |
+
types can include namespaces and classes already considered by the
|
| 1336 |
+
ordinary unqualified lookup. — *end note*]
|
| 1337 |
+
|
| 1338 |
+
[*Example 2*:
|
| 1339 |
|
| 1340 |
``` cpp
|
| 1341 |
namespace NS {
|
| 1342 |
class T { };
|
| 1343 |
void f(T);
|
|
|
|
| 1350 |
extern void g(NS::T, float);
|
| 1351 |
g(parm, 1); // OK: calls g(NS::T, float)
|
| 1352 |
}
|
| 1353 |
```
|
| 1354 |
|
| 1355 |
+
— *end example*]
|
| 1356 |
+
|
| 1357 |
When considering an associated namespace, the lookup is the same as the
|
| 1358 |
lookup performed when the associated namespace is used as a qualifier (
|
| 1359 |
[[namespace.qual]]) except that:
|
| 1360 |
|
| 1361 |
- Any *using-directive*s in the associated namespace are ignored.
|
|
|
|
| 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,
|
| 1379 |
the program is ill-formed.
|
| 1380 |
|
| 1381 |
+
[*Example 1*:
|
| 1382 |
+
|
| 1383 |
``` cpp
|
| 1384 |
class A {
|
| 1385 |
public:
|
| 1386 |
static int n;
|
| 1387 |
};
|
|
|
|
| 1390 |
A::n = 42; // OK
|
| 1391 |
A b; // ill-formed: A does not name a type
|
| 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 ([[class.nest]]) or members
|
| 1399 |
+
of nested namespaces. — *end note*]
|
| 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.
|
| 1405 |
|
| 1406 |
+
[*Example 2*:
|
| 1407 |
+
|
| 1408 |
``` cpp
|
| 1409 |
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]; // ill-formed:
|
| 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 `::` ([[expr.prim]]) is
|
| 1423 |
looked up in global scope, in the translation unit where it is used. The
|
| 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* ([[namespace.qual]]). The use of `::` allows a global
|
|
|
|
| 1439 |
nested-name-specifierₒₚₜ class-name '::' '~' class-name
|
| 1440 |
```
|
| 1441 |
|
| 1442 |
the second *class-name* is looked up in the same scope as the first.
|
| 1443 |
|
| 1444 |
+
[*Example 3*:
|
| 1445 |
+
|
| 1446 |
``` cpp
|
| 1447 |
struct C {
|
| 1448 |
typedef int I;
|
| 1449 |
};
|
| 1450 |
typedef int I1, I2;
|
| 1451 |
extern int* p;
|
| 1452 |
extern int* q;
|
| 1453 |
p->C::I::~I(); // I is looked up in the scope of C
|
| 1454 |
+
q->I1::~I2(); // I2 is looked up in the scope of the postfix-expression
|
|
|
|
| 1455 |
|
| 1456 |
struct A {
|
| 1457 |
~A();
|
| 1458 |
};
|
| 1459 |
typedef A AB;
|
|
|
|
| 1461 |
AB* p;
|
| 1462 |
p->AB::~AB(); // explicitly calls the destructor for A
|
| 1463 |
}
|
| 1464 |
```
|
| 1465 |
|
| 1466 |
+
— *end example*]
|
| 1467 |
+
|
| 1468 |
+
[*Note 2*: [[basic.lookup.classref]] describes how name lookup
|
| 1469 |
+
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 ([[class.member.lookup]]), except for the cases
|
| 1476 |
listed below. The name shall represent one or more members of that class
|
| 1477 |
+
or of one of its base classes (Clause [[class.derived]]).
|
|
|
|
|
|
|
|
|
|
| 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.
|
|
|
|
| 1495 |
In a lookup in which function names are not ignored[^9] and the
|
| 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` (Clause [[class]]), or
|
| 1500 |
+
- in a *using-declarator* of a *using-declaration* (
|
| 1501 |
+
[[namespace.udecl]]) that is a *member-declaration*, if the name
|
| 1502 |
+
specified after the *nested-name-specifier* is the same as the
|
| 1503 |
+
*identifier* or the *simple-template-id*’s *template-name* in the last
|
| 1504 |
+
component of the *nested-name-specifier*,
|
| 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
|
| 1510 |
+
used in place of the injected-class-name. — *end note*]
|
| 1511 |
+
|
| 1512 |
+
Such a constructor name shall be used only in the *declarator-id* of a
|
| 1513 |
+
declaration that names a constructor or in a *using-declaration*.
|
| 1514 |
+
|
| 1515 |
+
[*Example 1*:
|
| 1516 |
|
| 1517 |
``` cpp
|
| 1518 |
struct A { A(); };
|
| 1519 |
struct B: public A { B(); };
|
| 1520 |
|
|
|
|
| 1524 |
B::A ba; // object of type A
|
| 1525 |
A::A a; // error, A::A is not a type name
|
| 1526 |
struct A::A a2; // object of type A
|
| 1527 |
```
|
| 1528 |
|
| 1529 |
+
— *end example*]
|
| 1530 |
+
|
| 1531 |
A class member name hidden by a name in a nested declarative region or
|
| 1532 |
by the name of a derived class member can still be found if qualified by
|
| 1533 |
the name of its class followed by the `::` operator.
|
| 1534 |
|
| 1535 |
#### Namespace members <a id="namespace.qual">[[namespace.qual]]</a>
|
| 1536 |
|
| 1537 |
+
If the *nested-name-specifier* of a *qualified-id* nominates a namespace
|
| 1538 |
+
(including the case where the *nested-name-specifier* is `::`, i.e.,
|
| 1539 |
+
nominating the global namespace), the name specified after the
|
| 1540 |
+
*nested-name-specifier* is looked up in the scope of the namespace. 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]]). If S'(X, m) is not empty, S(X, m) is S'(X, m);
|
| 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]]), S(X, m) is the required set of declarations of
|
| 1556 |
`m`. Otherwise if the use of `m` is not one that allows a unique
|
| 1557 |
declaration to be chosen from S(X, m), the program is ill-formed.
|
| 1558 |
|
| 1559 |
+
[*Example 1*:
|
| 1560 |
+
|
| 1561 |
``` cpp
|
| 1562 |
int x;
|
| 1563 |
namespace Y {
|
| 1564 |
void f(float);
|
| 1565 |
void h(int);
|
|
|
|
| 1588 |
void g();
|
| 1589 |
}
|
| 1590 |
|
| 1591 |
void h()
|
| 1592 |
{
|
| 1593 |
+
AB::g(); // g is declared directly in AB, therefore S is { `AB::g()` } and AB::g() is chosen
|
| 1594 |
+
|
| 1595 |
+
AB::f(1); // f is not declared directly in AB so the rules are applied recursively to A and B;
|
| 1596 |
+
// namespace Y is not searched and Y::f(float) is not considered;
|
| 1597 |
+
// S is { `A::f(int)`, `B::f(char)` } and overload resolution chooses A::f(int)
|
| 1598 |
+
|
|
|
|
|
|
|
| 1599 |
AB::f('c'); // as above but resolution chooses B::f(char)
|
| 1600 |
|
| 1601 |
+
AB::x++; // x is not declared directly in AB, and is not declared in A or B, so the rules
|
| 1602 |
+
// are applied recursively to Y and Z, S is { } so the program is ill-formed
|
| 1603 |
+
|
| 1604 |
+
AB::i++; // i is not declared directly in AB so the rules are applied recursively to A and B,
|
| 1605 |
+
// S is { `A::i`, `B::i` } so the use is ambiguous and the program is ill-formed
|
| 1606 |
+
|
| 1607 |
+
AB::h(16.8); // h is not declared directly in AB and not declared directly in A or B so the rules
|
| 1608 |
+
// are applied recursively to Y and Z, S is { `Y::h(int)`, `Z::h(double)` } and
|
| 1609 |
+
// overload resolution chooses Z::h(double)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1610 |
}
|
| 1611 |
```
|
| 1612 |
|
| 1613 |
+
— *end example*]
|
| 1614 |
+
|
| 1615 |
+
[*Note 1*:
|
| 1616 |
+
|
| 1617 |
The same declaration found more than once is not an ambiguity (because
|
| 1618 |
+
it is still a unique declaration).
|
| 1619 |
+
|
| 1620 |
+
[*Example 2*:
|
| 1621 |
|
| 1622 |
``` cpp
|
| 1623 |
namespace A {
|
| 1624 |
int a;
|
| 1625 |
}
|
|
|
|
| 1637 |
using namespace C;
|
| 1638 |
}
|
| 1639 |
|
| 1640 |
void f()
|
| 1641 |
{
|
| 1642 |
+
BC::a++; // OK: S is { `A::a`, `A::a` }
|
| 1643 |
}
|
| 1644 |
|
| 1645 |
namespace D {
|
| 1646 |
using A::a;
|
| 1647 |
}
|
|
|
|
| 1651 |
using namespace D;
|
| 1652 |
}
|
| 1653 |
|
| 1654 |
void g()
|
| 1655 |
{
|
| 1656 |
+
BD::a++; // OK: S is { `A::a`, `A::a` }
|
| 1657 |
}
|
| 1658 |
```
|
| 1659 |
|
| 1660 |
+
— *end example*]
|
| 1661 |
+
|
| 1662 |
+
— *end note*]
|
| 1663 |
+
|
| 1664 |
+
[*Example 3*:
|
| 1665 |
+
|
| 1666 |
Because each referenced namespace is searched at most once, the
|
| 1667 |
following is well-defined:
|
| 1668 |
|
| 1669 |
``` cpp
|
| 1670 |
namespace B {
|
|
|
|
| 1680 |
using namespace A;
|
| 1681 |
}
|
| 1682 |
|
| 1683 |
void f()
|
| 1684 |
{
|
| 1685 |
+
A::a++; // OK: a declared directly in A, S is { `A::a` }
|
| 1686 |
+
B::a++; // OK: both A and B searched (once), S is { `A::a` }
|
| 1687 |
+
A::b++; // OK: both A and B searched (once), S is { `B::b` }
|
| 1688 |
+
B::b++; // OK: b declared directly in B, S is { `B::b` }
|
| 1689 |
}
|
| 1690 |
```
|
| 1691 |
|
| 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 |
either introduce the same variable, the same enumerator or a set of
|
| 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*:
|
| 1703 |
+
|
| 1704 |
``` cpp
|
| 1705 |
namespace A {
|
| 1706 |
struct x { };
|
| 1707 |
int x;
|
| 1708 |
int y;
|
|
|
|
| 1718 |
int i = C::x; // OK, A::x (of type int)
|
| 1719 |
int j = C::y; // ambiguous, A::y or B::y
|
| 1720 |
}
|
| 1721 |
```
|
| 1722 |
|
| 1723 |
+
— *end example*]
|
| 1724 |
+
|
| 1725 |
In a declaration for a namespace member in which the *declarator-id* is
|
| 1726 |
a *qualified-id*, given that the *qualified-id* for the namespace member
|
| 1727 |
has the form
|
| 1728 |
|
| 1729 |
``` bnf
|
|
|
|
| 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 |
set ([[namespace.def]]) of that namespace.
|
| 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){ } // ill-formed, f1 is not a member of A
|
| 1747 |
```
|
| 1748 |
|
| 1749 |
+
— *end example*]
|
| 1750 |
+
|
| 1751 |
However, in such namespace member declarations, the
|
| 1752 |
*nested-name-specifier* may rely on *using-directive*s to implicitly
|
| 1753 |
provide the initial part of the *nested-name-specifier*.
|
| 1754 |
|
| 1755 |
+
[*Example 6*:
|
| 1756 |
+
|
| 1757 |
``` cpp
|
| 1758 |
namespace A {
|
| 1759 |
namespace B {
|
| 1760 |
void f1(int);
|
| 1761 |
}
|
|
|
|
| 1770 |
using namespace A;
|
| 1771 |
using namespace C::D;
|
| 1772 |
void B::f1(int){ } // OK, defines A::B::f1(int)
|
| 1773 |
```
|
| 1774 |
|
| 1775 |
+
— *end example*]
|
| 1776 |
+
|
| 1777 |
### Elaborated type specifiers <a id="basic.lookup.elab">[[basic.lookup.elab]]</a>
|
| 1778 |
|
| 1779 |
An *elaborated-type-specifier* ([[dcl.type.elab]]) may be used to refer
|
| 1780 |
to a previously declared *class-name* or *enum-name* even though the
|
| 1781 |
name has been hidden by a non-type declaration (
|
|
|
|
| 1809 |
qualified name lookup is performed, as described in
|
| 1810 |
[[basic.lookup.qual]], but ignoring any non-type names that have been
|
| 1811 |
declared. If the name lookup does not find a previously declared
|
| 1812 |
*type-name*, the *elaborated-type-specifier* is ill-formed.
|
| 1813 |
|
| 1814 |
+
[*Example 1*:
|
| 1815 |
+
|
| 1816 |
``` cpp
|
| 1817 |
struct Node {
|
| 1818 |
struct Node* Next; // OK: Refers to Node at global scope
|
| 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~([dcl.type.elab])
|
| 1826 |
+
friend struct Glob; // OK: Refers to (as yet) undeclared Glob at global scope.
|
| 1827 |
+
...
|
|
|
|
|
|
|
| 1828 |
};
|
| 1829 |
|
| 1830 |
struct Base {
|
| 1831 |
struct Data; // OK: Declares nested Data
|
| 1832 |
struct ::Data* thatData; // OK: Refers to ::Data
|
| 1833 |
struct Base::Data* thisData; // OK: Refers to nested Data
|
| 1834 |
friend class ::Data; // OK: global Data is a friend
|
| 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~([dcl.type.elab])
|
| 1841 |
struct Base::Data; // error: cannot introduce a qualified type~([dcl.type.elab])
|
| 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 ([[expr.ref]]), if the `.` or `->`
|
| 1851 |
token is immediately followed by an *identifier* followed by a `<`, the
|
| 1852 |
identifier must be looked up to determine whether the `<` is the
|
|
|
|
| 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
|
| 1869 |
+
find a name that refers to cv `T`.
|
| 1870 |
+
|
| 1871 |
+
[*Example 1*:
|
| 1872 |
|
| 1873 |
``` cpp
|
| 1874 |
struct A { };
|
| 1875 |
|
| 1876 |
struct B {
|
|
|
|
| 1881 |
void B::f(::A* a) {
|
| 1882 |
a->~A(); // OK: lookup in *a finds the injected-class-name
|
| 1883 |
}
|
| 1884 |
```
|
| 1885 |
|
| 1886 |
+
— *end example*]
|
| 1887 |
+
|
| 1888 |
If the *id-expression* in a class member access is a *qualified-id* of
|
| 1889 |
the form
|
| 1890 |
|
| 1891 |
+
``` cpp
|
| 1892 |
+
class-name-or-namespace-name::...
|
| 1893 |
+
```
|
| 1894 |
+
|
| 1895 |
the *class-name-or-namespace-name* following the `.` or `->` operator is
|
| 1896 |
first looked up in the class of the object expression and the name, if
|
| 1897 |
found, is used. Otherwise it is looked up in the context of the entire
|
| 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 |
|
| 1904 |
If the *qualified-id* has the form
|
| 1905 |
|
| 1906 |
+
``` cpp
|
| 1907 |
+
::class-name-or-namespace-name::...
|
| 1908 |
+
```
|
| 1909 |
+
|
| 1910 |
the *class-name-or-namespace-name* is looked up in global scope as a
|
| 1911 |
*class-name* or *namespace-name*.
|
| 1912 |
|
| 1913 |
If the *nested-name-specifier* contains a *simple-template-id* (
|
| 1914 |
[[temp.names]]), the names in its *template-argument*s are looked up in
|
|
|
|
| 1919 |
expression and the name, if found, is used. Otherwise it is looked up in
|
| 1920 |
the context of the entire *postfix-expression*. In each of these
|
| 1921 |
lookups, only names that denote types or templates whose specializations
|
| 1922 |
are types are considered.
|
| 1923 |
|
| 1924 |
+
[*Example 2*:
|
| 1925 |
+
|
| 1926 |
``` cpp
|
| 1927 |
struct A { };
|
| 1928 |
namespace N {
|
| 1929 |
struct A {
|
| 1930 |
void g() { }
|
|
|
|
| 1936 |
N::A a;
|
| 1937 |
a.operator A(); // calls N::A::operator N::A
|
| 1938 |
}
|
| 1939 |
```
|
| 1940 |
|
| 1941 |
+
— *end example*]
|
| 1942 |
+
|
| 1943 |
### Using-directives and namespace aliases <a id="basic.lookup.udir">[[basic.lookup.udir]]</a>
|
| 1944 |
|
| 1945 |
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.
|
|
|
|
| 1972 |
A name having namespace scope ([[basic.scope.namespace]]) has internal
|
| 1973 |
linkage if it is the name of
|
| 1974 |
|
| 1975 |
- a variable, function or function template that is explicitly declared
|
| 1976 |
`static`; or,
|
| 1977 |
+
- a non-inline variable of non-volatile const-qualified type that is
|
| 1978 |
+
neither explicitly declared `extern` nor previously declared to have
|
| 1979 |
+
external linkage; or
|
| 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
|
|
|
|
| 1991 |
typedef declaration in which the class has the typedef name for
|
| 1992 |
linkage purposes ([[dcl.typedef]]); or
|
| 1993 |
- a named enumeration ([[dcl.enum]]), or an unnamed enumeration defined
|
| 1994 |
in a typedef declaration in which the enumeration has the typedef name
|
| 1995 |
for linkage purposes ([[dcl.typedef]]); or
|
|
|
|
| 1996 |
- a template.
|
| 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 ([[dcl.typedef]]), has the
|
| 2002 |
+
same linkage, if any, as the name of the class of which it is a member.
|
| 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 |
there is a visible declaration of an entity with linkage having the same
|
| 2007 |
name and type, ignoring entities declared outside the innermost
|
| 2008 |
enclosing namespace scope, the block scope declaration declares that
|
| 2009 |
same entity and receives the linkage of the previous declaration. If
|
| 2010 |
there is more than one such matching entity, the program is ill-formed.
|
| 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 |
}
|
| 2028 |
}
|
| 2029 |
```
|
| 2030 |
|
| 2031 |
+
Without the declaration at line \#2, the declaration at line \#3 would
|
| 2032 |
+
link with the declaration at line \#1. Because the declaration with
|
| 2033 |
+
internal linkage is hidden, however, \#3 is given external linkage,
|
| 2034 |
+
making the program ill-formed.
|
| 2035 |
+
|
| 2036 |
+
— *end example*]
|
| 2037 |
|
| 2038 |
When a block scope declaration of an entity with linkage is not found to
|
| 2039 |
refer to some other declaration, then that entity is a member of the
|
| 2040 |
innermost enclosing namespace. However such a declaration does not
|
| 2041 |
introduce the member name in its namespace scope.
|
| 2042 |
|
| 2043 |
+
[*Example 2*:
|
| 2044 |
+
|
| 2045 |
``` cpp
|
| 2046 |
namespace X {
|
| 2047 |
void p() {
|
| 2048 |
q(); // error: q not yet declared
|
| 2049 |
extern void q(); // q is a member of namespace X
|
|
|
|
| 2051 |
|
| 2052 |
void middle() {
|
| 2053 |
q(); // error: q not yet declared
|
| 2054 |
}
|
| 2055 |
|
| 2056 |
+
void q() { ... } // definition of X::q
|
| 2057 |
}
|
| 2058 |
|
| 2059 |
+
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 ([[basic.scope.block]]) has no
|
| 2066 |
linkage. A type is said to have linkage if and only if:
|
| 2067 |
|
| 2068 |
- it is a class or enumeration type that is named (or has a name for
|
| 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
|
|
|
|
| 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);
|
|
|
|
| 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 linkage or else both names have internal
|
| 2123 |
linkage and are declared in the same translation unit; and
|
| 2124 |
- both names refer to members of the same namespace or to members, not
|
| 2125 |
by inheritance, of the same class; and
|
|
|
|
| 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 ([[dcl.array]]). A violation of this rule on type identity does
|
| 2137 |
not require a diagnostic.
|
| 2138 |
|
| 2139 |
+
[*Note 2*: Linkage to non-C++declarations can be achieved using a
|
| 2140 |
+
*linkage-specification* ([[dcl.link]]). — *end note*]
|
| 2141 |
|
| 2142 |
## Start and termination <a id="basic.start">[[basic.start]]</a>
|
| 2143 |
|
| 2144 |
+
### `main` function <a id="basic.start.main">[[basic.start.main]]</a>
|
| 2145 |
|
| 2146 |
+
A program shall contain a global function called `main`. Executing a
|
| 2147 |
+
program starts a main thread of execution ([[intro.multithread]],
|
| 2148 |
+
[[thread.threads]]) in which the `main` function is invoked, and in
|
| 2149 |
+
which variables of static storage duration might be initialized (
|
| 2150 |
+
[[basic.start.static]]) and destroyed ([[basic.start.term]]). It is
|
| 2151 |
+
*implementation-defined* whether a program in a freestanding environment
|
| 2152 |
+
is required to define a `main` function.
|
| 2153 |
+
|
| 2154 |
+
[*Note 1*: In a freestanding environment, start-up and termination is
|
| 2155 |
*implementation-defined*; start-up contains the execution of
|
| 2156 |
constructors for objects of namespace scope with static storage
|
| 2157 |
duration; termination contains the execution of destructors for objects
|
| 2158 |
+
with static storage duration. — *end note*]
|
| 2159 |
|
| 2160 |
An implementation shall not predefine the `main` function. This function
|
| 2161 |
+
shall not be overloaded. Its type shall have C++language linkage and it
|
| 2162 |
+
shall have a declared return type of type `int`, but otherwise its type
|
| 2163 |
+
is *implementation-defined*. An implementation shall allow both
|
| 2164 |
|
| 2165 |
- a function of `()` returning `int` and
|
| 2166 |
- a function of `(int`, pointer to pointer to `char)` returning `int`
|
| 2167 |
|
| 2168 |
as the type of `main` ([[dcl.fct]]). In the latter form, for purposes
|
|
|
|
| 2173 |
supplied in `argv[0]` through `argv[argc-1]` as pointers to the initial
|
| 2174 |
characters of null-terminated multibyte strings (NTMBS s) (
|
| 2175 |
[[multibyte.strings]]) and `argv[0]` shall be the pointer to the initial
|
| 2176 |
character of a NTMBSthat represents the name used to invoke the program
|
| 2177 |
or `""`. The value of `argc` shall be non-negative. The value of
|
| 2178 |
+
`argv[argc]` shall be 0.
|
| 2179 |
+
|
| 2180 |
+
[*Note 2*: It is recommended that any further (optional) parameters be
|
| 2181 |
+
added after `argv`. — *end note*]
|
| 2182 |
|
| 2183 |
The function `main` shall not be used within a program. The linkage (
|
| 2184 |
[[basic.link]]) of `main` is *implementation-defined*. A program that
|
| 2185 |
defines `main` as deleted or that declares `main` to be `inline`,
|
| 2186 |
+
`static`, or `constexpr` is ill-formed. The `main` function shall not be
|
| 2187 |
+
declared with a *linkage-specification* ([[dcl.link]]). A program that
|
| 2188 |
+
declares a variable `main` at global scope or that declares the name
|
| 2189 |
+
`main` with C language linkage (in any namespace) is ill-formed. The
|
| 2190 |
+
name `main` is not otherwise reserved.
|
| 2191 |
+
|
| 2192 |
+
[*Example 1*: Member functions, classes, and enumerations can be called
|
| 2193 |
+
`main`, as can entities in other namespaces. — *end example*]
|
| 2194 |
|
| 2195 |
Terminating the program without leaving the current block (e.g., by
|
| 2196 |
calling the function `std::exit(int)` ([[support.start.term]])) does
|
| 2197 |
not destroy any objects with automatic storage duration (
|
| 2198 |
[[class.dtor]]). If `std::exit` is called to end a program during the
|
| 2199 |
destruction of an object with static or thread storage duration, the
|
| 2200 |
program has undefined behavior.
|
| 2201 |
|
| 2202 |
A return statement in `main` has the effect of leaving the main function
|
| 2203 |
(destroying any objects with automatic storage duration) and calling
|
| 2204 |
+
`std::exit` with the return value as the argument. If control flows off
|
| 2205 |
+
the end of the *compound-statement* of `main`, the effect is equivalent
|
| 2206 |
+
to a `return` with operand `0` (see also [[except.handle]]).
|
| 2207 |
|
| 2208 |
+
### Static initialization <a id="basic.start.static">[[basic.start.static]]</a>
|
|
|
|
|
|
|
| 2209 |
|
| 2210 |
+
Variables with static storage duration are initialized as a consequence
|
| 2211 |
+
of program initiation. Variables with thread storage duration are
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2212 |
initialized as a consequence of thread execution. Within each of these
|
| 2213 |
phases of initiation, initialization occurs as follows.
|
| 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
|
|
|
|
| 2265 |
// dynamically initialized to 0.0 if d1 is
|
| 2266 |
// dynamically initialized, or 1.0 otherwise
|
| 2267 |
double d1 = fd(); // may be initialized statically or dynamically to 1.0
|
| 2268 |
```
|
| 2269 |
|
| 2270 |
+
— *end note*]
|
| 2271 |
+
|
| 2272 |
+
### Dynamic initialization of non-local variables <a id="basic.start.dynamic">[[basic.start.dynamic]]</a>
|
| 2273 |
+
|
| 2274 |
+
Dynamic initialization of a non-local variable with static storage
|
| 2275 |
+
duration is unordered if the variable is an implicitly or explicitly
|
| 2276 |
+
instantiated specialization, is partially-ordered if the variable is an
|
| 2277 |
+
inline variable that is not an implicitly or explicitly instantiated
|
| 2278 |
+
specialization, and otherwise is ordered.
|
| 2279 |
+
|
| 2280 |
+
[*Note 1*: An explicitly specialized non-inline static data member or
|
| 2281 |
+
variable template specialization has ordered
|
| 2282 |
+
initialization. — *end note*]
|
| 2283 |
+
|
| 2284 |
+
Dynamic initialization of non-local variables `V` and `W` with static
|
| 2285 |
+
storage duration are ordered as follows:
|
| 2286 |
+
|
| 2287 |
+
- If `V` and `W` have ordered initialization and `V` is defined before
|
| 2288 |
+
`W` within a single translation unit, the initialization of `V` is
|
| 2289 |
+
sequenced before the initialization of `W`.
|
| 2290 |
+
- If `V` has partially-ordered initialization, `W` does not have
|
| 2291 |
+
unordered initialization, and `V` is defined before `W` in every
|
| 2292 |
+
translation unit in which `W` is defined, then
|
| 2293 |
+
- if the program starts a thread ([[intro.multithread]]) other than
|
| 2294 |
+
the main thread ([[basic.start.main]]), the initialization of `V`
|
| 2295 |
+
strongly happens before the initialization of `W`;
|
| 2296 |
+
- otherwise, the initialization of `V` is sequenced before the
|
| 2297 |
+
initialization of `W`.
|
| 2298 |
+
- Otherwise, if the program starts a thread other than the main thread
|
| 2299 |
+
before either `V` or `W` is initialized, it is unspecified in which
|
| 2300 |
+
threads the initializations of `V` and `W` occur; the initializations
|
| 2301 |
+
are unsequenced if they occur in the same thread.
|
| 2302 |
+
- Otherwise, the initializations of `V` and `W` are indeterminately
|
| 2303 |
+
sequenced.
|
| 2304 |
+
|
| 2305 |
+
[*Note 2*: This definition permits initialization of a sequence of
|
| 2306 |
+
ordered variables concurrently with another sequence. — *end note*]
|
| 2307 |
+
|
| 2308 |
+
A *non-initialization odr-use* is an odr-use ([[basic.def.odr]]) not
|
| 2309 |
+
caused directly or indirectly by the initialization of a non-local
|
| 2310 |
+
static or thread storage duration variable.
|
| 2311 |
+
|
| 2312 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 2313 |
+
non-local non-inline variable with static storage duration is sequenced
|
| 2314 |
+
before the first statement of `main` or is deferred. If it is deferred,
|
| 2315 |
+
it strongly happens before any non-initialization odr-use of any
|
| 2316 |
+
non-inline function or non-inline variable defined in the same
|
| 2317 |
+
translation unit as the variable to be initialized. [^11] It is
|
| 2318 |
+
*implementation-defined* in which threads and at which points in the
|
| 2319 |
+
program such deferred dynamic initialization occurs.
|
| 2320 |
+
|
| 2321 |
+
[*Note 3*: Such points should be chosen in a way that allows the
|
| 2322 |
+
programmer to avoid deadlocks. — *end note*]
|
| 2323 |
+
|
| 2324 |
+
[*Example 1*:
|
| 2325 |
|
| 2326 |
``` cpp
|
| 2327 |
// - File 1 -
|
| 2328 |
#include "a.h"
|
| 2329 |
#include "b.h"
|
|
|
|
| 2346 |
a.Use();
|
| 2347 |
b.Use();
|
| 2348 |
}
|
| 2349 |
```
|
| 2350 |
|
| 2351 |
+
It is *implementation-defined* whether either `a` or `b` is initialized
|
| 2352 |
before `main` is entered or whether the initializations are delayed
|
| 2353 |
until `a` is first odr-used in `main`. In particular, if `a` is
|
| 2354 |
initialized before `main` is entered, it is not guaranteed that `b` will
|
| 2355 |
be initialized before it is odr-used by the initialization of `a`, that
|
| 2356 |
is, before `A::A` is called. If, however, `a` is initialized at some
|
| 2357 |
point after the first statement of `main`, `b` will be initialized prior
|
| 2358 |
to its use in `A::A`.
|
| 2359 |
|
| 2360 |
+
— *end example*]
|
| 2361 |
+
|
| 2362 |
It is *implementation-defined* whether the dynamic initialization of a
|
| 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:
|
| 2463 |
|
| 2464 |
- static storage duration
|
| 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 ([[basic.def]]) and implicitly
|
| 2471 |
created by the implementation ([[class.temporary]]). The dynamic
|
| 2472 |
+
storage duration is associated with objects created by a
|
| 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 ([[basic.compound]]).
|
| 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.[^12]
|
| 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 shall last for the duration of
|
| 2490 |
+
the program ([[basic.start.static]], [[basic.start.term]]).
|
| 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`
|
| 2501 |
+
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>
|
|
|
|
| 2516 |
first odr-use ([[basic.def.odr]]) and, if constructed, shall be
|
| 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 |
|
| 2525 |
+
[*Note 1*: These variables are initialized and destroyed as described
|
| 2526 |
+
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]]), using *new-expression*s ([[expr.new]]), and
|
|
|
|
| 2539 |
C++implementation provides access to, and management of, dynamic storage
|
| 2540 |
via the global *allocation functions* `operator new` and `operator
|
| 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 ([[new.delete]]). A C++program shall provide
|
| 2551 |
at most one definition of a replaceable allocation or deallocation
|
| 2552 |
function. Any such function definition replaces the default version
|
|
|
|
| 2555 |
implicitly declared in global scope in each translation unit of a
|
| 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;
|
| 2574 |
```
|
| 2575 |
|
| 2576 |
These implicit declarations introduce only the function names `operator`
|
| 2577 |
`new`, `operator` `new[]`, `operator` `delete`, and `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 well-formed. However,
|
| 2585 |
+
referring to `std` or `std::size_t` or `std::align_val_t` is ill-formed
|
| 2586 |
+
unless the name has been declared by including the appropriate
|
| 2587 |
+
header. — *end note*]
|
| 2588 |
+
|
| 2589 |
+
Allocation and/or deallocation functions may also be declared and
|
| 2590 |
+
defined for any class ([[class.free]]).
|
| 2591 |
|
| 2592 |
Any allocation and/or deallocation functions defined in a C++program,
|
| 2593 |
including the default versions in the library, shall conform to the
|
| 2594 |
semantics specified in [[basic.stc.dynamic.allocation]] and
|
| 2595 |
[[basic.stc.dynamic.deallocation]].
|
|
|
|
| 2614 |
of a block of storage whose length in bytes shall be at least as large
|
| 2615 |
as the requested size. There are no constraints on the contents of the
|
| 2616 |
allocated storage on return from the allocation function. The order,
|
| 2617 |
contiguity, and initial value of storage allocated by successive calls
|
| 2618 |
to an allocation function are unspecified. The pointer returned shall be
|
| 2619 |
+
suitably aligned so that it can be converted to a pointer to any
|
| 2620 |
+
suitable complete object type ([[new.delete.single]]) and then used to
|
| 2621 |
+
access the object or array in the storage allocated (until the storage
|
| 2622 |
+
is explicitly deallocated by a call to a corresponding deallocation
|
| 2623 |
+
function). Even if the size of the space requested is zero, the request
|
| 2624 |
+
can fail. If the request succeeds, the value returned shall be a
|
| 2625 |
+
non-null pointer value ([[conv.ptr]]) `p0` different from any
|
| 2626 |
+
previously returned value `p1`, unless that value `p1` was subsequently
|
| 2627 |
+
passed to an `operator` `delete`. Furthermore, for the library
|
| 2628 |
+
allocation functions in [[new.delete.single]] and
|
| 2629 |
+
[[new.delete.array]], `p0` shall represent the address of a block of
|
| 2630 |
+
storage disjoint from the storage for any other object accessible to the
|
| 2631 |
+
caller. The effect of indirecting through a pointer returned as a
|
| 2632 |
+
request for zero size is undefined.[^13]
|
| 2633 |
|
| 2634 |
An allocation function that fails to allocate storage can invoke the
|
| 2635 |
+
currently installed new-handler function ([[new.handler]]), if any.
|
| 2636 |
+
|
| 2637 |
+
[*Note 1*: A program-supplied allocation function can obtain the
|
| 2638 |
+
address of the currently installed `new_handler` using the
|
| 2639 |
+
`std::get_new_handler` function ([[set.new.handler]]). — *end note*]
|
| 2640 |
+
|
| 2641 |
+
If an allocation function that has a non-throwing exception
|
| 2642 |
+
specification ([[except.spec]]) fails to allocate storage, it shall
|
| 2643 |
+
return a null pointer. Any other allocation function that fails to
|
| 2644 |
+
allocate storage shall indicate failure only by throwing an exception (
|
| 2645 |
+
[[except.throw]]) of a type that would match a handler (
|
| 2646 |
+
[[except.handle]]) of type `std::bad_alloc` ([[bad.alloc]]).
|
| 2647 |
|
| 2648 |
A global allocation function is only called as the result of a new
|
| 2649 |
expression ([[expr.new]]), or called directly using the function call
|
| 2650 |
syntax ([[expr.call]]), or called indirectly through calls to the
|
| 2651 |
+
functions in the C++standard library.
|
| 2652 |
+
|
| 2653 |
+
[*Note 2*: In particular, a global allocation function is not called to
|
| 2654 |
+
allocate storage for objects with static storage duration (
|
| 2655 |
+
[[basic.stc.static]]), for objects or references with thread storage
|
| 2656 |
+
duration ([[basic.stc.thread]]), for objects of type `std::type_info` (
|
| 2657 |
+
[[expr.typeid]]), or for an exception object (
|
| 2658 |
+
[[except.throw]]). — *end note*]
|
| 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 |
Each deallocation function shall return `void` and its first parameter
|
| 2668 |
+
shall be `void*`. A deallocation function may have more than one
|
| 2669 |
+
parameter. A *usual deallocation function* is a deallocation function
|
| 2670 |
+
that has:
|
| 2671 |
+
|
| 2672 |
+
- exactly one parameter; or
|
| 2673 |
+
- exactly two parameters, the type of the second being either
|
| 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 |
+
A deallocation function may be an instance of a function template.
|
| 2679 |
+
Neither the first parameter nor the return type shall depend on a
|
| 2680 |
+
template parameter.
|
| 2681 |
+
|
| 2682 |
+
[*Note 1*: That is, a deallocation function template shall have a first
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2683 |
parameter of type `void*` and a return type of `void` (as specified
|
| 2684 |
+
above). — *end note*]
|
| 2685 |
+
|
| 2686 |
+
A deallocation function template shall have two or more function
|
| 2687 |
parameters. A template instance is never a usual deallocation function,
|
| 2688 |
regardless of its signature.
|
| 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 ([[conv.ptr]]), the
|
| 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 |
|
|
|
|
| 2711 |
|
| 2712 |
A pointer value is a *safely-derived pointer* to a dynamic object only
|
| 2713 |
if it has an object pointer type and it is one of the following:
|
| 2714 |
|
| 2715 |
- the value returned by a call to the C++standard library implementation
|
| 2716 |
+
of `::operator new(std::{}size_t)` or
|
| 2717 |
+
`::operator new(std::size_t, std::align_val_t)` ;[^15]
|
| 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 ([[expr.add]]) using a
|
| 2722 |
safely-derived pointer value;
|
|
|
|
| 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
|
| 2730 |
copy of a safely-derived pointer value.
|
| 2731 |
|
| 2732 |
+
An integer value is an *integer representation of a safely-derived
|
| 2733 |
+
pointer* only if its type is at least as large as `std::intptr_t` and it
|
| 2734 |
+
is one of the following:
|
| 2735 |
|
| 2736 |
- the result of a `reinterpret_cast` of a safely-derived pointer value;
|
| 2737 |
- the result of a valid conversion of an integer representation of a
|
| 2738 |
safely-derived pointer value;
|
| 2739 |
- the value of an object whose value was copied from a traceable pointer
|
|
|
|
| 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 1*: The effect of using an invalid pointer value (including
|
| 2758 |
+
passing it to a deallocation function) is undefined, see
|
| 2759 |
[[basic.stc.dynamic.deallocation]]. This is true even if the
|
| 2760 |
unsafely-derived pointer value might compare equal to some
|
| 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 ([[intro.object]]).
|
| 2770 |
|
| 2771 |
## Object lifetime <a id="basic.life">[[basic.life]]</a>
|
| 2772 |
|
| 2773 |
+
The *lifetime* of an object or reference is a runtime property of the
|
| 2774 |
+
object or reference. An object is said to have *non-vacuous
|
| 2775 |
+
initialization* if it is of a class or aggregate type and it or one of
|
| 2776 |
+
its subobjects is initialized by a constructor other than a trivial
|
| 2777 |
+
default constructor.
|
| 2778 |
+
|
| 2779 |
+
[*Note 1*: Initialization by a trivial copy/move constructor is
|
| 2780 |
+
non-vacuous initialization. — *end note*]
|
| 2781 |
+
|
| 2782 |
+
The lifetime of an object of type `T` begins when:
|
| 2783 |
|
| 2784 |
- storage with the proper alignment and size for type `T` is obtained,
|
| 2785 |
and
|
| 2786 |
+
- if the object has non-vacuous initialization, its initialization is
|
| 2787 |
+
complete,
|
| 2788 |
|
| 2789 |
+
except that if the object is a union member or subobject thereof, its
|
| 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
|
|
|
|
| 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 |
#include <cstdlib>
|
| 2861 |
|
| 2862 |
struct B {
|
| 2863 |
virtual void f();
|
|
|
|
| 2876 |
|
| 2877 |
void g() {
|
| 2878 |
void* p = std::malloc(sizeof(D1) + sizeof(D2));
|
| 2879 |
B* pb = new (p) D1;
|
| 2880 |
pb->mutate();
|
| 2881 |
+
*pb; // OK: pb points to valid memory
|
| 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 |
Similarly, before the lifetime of an object has started but after the
|
| 2890 |
storage which the object will occupy has been allocated or, after the
|
| 2891 |
lifetime of an object has ended and before the storage which the object
|
| 2892 |
occupied is reused or released, any glvalue that refers to the original
|
| 2893 |
object may be used but only in limited ways. For an object under
|
|
|
|
| 2895 |
glvalue refers to allocated storage (
|
| 2896 |
[[basic.stc.dynamic.deallocation]]), and using the properties of the
|
| 2897 |
glvalue that do not depend on its value is well-defined. The program has
|
| 2898 |
undefined behavior if:
|
| 2899 |
|
| 2900 |
+
- the glvalue is used to access the object, or
|
| 2901 |
+
- the glvalue is used to call a non-static member function of the
|
| 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 |
|
|
|
|
| 2921 |
class type, does not contain any non-static data member whose type is
|
| 2922 |
const-qualified or a reference type, and
|
| 2923 |
- the original object was a most derived object ([[intro.object]]) of
|
| 2924 |
type `T` and the new object is a most derived object of type `T` (that
|
| 2925 |
is, they are not base class subobjects).
|
| 2926 |
+
|
| 2927 |
+
[*Example 2*:
|
| 2928 |
+
|
| 2929 |
``` cpp
|
| 2930 |
struct C {
|
| 2931 |
int i;
|
| 2932 |
void f();
|
| 2933 |
const C& operator=( const C& );
|
|
|
|
| 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 4*: If these conditions are not met, a pointer to the new object
|
| 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 |
If a program ends the lifetime of an object of type `T` with static (
|
| 2958 |
[[basic.stc.static]]), thread ([[basic.stc.thread]]), or automatic (
|
| 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 |
class T { };
|
| 2969 |
struct B {
|
| 2970 |
~B();
|
| 2971 |
};
|
|
|
|
| 2974 |
B b;
|
| 2975 |
new (&b) T;
|
| 2976 |
} // undefined behavior at block exit
|
| 2977 |
```
|
| 2978 |
|
| 2979 |
+
— *end example*]
|
| 2980 |
+
|
| 2981 |
+
Creating a new object within the storage that a `const` complete object
|
| 2982 |
+
with static, thread, or automatic storage duration occupies, or within
|
| 2983 |
+
the storage that such a `const` object used to occupy before its
|
| 2984 |
+
lifetime ended, results in undefined behavior.
|
| 2985 |
+
|
| 2986 |
+
[*Example 4*:
|
| 2987 |
|
| 2988 |
``` cpp
|
| 2989 |
struct B {
|
| 2990 |
B();
|
| 2991 |
~B();
|
|
|
|
| 2997 |
b.~B();
|
| 2998 |
new (const_cast<B*>(&b)) const B; // undefined behavior
|
| 2999 |
}
|
| 3000 |
```
|
| 3001 |
|
| 3002 |
+
— *end example*]
|
| 3003 |
+
|
| 3004 |
In this section, “before” and “after” refer to the “happens before”
|
| 3005 |
+
relation ([[intro.multithread]]).
|
| 3006 |
+
|
| 3007 |
+
[*Note 5*: Therefore, undefined behavior results if an object that is
|
| 3008 |
+
being constructed in one thread is referenced from another thread
|
| 3009 |
+
without adequate synchronization. — *end note*]
|
| 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 ([[intro.object]]), references ([[dcl.ref]]),
|
| 3017 |
+
or functions ([[dcl.fct]]). — *end note*]
|
| 3018 |
|
| 3019 |
For any object (other than a base-class subobject) of trivially copyable
|
| 3020 |
type `T`, whether or not the object holds a valid value of type `T`, the
|
| 3021 |
underlying bytes ([[intro.memory]]) making up the object can be copied
|
| 3022 |
+
into an array of `char`, `unsigned char`, or `std::byte` (
|
| 3023 |
+
[[cstddef.syn]]). [^18] If the content of that array is copied back into
|
| 3024 |
+
the object, the object shall subsequently hold its original value.
|
| 3025 |
+
|
| 3026 |
+
[*Example 1*:
|
| 3027 |
|
| 3028 |
``` cpp
|
| 3029 |
#define N sizeof(T)
|
| 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 base-class subobject, if the underlying bytes ([[intro.memory]])
|
| 3041 |
making up `obj1` are copied into `obj2`,[^19] `obj2` shall subsequently
|
| 3042 |
hold the same value as `obj1`.
|
| 3043 |
|
| 3044 |
+
[*Example 2*:
|
| 3045 |
+
|
| 3046 |
``` cpp
|
| 3047 |
T* t1p;
|
| 3048 |
T* t2p;
|
| 3049 |
// provided that t2p points to an initialized object ...
|
| 3050 |
std::memcpy(t1p, t2p, sizeof(T));
|
| 3051 |
// at this point, every subobject of trivially copyable type in *t1p contains
|
| 3052 |
// the same value as the corresponding subobject in *t2p
|
| 3053 |
```
|
| 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 is the
|
| 3060 |
set of bits that hold the value of type `T`. For trivially copyable
|
| 3061 |
types, the value representation is a set of bits in the object
|
| 3062 |
representation that determines a *value*, which is one discrete element
|
| 3063 |
of an *implementation-defined* set of values.[^20]
|
| 3064 |
|
| 3065 |
A class that has been declared but not defined, an enumeration type in
|
| 3066 |
+
certain contexts ([[dcl.enum]]), or an array of unknown bound or of
|
| 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]]). Objects shall not be defined to have an
|
| 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
|
| 3075 |
array of incomplete class type and therefore incomplete; if the class
|
| 3076 |
type is completed later on in the translation unit, the array type
|
| 3077 |
becomes complete; the array type at those two points is the same type.
|
| 3078 |
+
The declared type of an array object might be an array of unknown bound
|
| 3079 |
and therefore be incomplete at one point in a translation unit and
|
| 3080 |
complete later on; the array types at those two points (“array of
|
| 3081 |
unknown bound of `T`” and “array of `N` `T`”) are different types. The
|
| 3082 |
+
type of a pointer to array of unknown bound, or of a type defined by a
|
| 3083 |
+
`typedef` declaration to be an array of unknown bound, cannot be
|
| 3084 |
completed.
|
| 3085 |
|
| 3086 |
+
[*Example 3*:
|
| 3087 |
+
|
| 3088 |
``` cpp
|
| 3089 |
class X; // X is an incomplete type
|
| 3090 |
extern X* xp; // xp is a pointer to an incomplete type
|
| 3091 |
extern int arr[]; // the type of arr is incomplete
|
| 3092 |
typedef int UNKA[]; // UNKA is an incomplete type
|
|
|
|
| 3109 |
xp++; // OK: X is complete
|
| 3110 |
arrp++; // ill-formed: UNKA can't be completed
|
| 3111 |
}
|
| 3112 |
```
|
| 3113 |
|
| 3114 |
+
— *end example*]
|
| 3115 |
+
|
| 3116 |
+
[*Note 2*: The rules for declarations and expressions describe in which
|
| 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 ([[basic.fundamental]]), enumeration types, pointer
|
| 3123 |
types, pointer to member types ([[basic.compound]]), `std::nullptr_t`,
|
| 3124 |
+
and cv-qualified ([[basic.type.qualifier]]) versions of these types are
|
| 3125 |
collectively called *scalar types*. Scalar types, POD classes (Clause
|
| 3126 |
+
[[class]]), arrays of such types and cv-qualified versions of these
|
| 3127 |
+
types are collectively called *POD types*. Cv-unqualified scalar types,
|
| 3128 |
+
trivially copyable class types (Clause [[class]]), arrays of such
|
| 3129 |
+
types, and cv-qualified versions of these types are collectively called
|
| 3130 |
+
*trivially copyable types*. Scalar types, trivial class types (Clause
|
| 3131 |
+
[[class]]), arrays of such types and cv-qualified versions of these
|
| 3132 |
+
types are collectively called *trivial types*. Scalar types,
|
| 3133 |
+
standard-layout class types (Clause [[class]]), arrays of such types
|
| 3134 |
+
and cv-qualified versions of these types are collectively called
|
| 3135 |
+
*standard-layout types*.
|
|
|
|
|
|
|
| 3136 |
|
| 3137 |
A type is a *literal type* if it is:
|
| 3138 |
|
| 3139 |
+
- possibly cv-qualified `void`; or
|
| 3140 |
- a scalar type; or
|
| 3141 |
- a reference type; or
|
| 3142 |
- an array of literal type; or
|
| 3143 |
+
- a possibly cv-qualified class type (Clause [[class]]) that has all of
|
| 3144 |
+
the following properties:
|
| 3145 |
- it has a trivial destructor,
|
| 3146 |
+
- it is either a closure type ([[expr.prim.lambda.closure]]), an
|
| 3147 |
+
aggregate type ([[dcl.init.aggr]]), or has at least one constexpr
|
| 3148 |
+
constructor or constructor template (possibly inherited (
|
| 3149 |
+
[[namespace.udecl]]) from a base class) that is not a copy or move
|
| 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]]), or layout-compatible standard-layout class types (
|
| 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
|
|
|
|
| 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*. Plain `int`s have the
|
| 3204 |
+
natural size suggested by the architecture of the execution environment
|
| 3205 |
+
[^22]; the other signed integer types are provided to meet special
|
| 3206 |
+
needs.
|
| 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`”, each of which
|
| 3212 |
+
occupies the same amount of storage and has the same alignment
|
| 3213 |
requirements ([[basic.align]]) as the corresponding signed integer
|
| 3214 |
type[^23]; that is, each signed integer type has the same object
|
| 3215 |
representation as its corresponding unsigned integer type. Likewise, for
|
| 3216 |
each of the extended signed integer types there exists a corresponding
|
| 3217 |
*extended unsigned integer type* with the same amount of storage and
|
| 3218 |
alignment requirements. The standard and extended unsigned integer types
|
| 3219 |
are collectively called *unsigned integer types*. The range of
|
| 3220 |
+
non-negative values of a signed integer type is a subrange of the
|
| 3221 |
+
corresponding unsigned integer type, the representation of the same
|
| 3222 |
+
value in each of the two types is the same, and the value representation
|
| 3223 |
+
of each corresponding signed/unsigned type shall be the same. The
|
| 3224 |
+
standard signed integer types and standard unsigned integer types are
|
| 3225 |
collectively called the *standard integer types*, and the extended
|
| 3226 |
signed integer types and extended unsigned integer types are
|
| 3227 |
collectively called the *extended integer types*. The signed and
|
| 3228 |
unsigned integer types shall satisfy the constraints given in the C
|
| 3229 |
standard, section 5.2.4.2.1.
|
|
|
|
| 3239 |
one of the other integral types, called its *underlying type*. Types
|
| 3240 |
`char16_t` and `char32_t` denote distinct types with the same size,
|
| 3241 |
signedness, and alignment as `uint_least16_t` and `uint_least32_t`,
|
| 3242 |
respectively, in `<cstdint>`, called the underlying types.
|
| 3243 |
|
| 3244 |
+
Values of type `bool` are either `true` or `false`.[^25]
|
| 3245 |
+
|
| 3246 |
+
[*Note 2*: There are no `signed`, `unsigned`, `short`, or `long bool`
|
| 3247 |
+
types or values. — *end note*]
|
| 3248 |
+
|
| 3249 |
+
Values of type `bool` participate in integral promotions (
|
| 3250 |
+
[[conv.prom]]).
|
| 3251 |
|
| 3252 |
Types `bool`, `char`, `char16_t`, `char32_t`, `wchar_t`, and the signed
|
| 3253 |
and unsigned integer types are collectively called *integral*
|
| 3254 |
types.[^26] A synonym for integral type is *integer type*. The
|
| 3255 |
representations of integral types shall define values by use of a pure
|
| 3256 |
+
binary numeration system.[^27]
|
|
|
|
|
|
|
| 3257 |
|
| 3258 |
+
[*Example 1*: This International Standard permits two’s complement,
|
| 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 3*: This International Standard imposes no requirements on the
|
| 3272 |
+
accuracy of floating-point operations; see also
|
| 3273 |
+
[[support.limits]]. — *end note*]
|
| 3274 |
+
|
| 3275 |
+
Integral and floating types are collectively called *arithmetic* types.
|
| 3276 |
+
Specializations of the standard library template `std::numeric_limits` (
|
| 3277 |
+
[[support.limits]]) shall specify the maximum and minimum values of each
|
| 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.cast]]). An expression of type
|
| 3284 |
+
cv `void` shall be used only as an expression statement (
|
| 3285 |
[[stmt.expr]]), as an operand of a comma expression ([[expr.comma]]),
|
| 3286 |
as a second or third operand of `?:` ([[expr.cond]]), as the operand of
|
| 3287 |
`typeid`, `noexcept`, or `decltype`, as the expression in a return
|
| 3288 |
+
statement ([[stmt.return]]) for a function with the return type
|
| 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]]). Such values participate in the pointer and the pointer to
|
| 3294 |
member conversions ([[conv.ptr]], [[conv.mem]]).
|
| 3295 |
`sizeof(std::nullptr_t)` shall be equal to `sizeof(void*)`.
|
| 3296 |
|
| 3297 |
+
[*Note 4*: Even if the implementation defines two or more basic types
|
| 3298 |
+
to have the same value representation, they are nevertheless different
|
| 3299 |
+
types. — *end note*]
|
| 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, [[dcl.array]];
|
| 3306 |
- *functions*, which have parameters of given types and return `void` or
|
| 3307 |
references or objects of a given type, [[dcl.fct]];
|
| 3308 |
+
- *pointers* to cv `void` or objects or functions (including static
|
| 3309 |
+
members of classes) of a given type, [[dcl.ptr]];
|
| 3310 |
- *references* to objects or functions of a given type, [[dcl.ref]].
|
| 3311 |
There are two types of references:
|
| 3312 |
- *lvalue reference*
|
| 3313 |
- *rvalue reference*
|
| 3314 |
- *classes* containing a sequence of objects of various types (Clause
|
|
|
|
| 3319 |
- *unions*, which are classes capable of containing objects of different
|
| 3320 |
types at different times, [[class.union]];
|
| 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*, [^28] which identify members
|
| 3325 |
of a given type within objects of a given class, [[dcl.mptr]].
|
| 3326 |
|
| 3327 |
These methods of constructing types can be applied recursively;
|
| 3328 |
restrictions are mentioned in [[dcl.ptr]], [[dcl.array]], [[dcl.fct]],
|
| 3329 |
and [[dcl.ref]]. Constructing a type such that the number of bytes in
|
| 3330 |
its object representation exceeds the maximum value representable in the
|
| 3331 |
type `std::size_t` ([[support.types]]) is ill-formed.
|
| 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 objects of type `T` is referred 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]]). Every value of pointer type is one of the following:
|
| 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 ([[expr.add]]), or
|
| 3355 |
+
- the *null pointer value* ([[conv.ptr]]) for that type, or
|
| 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]]) occupied by the object [^29] or the first byte in
|
| 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 ([[expr.add]]) is not
|
| 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 ([[expr.add]]) and comparison (
|
| 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[n]`. The value representation of
|
| 3374 |
+
pointer types is *implementation-defined*. Pointers to layout-compatible
|
| 3375 |
+
types shall have the same value representation and alignment
|
| 3376 |
+
requirements ([[basic.align]]).
|
| 3377 |
+
|
| 3378 |
+
[*Note 3*: Pointers to over-aligned types ([[basic.align]]) have no
|
| 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 standard-layout union object and the other is a non-static
|
| 3386 |
+
data member of that object ([[class.union]]), or
|
| 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, the first base class subobject of that
|
| 3390 |
+
object ([[class.mem]]), or
|
| 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` ([[expr.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-qualified ([[basic.type.qualifier]]) or cv-unqualified
|
| 3404 |
`void` can be used to point to objects of unknown type. Such a pointer
|
| 3405 |
shall be able to hold any object pointer. An object of type cv `void*`
|
| 3406 |
shall have the same representation and alignment requirements as
|
|
|
|
| 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` ([[basic.types]]) has three
|
| 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]]) includes the *cv-qualifier*s specified in the
|
| 3418 |
*decl-specifier-seq* ([[dcl.spec]]), *declarator* (Clause
|
| 3419 |
[[dcl.decl]]), *type-id* ([[dcl.name]]), or *new-type-id* (
|
| 3420 |
[[expr.new]]) when the object is created.
|
| 3421 |
|
| 3422 |
- A *const object* is an object of type `const T` or a non-mutable
|
|
|
|
| 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 ([[basic.align]]).[^30]
|
| 3434 |
|
| 3435 |
A compound type ([[basic.compound]]) is not cv-qualified by the
|
| 3436 |
cv-qualifiers (if any) of the types from which it is compounded. Any
|
| 3437 |
+
cv-qualifiers applied to an array type affect the array element type (
|
| 3438 |
+
[[dcl.array]]).
|
| 3439 |
|
| 3440 |
See [[dcl.fct]] and [[class.this]] regarding function types that have
|
| 3441 |
*cv-qualifier*s.
|
| 3442 |
|
| 3443 |
There is a partial ordering on cv-qualifiers, so that a type can be said
|
|
|
|
| 3454 |
| no cv-qualifier | < | `const volatile` |
|
| 3455 |
| `const` | < | `const volatile` |
|
| 3456 |
| `volatile` | < | `const volatile` |
|
| 3457 |
|
| 3458 |
|
| 3459 |
+
In this International Standard, the notation cv (or *cv1*, *cv2*, etc.),
|
| 3460 |
+
used in the description of types, represents an arbitrary set of
|
| 3461 |
cv-qualifiers, i.e., one of {`const`}, {`volatile`}, {`const`,
|
| 3462 |
+
`volatile`}, or the empty set. For a type cv `T`, the *top-level
|
| 3463 |
+
cv-qualifiers* of that type are those denoted by cv.
|
| 3464 |
+
|
| 3465 |
+
[*Example 1*: The type corresponding to the *type-id* `const int&` has
|
| 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 |
+
Cv-qualifiers applied to an array type attach to the underlying element
|
| 3473 |
+
type, so the notation “cv `T`”, where `T` is an array type, refers to an
|
| 3474 |
+
array whose elements are so-qualified. An array type whose elements are
|
| 3475 |
+
cv-qualified is also considered to have the same cv-qualifications as
|
| 3476 |
+
its elements.
|
| 3477 |
+
|
| 3478 |
+
[*Example 2*:
|
| 3479 |
|
| 3480 |
``` cpp
|
| 3481 |
typedef char CA[5];
|
| 3482 |
typedef const char CC;
|
| 3483 |
CC arr1[5] = { 0 };
|
| 3484 |
const CA arr2 = { 0 };
|
| 3485 |
```
|
| 3486 |
|
| 3487 |
+
The type of both `arr1` and `arr2` is “array of 5 `const char`”, and the
|
| 3488 |
+
array type is considered to be const-qualified.
|
| 3489 |
+
|
| 3490 |
+
— *end example*]
|
| 3491 |
|
| 3492 |
## Lvalues and rvalues <a id="basic.lval">[[basic.lval]]</a>
|
| 3493 |
|
| 3494 |
Expressions are categorized according to the taxonomy in Figure
|
| 3495 |
[[fig:categories]].
|
| 3496 |
|
| 3497 |
<a id="fig:categories"></a>
|
| 3498 |
|
| 3499 |
![Expression category taxonomy \[fig:categories\]](images/valuecategories.svg)
|
| 3500 |
|
| 3501 |
+
- A *glvalue* is an expression whose evaluation determines the identity
|
| 3502 |
+
of an object, bit-field, or function.
|
| 3503 |
+
- A *prvalue* is an expression whose evaluation initializes an object or
|
| 3504 |
+
a bit-field, or computes the value of the operand of an operator, as
|
| 3505 |
+
specified by the context in which it appears.
|
| 3506 |
+
- An *xvalue* is a glvalue that denotes an object or bit-field whose
|
| 3507 |
+
resources can be reused (usually because it is near the end of its
|
| 3508 |
+
lifetime). \[*Example 1*: Certain kinds of expressions involving
|
| 3509 |
+
rvalue references ([[dcl.ref]]) yield xvalues, such as a call to a
|
| 3510 |
+
function whose return type is an rvalue reference or a cast to an
|
| 3511 |
+
rvalue reference type. — *end example*]
|
| 3512 |
+
- An *lvalue* is a glvalue that is not an xvalue.
|
| 3513 |
+
- An *rvalue* is a prvalue or an xvalue.
|
| 3514 |
+
|
| 3515 |
+
[*Note 1*: Historically, lvalues and rvalues were so-called because
|
| 3516 |
+
they could appear on the left- and right-hand side of an assignment
|
| 3517 |
+
(although this is no longer generally true); glvalues are “generalized”
|
| 3518 |
+
lvalues, prvalues are “pure” rvalues, and xvalues are “eXpiring”
|
| 3519 |
+
lvalues. Despite their names, these terms classify expressions, not
|
| 3520 |
+
values. — *end note*]
|
|
|
|
| 3521 |
|
| 3522 |
Every expression belongs to exactly one of the fundamental
|
| 3523 |
classifications in this taxonomy: lvalue, xvalue, or prvalue. This
|
| 3524 |
+
property of an expression is called its *value category*.
|
| 3525 |
+
|
| 3526 |
+
[*Note 2*: The discussion of each built-in operator in Clause [[expr]]
|
| 3527 |
+
indicates the category of the value it yields and the value categories
|
| 3528 |
+
of the operands it expects. For example, the built-in assignment
|
| 3529 |
+
operators expect that the left operand is an lvalue and that the right
|
| 3530 |
+
operand is a prvalue and yield an lvalue as the result. User-defined
|
| 3531 |
+
operators are functions, and the categories of values they expect and
|
| 3532 |
+
yield are determined by their parameter and return types. — *end note*]
|
| 3533 |
+
|
| 3534 |
+
The *result* of a prvalue is the value that the expression stores into
|
| 3535 |
+
its context. A prvalue whose result is the value *V* is sometimes said
|
| 3536 |
+
to have or name the value *V*. The *result object* of a prvalue is the
|
| 3537 |
+
object initialized by the prvalue; a prvalue that is used to compute the
|
| 3538 |
+
value of an operand of an operator or that has type cv `void` has no
|
| 3539 |
+
result object.
|
| 3540 |
+
|
| 3541 |
+
[*Note 3*: Except when the prvalue is the operand of a
|
| 3542 |
+
*decltype-specifier*, a prvalue of class or array type always has a
|
| 3543 |
+
result object. For a discarded prvalue, a temporary object is
|
| 3544 |
+
materialized; see Clause [[expr]]. — *end note*]
|
| 3545 |
+
|
| 3546 |
+
The *result* of a glvalue is the entity denoted by the expression.
|
| 3547 |
+
|
| 3548 |
+
[*Note 4*: Whenever a glvalue appears in a context where a prvalue is
|
| 3549 |
+
expected, the glvalue is converted to a prvalue; see [[conv.lval]],
|
| 3550 |
+
[[conv.array]], and [[conv.func]]. An attempt to bind an rvalue
|
| 3551 |
+
reference to an lvalue is not such a context; see
|
| 3552 |
+
[[dcl.init.ref]]. — *end note*]
|
| 3553 |
+
|
| 3554 |
+
[*Note 5*: There are no prvalue bit-fields; if a bit-field is converted
|
| 3555 |
+
to a prvalue ([[conv.lval]]), a prvalue of the type of the bit-field is
|
| 3556 |
+
created, which might then be promoted ([[conv.prom]]). — *end note*]
|
| 3557 |
+
|
| 3558 |
+
[*Note 6*: Whenever a prvalue appears in a context where a glvalue is
|
| 3559 |
+
expected, the prvalue is converted to an xvalue; see
|
| 3560 |
+
[[conv.rval]]. — *end note*]
|
| 3561 |
|
| 3562 |
The discussion of reference initialization in [[dcl.init.ref]] and of
|
| 3563 |
temporaries in [[class.temporary]] indicates the behavior of lvalues
|
| 3564 |
and rvalues in other significant contexts.
|
| 3565 |
|
| 3566 |
+
Unless otherwise indicated ([[expr.call]]), a prvalue shall always have
|
| 3567 |
+
complete type or the `void` type. A glvalue shall not have type
|
| 3568 |
+
cv `void`.
|
|
|
|
|
|
|
| 3569 |
|
| 3570 |
+
[*Note 7*: A glvalue may have complete or incomplete non-`void` type.
|
| 3571 |
+
Class and array prvalues can have cv-qualified types; other prvalues
|
| 3572 |
+
always have cv-unqualified types. See Clause [[expr]]. — *end note*]
|
|
|
|
| 3573 |
|
| 3574 |
+
An lvalue is *modifiable* unless its type is const-qualified or is a
|
| 3575 |
+
function type.
|
| 3576 |
|
| 3577 |
+
[*Note 8*: A program that attempts to modify an object through a
|
| 3578 |
+
nonmodifiable lvalue expression or through an rvalue expression is
|
| 3579 |
+
ill-formed ([[expr.ass]], [[expr.post.incr]],
|
| 3580 |
+
[[expr.pre.incr]]). — *end note*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3581 |
|
| 3582 |
If a program attempts to access the stored value of an object through a
|
| 3583 |
glvalue of other than one of the following types the behavior is
|
| 3584 |
+
undefined:[^31]
|
| 3585 |
|
| 3586 |
- the dynamic type of the object,
|
| 3587 |
- a cv-qualified version of the dynamic type of the object,
|
| 3588 |
- a type similar (as defined in [[conv.qual]]) to the dynamic type of
|
| 3589 |
the object,
|
|
|
|
| 3595 |
types among its elements or non-static data members (including,
|
| 3596 |
recursively, an element or non-static data member of a subaggregate or
|
| 3597 |
contained union),
|
| 3598 |
- a type that is a (possibly cv-qualified) base class type of the
|
| 3599 |
dynamic type of the object,
|
| 3600 |
+
- a `char`, `unsigned char`, or `std::byte` type.
|
| 3601 |
|
| 3602 |
## Alignment <a id="basic.align">[[basic.align]]</a>
|
| 3603 |
|
| 3604 |
Object types have *alignment requirements* ([[basic.fundamental]],
|
| 3605 |
[[basic.compound]]) which place restrictions on the addresses at which
|
|
|
|
| 3615 |
contexts, which is equal to `alignof(std::max_align_t)` (
|
| 3616 |
[[support.types]]). The alignment required for a type might be different
|
| 3617 |
when it is used as the type of a complete object and when it is used as
|
| 3618 |
the type of a subobject.
|
| 3619 |
|
| 3620 |
+
[*Example 1*:
|
| 3621 |
+
|
| 3622 |
``` cpp
|
| 3623 |
struct B { long double d; };
|
| 3624 |
+
struct D : virtual B { char c; };
|
| 3625 |
```
|
| 3626 |
|
| 3627 |
When `D` is the type of a complete object, it will have a subobject of
|
| 3628 |
type `B`, so it must be aligned appropriately for a `long double`. If
|
| 3629 |
`D` appears as a subobject of another object that also has `B` as a
|
| 3630 |
virtual base class, the `B` subobject might be part of a different
|
| 3631 |
+
subobject, reducing the alignment requirements on the `D` subobject.
|
| 3632 |
+
|
| 3633 |
+
— *end example*]
|
| 3634 |
+
|
| 3635 |
+
The result of the `alignof` operator reflects the alignment requirement
|
| 3636 |
+
of the type in the complete-object case.
|
| 3637 |
|
| 3638 |
An *extended alignment* is represented by an alignment greater than
|
| 3639 |
+
`alignof(std::max_align_t)`. It is *implementation-defined* whether any
|
| 3640 |
extended alignments are supported and the contexts in which they are
|
| 3641 |
supported ([[dcl.align]]). A type having an extended alignment
|
| 3642 |
+
requirement is an *over-aligned type*.
|
| 3643 |
+
|
| 3644 |
+
[*Note 1*: Every over-aligned type is or contains a class type to which
|
| 3645 |
+
extended alignment applies (possibly through a non-static data
|
| 3646 |
+
member). — *end note*]
|
| 3647 |
+
|
| 3648 |
+
A *new-extended alignment* is represented by an alignment greater than
|
| 3649 |
+
`__STDCPP_DEFAULT_NEW_ALIGNMENT__` ([[cpp.predefined]]).
|
| 3650 |
|
| 3651 |
Alignments are represented as values of the type `std::size_t`. Valid
|
| 3652 |
alignments include only those values returned by an `alignof` expression
|
| 3653 |
for the fundamental types plus an additional *implementation-defined*
|
| 3654 |
set of values, which may be empty. Every alignment value shall be a
|
|
|
|
| 3660 |
alignment requirement.
|
| 3661 |
|
| 3662 |
The alignment requirement of a complete type can be queried using an
|
| 3663 |
`alignof` expression ([[expr.alignof]]). Furthermore, the narrow
|
| 3664 |
character types ([[basic.fundamental]]) shall have the weakest
|
| 3665 |
+
alignment requirement.
|
| 3666 |
+
|
| 3667 |
+
[*Note 2*: This enables the narrow character types to be used as the
|
| 3668 |
+
underlying type for an aligned memory area (
|
| 3669 |
+
[[dcl.align]]). — *end note*]
|
| 3670 |
|
| 3671 |
Comparing alignments is meaningful and provides the obvious results:
|
| 3672 |
|
| 3673 |
- Two alignments are equal when their numeric values are equal.
|
| 3674 |
- Two alignments are different when their numeric values are not equal.
|
| 3675 |
- When an alignment is larger than another it represents a stricter
|
| 3676 |
alignment.
|
| 3677 |
|
| 3678 |
+
[*Note 3*: The runtime pointer alignment function ([[ptr.align]]) can
|
| 3679 |
+
be used to obtain an aligned pointer within a buffer; the
|
| 3680 |
+
aligned-storage templates in the library ([[meta.trans.other]]) can be
|
| 3681 |
+
used to obtain aligned storage. — *end note*]
|
| 3682 |
|
| 3683 |
If a request for a specific extended alignment in a specific context is
|
| 3684 |
not supported by an implementation, the program is ill-formed.
|
|
|
|
|
|
|
|
|
|
| 3685 |
|
| 3686 |
<!-- Link reference definitions -->
|
| 3687 |
[bad.alloc]: language.md#bad.alloc
|
| 3688 |
[basic]: #basic
|
| 3689 |
[basic.align]: #basic.align
|
|
|
|
| 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
|
| 3720 |
[basic.start.term]: #basic.start.term
|
| 3721 |
[basic.stc]: #basic.stc
|
| 3722 |
[basic.stc.auto]: #basic.stc.auto
|
| 3723 |
[basic.stc.dynamic]: #basic.stc.dynamic
|
| 3724 |
[basic.stc.dynamic.allocation]: #basic.stc.dynamic.allocation
|
|
|
|
| 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]: special.md#class.base.init
|
| 3735 |
+
[class.bit]: class.md#class.bit
|
| 3736 |
[class.cdtor]: special.md#class.cdtor
|
| 3737 |
[class.conv.fct]: special.md#class.conv.fct
|
| 3738 |
[class.copy]: special.md#class.copy
|
| 3739 |
[class.ctor]: special.md#class.ctor
|
| 3740 |
[class.derived]: class.md#class.derived
|
|
|
|
| 3761 |
[conv.lval]: conv.md#conv.lval
|
| 3762 |
[conv.mem]: conv.md#conv.mem
|
| 3763 |
[conv.prom]: conv.md#conv.prom
|
| 3764 |
[conv.ptr]: conv.md#conv.ptr
|
| 3765 |
[conv.qual]: conv.md#conv.qual
|
| 3766 |
+
[conv.rval]: conv.md#conv.rval
|
| 3767 |
+
[cpp.predefined]: cpp.md#cpp.predefined
|
| 3768 |
+
[cstddef.syn]: language.md#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 |
+
[depr.static_constexpr]: future.md#depr.static_constexpr
|
| 3792 |
[diff.cpp11.basic]: compatibility.md#diff.cpp11.basic
|
| 3793 |
[except]: except.md#except
|
| 3794 |
[except.handle]: except.md#except.handle
|
| 3795 |
[except.spec]: except.md#except.spec
|
| 3796 |
[except.terminate]: except.md#except.terminate
|
|
|
|
| 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.post.incr]: expr.md#expr.post.incr
|
| 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.pseudo]: expr.md#expr.pseudo
|
| 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 |
[fig:categories]: #fig:categories
|
| 3828 |
[headers]: library.md#headers
|
| 3829 |
[intro.execution]: intro.md#intro.execution
|
| 3830 |
[intro.memory]: intro.md#intro.memory
|
| 3831 |
[intro.multithread]: intro.md#intro.multithread
|
| 3832 |
[intro.object]: intro.md#intro.object
|
| 3833 |
+
[intro.races]: intro.md#intro.races
|
| 3834 |
[lex]: lex.md#lex
|
| 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
|
|
|
|
| 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]: language.md#new.delete
|
| 3846 |
+
[new.delete.array]: language.md#new.delete.array
|
| 3847 |
+
[new.delete.placement]: language.md#new.delete.placement
|
| 3848 |
+
[new.delete.single]: language.md#new.delete.single
|
| 3849 |
[new.handler]: language.md#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
|
|
|
|
| 3858 |
[set.new.handler]: language.md#set.new.handler
|
| 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 |
[stmt.select]: stmt.md#stmt.select
|
| 3867 |
[support.dynamic]: language.md#support.dynamic
|
| 3868 |
[support.limits]: language.md#support.limits
|
| 3869 |
[support.runtime]: language.md#support.runtime
|
| 3870 |
[support.start.term]: language.md#support.start.term
|
| 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
|
|
|
|
| 3915 |
`Y`’s definition or whether `X`’s definition appears in a namespace
|
| 3916 |
scope enclosing `Y`’s definition ([[class.nest]]).
|
| 3917 |
|
| 3918 |
[^7]: That is, an unqualified name that occurs, for instance, in a type
|
| 3919 |
in the *parameter-declaration-clause* or in the
|
| 3920 |
+
*noexcept-specifier*.
|
| 3921 |
|
| 3922 |
[^8]: This lookup applies whether the member function is defined within
|
| 3923 |
the definition of class `X` or whether the member function is
|
| 3924 |
defined in a namespace scope enclosing `X`’s definition.
|
| 3925 |
|
| 3926 |
[^9]: Lookups in which function names are ignored include names
|
| 3927 |
appearing in a *nested-name-specifier*, an
|
| 3928 |
*elaborated-type-specifier*, or a *base-specifier*.
|
| 3929 |
|
| 3930 |
+
[^10]: A class template has the linkage of the innermost enclosing class
|
| 3931 |
+
or namespace in which it is declared.
|
|
|
|
| 3932 |
|
| 3933 |
[^11]: A non-local variable with static storage duration having
|
| 3934 |
+
initialization with side effects is initialized in this case, even
|
| 3935 |
+
if it is not itself odr-used ([[basic.def.odr]],
|
| 3936 |
+
[[basic.stc.static]]).
|
| 3937 |
|
| 3938 |
+
[^12]: Some implementations might define that copying an invalid pointer
|
| 3939 |
+
value causes a system-generated runtime fault.
|
| 3940 |
+
|
| 3941 |
+
[^13]: The intent is to have `operator new()` implementable by calling
|
| 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 |
+
[^14]: The global `operator delete(void*, std::size_t)` precludes use of
|
| 3947 |
+
an allocation function `void operator new(std::size_t, std::size_t)`
|
| 3948 |
+
as a placement allocation function ([[diff.cpp11.basic]]).
|
|
|
|
|
|
|
|
|
|
| 3949 |
|
| 3950 |
[^15]: This section does not impose restrictions on indirection through
|
| 3951 |
pointers to memory not allocated by `::operator new`. This maintains
|
| 3952 |
the ability of many C++implementations to use binary libraries and
|
| 3953 |
components written in other languages. In particular, this applies
|
|
|
|
| 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]: `int` must also be large enough to contain any value in the range
|
| 3979 |
+
\[`INT_MIN`, `INT_MAX`\], as defined in the header `<climits>`.
|
| 3980 |
|
| 3981 |
[^23]: See [[dcl.type.simple]] regarding the correspondence between
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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 |
+
[^29]: For an object that is not within its lifetime, this is the first
|
| 4010 |
+
byte in memory that it will occupy or used to occupy.
|
| 4011 |
+
|
| 4012 |
+
[^30]: The same representation and alignment requirements are meant to
|
| 4013 |
imply interchangeability as arguments to functions, return values
|
| 4014 |
from functions, and non-static data members of unions.
|
| 4015 |
|
| 4016 |
+
[^31]: The intent of this list is to specify those circumstances in
|
| 4017 |
which an object may or may not be aliased.
|