- tmp/tmpjn0i2zxd/{from.md → to.md} +152 -177
tmp/tmpjn0i2zxd/{from.md → to.md}
RENAMED
|
@@ -6,61 +6,75 @@ reporting storage management errors.
|
|
| 6 |
|
| 7 |
### Header `<new>` synopsis <a id="new.syn">[[new.syn]]</a>
|
| 8 |
|
| 9 |
``` cpp
|
| 10 |
namespace std {
|
|
|
|
| 11 |
class bad_alloc;
|
| 12 |
class bad_array_new_length;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
enum class align_val_t : size_t {};
|
|
|
|
| 14 |
struct nothrow_t { explicit nothrow_t() = default; };
|
| 15 |
extern const nothrow_t nothrow;
|
|
|
|
| 16 |
using new_handler = void (*)();
|
| 17 |
new_handler get_new_handler() noexcept;
|
| 18 |
new_handler set_new_handler(new_handler new_p) noexcept;
|
| 19 |
|
| 20 |
// [ptr.launder], pointer optimization barrier
|
| 21 |
-
template
|
| 22 |
|
| 23 |
// [hardware.interference], hardware interference size
|
| 24 |
inline constexpr size_t hardware_destructive_interference_size = implementation-defined{};
|
| 25 |
inline constexpr size_t hardware_constructive_interference_size = implementation-defined{};
|
| 26 |
}
|
| 27 |
|
| 28 |
-
|
| 29 |
-
void* operator new(std::size_t size
|
| 30 |
-
void* operator new(std::size_t size,
|
| 31 |
-
void* operator new(std::size_t size, std::
|
|
|
|
| 32 |
const std::nothrow_t&) noexcept;
|
|
|
|
| 33 |
void operator delete(void* ptr) noexcept;
|
| 34 |
void operator delete(void* ptr, std::size_t size) noexcept;
|
| 35 |
void operator delete(void* ptr, std::align_val_t alignment) noexcept;
|
| 36 |
void operator delete(void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
|
| 37 |
void operator delete(void* ptr, const std::nothrow_t&) noexcept;
|
| 38 |
-
void operator delete(void* ptr, std::align_val_t alignment,
|
| 39 |
-
|
| 40 |
-
void* operator new[](std::size_t size);
|
| 41 |
-
void* operator new[](std::size_t size, std::align_val_t alignment);
|
| 42 |
-
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
|
| 43 |
-
void* operator new[](std::size_t size, std::align_val_t alignment,
|
| 44 |
const std::nothrow_t&) noexcept;
|
|
|
|
| 45 |
void operator delete[](void* ptr) noexcept;
|
| 46 |
void operator delete[](void* ptr, std::size_t size) noexcept;
|
| 47 |
void operator delete[](void* ptr, std::align_val_t alignment) noexcept;
|
| 48 |
void operator delete[](void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
|
| 49 |
void operator delete[](void* ptr, const std::nothrow_t&) noexcept;
|
| 50 |
-
void operator delete[](void* ptr, std::align_val_t alignment,
|
| 51 |
-
const std::nothrow_t&) noexcept;
|
| 52 |
|
| 53 |
-
void* operator new (std::size_t size, void* ptr) noexcept;
|
| 54 |
-
void* operator new[](std::size_t size, void* ptr) noexcept;
|
| 55 |
void operator delete (void* ptr, void*) noexcept;
|
| 56 |
void operator delete[](void* ptr, void*) noexcept;
|
| 57 |
```
|
| 58 |
|
| 59 |
-
[[intro.memory]], [[basic.stc.dynamic]], [[expr.new]],
|
| 60 |
-
[[expr.delete]], [[class.free]], [[memory]].
|
| 61 |
-
|
| 62 |
### Storage allocation and deallocation <a id="new.delete">[[new.delete]]</a>
|
| 63 |
|
| 64 |
Except where otherwise specified, the provisions of
|
| 65 |
[[basic.stc.dynamic]] apply to the library versions of `operator new`
|
| 66 |
and `operator
|
|
@@ -68,50 +82,47 @@ delete`. If the value of an alignment argument passed to any of these
|
|
| 68 |
functions is not a valid alignment value, the behavior is undefined.
|
| 69 |
|
| 70 |
#### Single-object forms <a id="new.delete.single">[[new.delete.single]]</a>
|
| 71 |
|
| 72 |
``` cpp
|
| 73 |
-
void* operator new(std::size_t size);
|
| 74 |
-
void* operator new(std::size_t size, std::align_val_t alignment);
|
| 75 |
```
|
| 76 |
|
| 77 |
-
*Effects:* The allocation functions
|
| 78 |
-
called by a *new-expression*
|
| 79 |
storage. The second form is called for a type with new-extended
|
| 80 |
-
alignment, and
|
| 81 |
-
form is called otherwise, and allocates storage suitably aligned to
|
| 82 |
-
represent any object of that size provided the object’s type does not
|
| 83 |
-
have new-extended alignment.
|
| 84 |
|
| 85 |
*Replaceable:* A C++ program may define functions with either of these
|
| 86 |
function signatures, and thereby displace the default versions defined
|
| 87 |
by the C++ standard library.
|
| 88 |
|
| 89 |
*Required behavior:* Return a non-null pointer to suitably aligned
|
| 90 |
-
storage
|
| 91 |
This requirement is binding on any replacement versions of these
|
| 92 |
functions.
|
| 93 |
|
| 94 |
*Default behavior:*
|
| 95 |
|
| 96 |
- Executes a loop: Within the loop, the function first attempts to
|
| 97 |
allocate the requested storage. Whether the attempt involves a call to
|
| 98 |
the C standard library functions `malloc` or `aligned_alloc` is
|
| 99 |
unspecified.
|
| 100 |
- Returns a pointer to the allocated storage if the attempt is
|
| 101 |
-
successful. Otherwise, if the current
|
| 102 |
-
|
| 103 |
-
`bad_alloc`.
|
| 104 |
- Otherwise, the function calls the current `new_handler`
|
| 105 |
-
function
|
| 106 |
repeats.
|
| 107 |
- The loop terminates when an attempt to allocate the requested storage
|
| 108 |
is successful or when a called `new_handler` function does not return.
|
| 109 |
|
| 110 |
``` cpp
|
| 111 |
-
void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
|
| 112 |
-
void* operator new(std::size_t size, std::align_val_t alignment,
|
|
|
|
| 113 |
```
|
| 114 |
|
| 115 |
*Effects:* Same as above, except that these are called by a placement
|
| 116 |
version of a *new-expression* when a C++ program prefers a null pointer
|
| 117 |
result as an error indication, instead of a `bad_alloc` exception.
|
|
@@ -119,11 +130,11 @@ result as an error indication, instead of a `bad_alloc` exception.
|
|
| 119 |
*Replaceable:* A C++ program may define functions with either of these
|
| 120 |
function signatures, and thereby displace the default versions defined
|
| 121 |
by the C++ standard library.
|
| 122 |
|
| 123 |
*Required behavior:* Return a non-null pointer to suitably aligned
|
| 124 |
-
storage
|
| 125 |
these nothrow versions of `operator new` returns a pointer obtained as
|
| 126 |
if acquired from the (possibly replaced) corresponding non-placement
|
| 127 |
function. This requirement is binding on any replacement versions of
|
| 128 |
these functions.
|
| 129 |
|
|
@@ -146,13 +157,13 @@ void operator delete(void* ptr) noexcept;
|
|
| 146 |
void operator delete(void* ptr, std::size_t size) noexcept;
|
| 147 |
void operator delete(void* ptr, std::align_val_t alignment) noexcept;
|
| 148 |
void operator delete(void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
|
| 149 |
```
|
| 150 |
|
| 151 |
-
*Effects:* The deallocation
|
| 152 |
-
|
| 153 |
-
|
| 154 |
|
| 155 |
*Replaceable:* A C++ program may define functions with any of these
|
| 156 |
function signatures, and thereby displace the default versions defined
|
| 157 |
by the C++ standard library.
|
| 158 |
|
|
@@ -163,26 +174,26 @@ define the corresponding version without the `size` parameter.
|
|
| 163 |
|
| 164 |
[*Note 1*: The default behavior below may change in the future, which
|
| 165 |
will require replacing both deallocation functions when replacing the
|
| 166 |
allocation function. — *end note*]
|
| 167 |
|
| 168 |
-
*
|
| 169 |
-
|
| 170 |
-
|
| 171 |
`operator new(std::size_t, std::align_val_t)` which has not been
|
| 172 |
invalidated by an intervening call to `operator delete`.
|
| 173 |
|
| 174 |
-
*
|
| 175 |
-
safety
|
| 176 |
-
|
| 177 |
|
| 178 |
-
*
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
`
|
| 183 |
-
|
| 184 |
|
| 185 |
*Required behavior:* A call to an `operator delete` with a `size`
|
| 186 |
parameter may be changed to a call to the corresponding
|
| 187 |
`operator delete` without a `size` parameter, without affecting memory
|
| 188 |
allocation.
|
|
@@ -209,53 +220,48 @@ reclaimed storage will be allocated by subsequent calls to
|
|
| 209 |
``` cpp
|
| 210 |
void operator delete(void* ptr, const std::nothrow_t&) noexcept;
|
| 211 |
void operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept;
|
| 212 |
```
|
| 213 |
|
| 214 |
-
*Effects:* The deallocation
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
an exception.
|
| 219 |
|
| 220 |
*Replaceable:* A C++ program may define functions with either of these
|
| 221 |
function signatures, and thereby displace the default versions defined
|
| 222 |
by the C++ standard library.
|
| 223 |
|
| 224 |
-
*
|
| 225 |
-
|
| 226 |
-
|
| 227 |
`operator new(std::size_t, std::align_val_t)` which has not been
|
| 228 |
invalidated by an intervening call to `operator delete`.
|
| 229 |
|
| 230 |
-
*
|
| 231 |
-
safety
|
| 232 |
-
|
| 233 |
|
| 234 |
-
*
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
`ptr`.
|
| 239 |
|
| 240 |
*Default behavior:* Calls `operator delete(ptr)`, or
|
| 241 |
`operator delete(ptr, alignment)`, respectively.
|
| 242 |
|
| 243 |
#### Array forms <a id="new.delete.array">[[new.delete.array]]</a>
|
| 244 |
|
| 245 |
``` cpp
|
| 246 |
-
void* operator new[](std::size_t size);
|
| 247 |
-
void* operator new[](std::size_t size, std::align_val_t alignment);
|
| 248 |
```
|
| 249 |
|
| 250 |
-
*Effects:* The allocation functions
|
| 251 |
-
called by the array form of a *new-expression*
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
alignment. The first form is called otherwise, and allocates storage
|
| 255 |
-
suitably aligned to represent any array object of that size or smaller,
|
| 256 |
-
provided the object’s type does not have new-extended alignment. [^33]
|
| 257 |
|
| 258 |
*Replaceable:* A C++ program may define functions with either of these
|
| 259 |
function signatures, and thereby displace the default versions defined
|
| 260 |
by the C++ standard library.
|
| 261 |
|
|
@@ -265,12 +271,13 @@ functions.
|
|
| 265 |
|
| 266 |
*Default behavior:* Returns `operator new(size)`, or
|
| 267 |
`operator new(size, alignment)`, respectively.
|
| 268 |
|
| 269 |
``` cpp
|
| 270 |
-
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
|
| 271 |
-
void* operator new[](std::size_t size, std::align_val_t alignment,
|
|
|
|
| 272 |
```
|
| 273 |
|
| 274 |
*Effects:* Same as above, except that these are called by a placement
|
| 275 |
version of a *new-expression* when a C++ program prefers a null pointer
|
| 276 |
result as an error indication, instead of a `bad_alloc` exception.
|
|
@@ -278,11 +285,11 @@ result as an error indication, instead of a `bad_alloc` exception.
|
|
| 278 |
*Replaceable:* A C++ program may define functions with either of these
|
| 279 |
function signatures, and thereby displace the default versions defined
|
| 280 |
by the C++ standard library.
|
| 281 |
|
| 282 |
*Required behavior:* Return a non-null pointer to suitably aligned
|
| 283 |
-
storage
|
| 284 |
these nothrow versions of `operator new[]` returns a pointer obtained as
|
| 285 |
if acquired from the (possibly replaced) corresponding non-placement
|
| 286 |
function. This requirement is binding on any replacement versions of
|
| 287 |
these functions.
|
| 288 |
|
|
@@ -296,13 +303,13 @@ void operator delete[](void* ptr) noexcept;
|
|
| 296 |
void operator delete[](void* ptr, std::size_t size) noexcept;
|
| 297 |
void operator delete[](void* ptr, std::align_val_t alignment) noexcept;
|
| 298 |
void operator delete[](void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
|
| 299 |
```
|
| 300 |
|
| 301 |
-
*Effects:* The deallocation
|
| 302 |
-
|
| 303 |
-
|
| 304 |
|
| 305 |
*Replaceable:* A C++ program may define functions with any of these
|
| 306 |
function signatures, and thereby displace the default versions defined
|
| 307 |
by the C++ standard library.
|
| 308 |
|
|
@@ -313,26 +320,26 @@ define the corresponding version without the `size` parameter.
|
|
| 313 |
|
| 314 |
[*Note 1*: The default behavior below may change in the future, which
|
| 315 |
will require replacing both deallocation functions when replacing the
|
| 316 |
allocation function. — *end note*]
|
| 317 |
|
| 318 |
-
*
|
| 319 |
-
|
| 320 |
-
|
| 321 |
`operator new[](std::size_t, std::align_val_t)` which has not been
|
| 322 |
invalidated by an intervening call to `operator delete[]`.
|
| 323 |
|
| 324 |
-
*
|
| 325 |
-
safety
|
| 326 |
-
|
| 327 |
|
| 328 |
-
*
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
`
|
| 333 |
-
|
| 334 |
|
| 335 |
*Required behavior:* A call to an `operator delete[]` with a `size`
|
| 336 |
parameter may be changed to a call to the corresponding
|
| 337 |
`operator delete[]` without a `size` parameter, without affecting memory
|
| 338 |
allocation.
|
|
@@ -350,48 +357,46 @@ function.
|
|
| 350 |
``` cpp
|
| 351 |
void operator delete[](void* ptr, const std::nothrow_t&) noexcept;
|
| 352 |
void operator delete[](void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept;
|
| 353 |
```
|
| 354 |
|
| 355 |
-
*Effects:* The deallocation
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
throws an exception.
|
| 360 |
|
| 361 |
*Replaceable:* A C++ program may define functions with either of these
|
| 362 |
function signatures, and thereby displace the default versions defined
|
| 363 |
by the C++ standard library.
|
| 364 |
|
| 365 |
-
*
|
| 366 |
-
|
| 367 |
-
|
| 368 |
`operator new[](std::size_t, std::align_val_t)` which has not been
|
| 369 |
invalidated by an intervening call to `operator delete[]`.
|
| 370 |
|
| 371 |
-
*
|
| 372 |
-
safety
|
| 373 |
-
|
| 374 |
|
| 375 |
-
*
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
`ptr`.
|
| 380 |
|
| 381 |
*Default behavior:* Calls `operator delete[](ptr)`, or
|
| 382 |
`operator delete[](ptr, alignment)`, respectively.
|
| 383 |
|
| 384 |
#### Non-allocating forms <a id="new.delete.placement">[[new.delete.placement]]</a>
|
| 385 |
|
| 386 |
-
These functions are reserved; a C++program may not define functions
|
| 387 |
-
displace the versions in the C++standard library
|
| 388 |
-
provisions of [[basic.stc.dynamic]] do not apply to these reserved
|
| 389 |
placement forms of `operator new` and `operator delete`.
|
| 390 |
|
| 391 |
``` cpp
|
| 392 |
-
void* operator new(std::size_t size, void* ptr) noexcept;
|
| 393 |
```
|
| 394 |
|
| 395 |
*Returns:* `ptr`.
|
| 396 |
|
| 397 |
*Remarks:* Intentionally performs no other action.
|
|
@@ -406,11 +411,11 @@ Something* p = new (place) Something();
|
|
| 406 |
```
|
| 407 |
|
| 408 |
— *end example*]
|
| 409 |
|
| 410 |
``` cpp
|
| 411 |
-
void* operator new[](std::size_t size, void* ptr) noexcept;
|
| 412 |
```
|
| 413 |
|
| 414 |
*Returns:* `ptr`.
|
| 415 |
|
| 416 |
*Remarks:* Intentionally performs no other action.
|
|
@@ -419,132 +424,100 @@ void* operator new[](std::size_t size, void* ptr) noexcept;
|
|
| 419 |
void operator delete(void* ptr, void*) noexcept;
|
| 420 |
```
|
| 421 |
|
| 422 |
*Effects:* Intentionally performs no action.
|
| 423 |
|
| 424 |
-
*
|
| 425 |
-
safety
|
| 426 |
-
|
| 427 |
|
| 428 |
*Remarks:* Default function called when any part of the initialization
|
| 429 |
in a placement *new-expression* that invokes the library’s non-array
|
| 430 |
-
placement operator new terminates by throwing an
|
| 431 |
-
exception ([[expr.new]]).
|
| 432 |
|
| 433 |
``` cpp
|
| 434 |
void operator delete[](void* ptr, void*) noexcept;
|
| 435 |
```
|
| 436 |
|
| 437 |
*Effects:* Intentionally performs no action.
|
| 438 |
|
| 439 |
-
*
|
| 440 |
-
safety
|
| 441 |
-
|
| 442 |
|
| 443 |
*Remarks:* Default function called when any part of the initialization
|
| 444 |
in a placement *new-expression* that invokes the library’s array
|
| 445 |
-
placement operator new terminates by throwing an
|
| 446 |
-
exception ([[expr.new]]).
|
| 447 |
|
| 448 |
#### Data races <a id="new.delete.dataraces">[[new.delete.dataraces]]</a>
|
| 449 |
|
| 450 |
For purposes of determining the existence of data races, the library
|
| 451 |
versions of `operator new`, user replacement versions of global
|
| 452 |
`operator new`, the C standard library functions `aligned_alloc`,
|
| 453 |
`calloc`, and `malloc`, the library versions of `operator delete`, user
|
| 454 |
replacement versions of `operator delete`, the C standard library
|
| 455 |
function `free`, and the C standard library function `realloc` shall not
|
| 456 |
-
introduce a data race
|
| 457 |
that allocate or deallocate a particular unit of storage shall occur in
|
| 458 |
a single total order, and each such deallocation call shall happen
|
| 459 |
-
before
|
| 460 |
-
order.
|
| 461 |
|
| 462 |
### Storage allocation errors <a id="alloc.errors">[[alloc.errors]]</a>
|
| 463 |
|
| 464 |
#### Class `bad_alloc` <a id="bad.alloc">[[bad.alloc]]</a>
|
| 465 |
|
| 466 |
``` cpp
|
| 467 |
namespace std {
|
| 468 |
class bad_alloc : public exception {
|
| 469 |
public:
|
| 470 |
-
|
| 471 |
-
bad_alloc(const bad_alloc&) noexcept;
|
| 472 |
-
bad_alloc& operator=(const bad_alloc&) noexcept;
|
| 473 |
const char* what() const noexcept override;
|
| 474 |
};
|
| 475 |
}
|
| 476 |
```
|
| 477 |
|
| 478 |
The class `bad_alloc` defines the type of objects thrown as exceptions
|
| 479 |
by the implementation to report a failure to allocate storage.
|
| 480 |
|
| 481 |
-
``` cpp
|
| 482 |
-
bad_alloc() noexcept;
|
| 483 |
-
```
|
| 484 |
-
|
| 485 |
-
*Effects:* Constructs an object of class `bad_alloc`.
|
| 486 |
-
|
| 487 |
-
``` cpp
|
| 488 |
-
bad_alloc(const bad_alloc&) noexcept;
|
| 489 |
-
bad_alloc& operator=(const bad_alloc&) noexcept;
|
| 490 |
-
```
|
| 491 |
-
|
| 492 |
-
*Effects:* Copies an object of class `bad_alloc`.
|
| 493 |
-
|
| 494 |
``` cpp
|
| 495 |
const char* what() const noexcept override;
|
| 496 |
```
|
| 497 |
|
| 498 |
*Returns:* An *implementation-defined* NTBS.
|
| 499 |
|
| 500 |
-
*Remarks:* The message may be a null-terminated multibyte
|
| 501 |
-
string ([[multibyte.strings]]), suitable for conversion and display as
|
| 502 |
-
a `wstring` ([[string.classes]], [[locale.codecvt]]).
|
| 503 |
-
|
| 504 |
#### Class `bad_array_new_length` <a id="new.badlength">[[new.badlength]]</a>
|
| 505 |
|
| 506 |
``` cpp
|
| 507 |
namespace std {
|
| 508 |
class bad_array_new_length : public bad_alloc {
|
| 509 |
public:
|
| 510 |
-
|
| 511 |
const char* what() const noexcept override;
|
| 512 |
};
|
| 513 |
}
|
| 514 |
```
|
| 515 |
|
| 516 |
The class `bad_array_new_length` defines the type of objects thrown as
|
| 517 |
exceptions by the implementation to report an attempt to allocate an
|
| 518 |
array of size less than zero or greater than an *implementation-defined*
|
| 519 |
-
limit
|
| 520 |
-
|
| 521 |
-
``` cpp
|
| 522 |
-
bad_array_new_length() noexcept;
|
| 523 |
-
```
|
| 524 |
-
|
| 525 |
-
*Effects:* constructs an object of class `bad_array_new_length`.
|
| 526 |
|
| 527 |
``` cpp
|
| 528 |
const char* what() const noexcept override;
|
| 529 |
```
|
| 530 |
|
| 531 |
*Returns:* An *implementation-defined* NTBS.
|
| 532 |
|
| 533 |
-
*Remarks:* The message may be a null-terminated multibyte
|
| 534 |
-
string ([[multibyte.strings]]), suitable for conversion and display as
|
| 535 |
-
a `wstring` ([[string.classes]], [[locale.codecvt]]).
|
| 536 |
-
|
| 537 |
#### Type `new_handler` <a id="new.handler">[[new.handler]]</a>
|
| 538 |
|
| 539 |
``` cpp
|
| 540 |
using new_handler = void (*)();
|
| 541 |
```
|
| 542 |
|
| 543 |
The type of a *handler function* to be called by `operator new()` or
|
| 544 |
-
`operator new[]()`
|
| 545 |
-
|
| 546 |
|
| 547 |
*Required behavior:* A `new_handler` shall perform one of the following:
|
| 548 |
|
| 549 |
- make more storage available for allocation and then return;
|
| 550 |
- throw an exception of type `bad_alloc` or a class derived from
|
|
@@ -575,43 +548,45 @@ new_handler get_new_handler() noexcept;
|
|
| 575 |
[*Note 1*: This may be a null pointer value. — *end note*]
|
| 576 |
|
| 577 |
### Pointer optimization barrier <a id="ptr.launder">[[ptr.launder]]</a>
|
| 578 |
|
| 579 |
``` cpp
|
| 580 |
-
template
|
| 581 |
```
|
| 582 |
|
| 583 |
-
*
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
|
|
|
|
|
|
| 588 |
|
| 589 |
*Returns:* A value of type `T*` that points to `X`.
|
| 590 |
|
| 591 |
*Remarks:* An invocation of this function may be used in a core constant
|
| 592 |
expression whenever the value of its argument may be used in a core
|
| 593 |
-
constant expression. A byte of storage is reachable through a
|
| 594 |
-
value that points to an object *Y* if
|
| 595 |
-
|
| 596 |
-
immediately-enclosing array object if *
|
| 597 |
-
|
| 598 |
|
| 599 |
[*Note 1*: If a new object is created in storage occupied by an
|
| 600 |
existing object of the same type, a pointer to the original object can
|
| 601 |
-
be used to refer to the new object unless
|
| 602 |
-
|
| 603 |
-
obtain a usable pointer to the new object.
|
| 604 |
See [[basic.life]]. — *end note*]
|
| 605 |
|
| 606 |
[*Example 1*:
|
| 607 |
|
| 608 |
``` cpp
|
| 609 |
-
struct X {
|
| 610 |
-
X *p = new X{3};
|
| 611 |
const int a = p->n;
|
| 612 |
-
new (p) X{5};
|
| 613 |
const int b = p->n; // undefined behavior
|
| 614 |
const int c = std::launder(p)->n; // OK
|
| 615 |
```
|
| 616 |
|
| 617 |
— *end example*]
|
|
|
|
| 6 |
|
| 7 |
### Header `<new>` synopsis <a id="new.syn">[[new.syn]]</a>
|
| 8 |
|
| 9 |
``` cpp
|
| 10 |
namespace std {
|
| 11 |
+
// [alloc.errors], storage allocation errors
|
| 12 |
class bad_alloc;
|
| 13 |
class bad_array_new_length;
|
| 14 |
+
|
| 15 |
+
struct destroying_delete_t {
|
| 16 |
+
explicit destroying_delete_t() = default;
|
| 17 |
+
};
|
| 18 |
+
inline constexpr destroying_delete_t destroying_delete{};
|
| 19 |
+
|
| 20 |
+
// global operator new control%
|
| 21 |
+
%
|
| 22 |
+
%
|
| 23 |
+
%
|
| 24 |
+
%
|
| 25 |
+
|
| 26 |
enum class align_val_t : size_t {};
|
| 27 |
+
|
| 28 |
struct nothrow_t { explicit nothrow_t() = default; };
|
| 29 |
extern const nothrow_t nothrow;
|
| 30 |
+
|
| 31 |
using new_handler = void (*)();
|
| 32 |
new_handler get_new_handler() noexcept;
|
| 33 |
new_handler set_new_handler(new_handler new_p) noexcept;
|
| 34 |
|
| 35 |
// [ptr.launder], pointer optimization barrier
|
| 36 |
+
template<class T> [[nodiscard]] constexpr T* launder(T* p) noexcept;
|
| 37 |
|
| 38 |
// [hardware.interference], hardware interference size
|
| 39 |
inline constexpr size_t hardware_destructive_interference_size = implementation-defined{};
|
| 40 |
inline constexpr size_t hardware_constructive_interference_size = implementation-defined{};
|
| 41 |
}
|
| 42 |
|
| 43 |
+
// [new.delete], storage allocation and deallocation
|
| 44 |
+
[[nodiscard]] void* operator new(std::size_t size);
|
| 45 |
+
[[nodiscard]] void* operator new(std::size_t size, std::align_val_t alignment);
|
| 46 |
+
[[nodiscard]] void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
|
| 47 |
+
[[nodiscard]] void* operator new(std::size_t size, std::align_val_t alignment,
|
| 48 |
const std::nothrow_t&) noexcept;
|
| 49 |
+
|
| 50 |
void operator delete(void* ptr) noexcept;
|
| 51 |
void operator delete(void* ptr, std::size_t size) noexcept;
|
| 52 |
void operator delete(void* ptr, std::align_val_t alignment) noexcept;
|
| 53 |
void operator delete(void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
|
| 54 |
void operator delete(void* ptr, const std::nothrow_t&) noexcept;
|
| 55 |
+
void operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept;
|
| 56 |
+
|
| 57 |
+
[[nodiscard]] void* operator new[](std::size_t size);
|
| 58 |
+
[[nodiscard]] void* operator new[](std::size_t size, std::align_val_t alignment);
|
| 59 |
+
[[nodiscard]] void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
|
| 60 |
+
[[nodiscard]] void* operator new[](std::size_t size, std::align_val_t alignment,
|
| 61 |
const std::nothrow_t&) noexcept;
|
| 62 |
+
|
| 63 |
void operator delete[](void* ptr) noexcept;
|
| 64 |
void operator delete[](void* ptr, std::size_t size) noexcept;
|
| 65 |
void operator delete[](void* ptr, std::align_val_t alignment) noexcept;
|
| 66 |
void operator delete[](void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
|
| 67 |
void operator delete[](void* ptr, const std::nothrow_t&) noexcept;
|
| 68 |
+
void operator delete[](void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept;
|
|
|
|
| 69 |
|
| 70 |
+
[[nodiscard]] void* operator new (std::size_t size, void* ptr) noexcept;
|
| 71 |
+
[[nodiscard]] void* operator new[](std::size_t size, void* ptr) noexcept;
|
| 72 |
void operator delete (void* ptr, void*) noexcept;
|
| 73 |
void operator delete[](void* ptr, void*) noexcept;
|
| 74 |
```
|
| 75 |
|
|
|
|
|
|
|
|
|
|
| 76 |
### Storage allocation and deallocation <a id="new.delete">[[new.delete]]</a>
|
| 77 |
|
| 78 |
Except where otherwise specified, the provisions of
|
| 79 |
[[basic.stc.dynamic]] apply to the library versions of `operator new`
|
| 80 |
and `operator
|
|
|
|
| 82 |
functions is not a valid alignment value, the behavior is undefined.
|
| 83 |
|
| 84 |
#### Single-object forms <a id="new.delete.single">[[new.delete.single]]</a>
|
| 85 |
|
| 86 |
``` cpp
|
| 87 |
+
[[nodiscard]] void* operator new(std::size_t size);
|
| 88 |
+
[[nodiscard]] void* operator new(std::size_t size, std::align_val_t alignment);
|
| 89 |
```
|
| 90 |
|
| 91 |
+
*Effects:* The allocation functions [[basic.stc.dynamic.allocation]]
|
| 92 |
+
called by a *new-expression*[[expr.new]] to allocate `size` bytes of
|
| 93 |
storage. The second form is called for a type with new-extended
|
| 94 |
+
alignment, and the first form is called otherwise.
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
*Replaceable:* A C++ program may define functions with either of these
|
| 97 |
function signatures, and thereby displace the default versions defined
|
| 98 |
by the C++ standard library.
|
| 99 |
|
| 100 |
*Required behavior:* Return a non-null pointer to suitably aligned
|
| 101 |
+
storage [[basic.stc.dynamic]], or else throw a `bad_alloc` exception.
|
| 102 |
This requirement is binding on any replacement versions of these
|
| 103 |
functions.
|
| 104 |
|
| 105 |
*Default behavior:*
|
| 106 |
|
| 107 |
- Executes a loop: Within the loop, the function first attempts to
|
| 108 |
allocate the requested storage. Whether the attempt involves a call to
|
| 109 |
the C standard library functions `malloc` or `aligned_alloc` is
|
| 110 |
unspecified.
|
| 111 |
- Returns a pointer to the allocated storage if the attempt is
|
| 112 |
+
successful. Otherwise, if the current `new_handler`
|
| 113 |
+
[[get.new.handler]] is a null pointer value, throws `bad_alloc`.
|
|
|
|
| 114 |
- Otherwise, the function calls the current `new_handler`
|
| 115 |
+
function [[new.handler]]. If the called function returns, the loop
|
| 116 |
repeats.
|
| 117 |
- The loop terminates when an attempt to allocate the requested storage
|
| 118 |
is successful or when a called `new_handler` function does not return.
|
| 119 |
|
| 120 |
``` cpp
|
| 121 |
+
[[nodiscard]] void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
|
| 122 |
+
[[nodiscard]] void* operator new(std::size_t size, std::align_val_t alignment,
|
| 123 |
+
const std::nothrow_t&) noexcept;
|
| 124 |
```
|
| 125 |
|
| 126 |
*Effects:* Same as above, except that these are called by a placement
|
| 127 |
version of a *new-expression* when a C++ program prefers a null pointer
|
| 128 |
result as an error indication, instead of a `bad_alloc` exception.
|
|
|
|
| 130 |
*Replaceable:* A C++ program may define functions with either of these
|
| 131 |
function signatures, and thereby displace the default versions defined
|
| 132 |
by the C++ standard library.
|
| 133 |
|
| 134 |
*Required behavior:* Return a non-null pointer to suitably aligned
|
| 135 |
+
storage [[basic.stc.dynamic]], or else return a null pointer. Each of
|
| 136 |
these nothrow versions of `operator new` returns a pointer obtained as
|
| 137 |
if acquired from the (possibly replaced) corresponding non-placement
|
| 138 |
function. This requirement is binding on any replacement versions of
|
| 139 |
these functions.
|
| 140 |
|
|
|
|
| 157 |
void operator delete(void* ptr, std::size_t size) noexcept;
|
| 158 |
void operator delete(void* ptr, std::align_val_t alignment) noexcept;
|
| 159 |
void operator delete(void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
|
| 160 |
```
|
| 161 |
|
| 162 |
+
*Effects:* The deallocation functions [[basic.stc.dynamic.deallocation]]
|
| 163 |
+
called by a *delete-expression*[[expr.delete]] to render the value of
|
| 164 |
+
`ptr` invalid.
|
| 165 |
|
| 166 |
*Replaceable:* A C++ program may define functions with any of these
|
| 167 |
function signatures, and thereby displace the default versions defined
|
| 168 |
by the C++ standard library.
|
| 169 |
|
|
|
|
| 174 |
|
| 175 |
[*Note 1*: The default behavior below may change in the future, which
|
| 176 |
will require replacing both deallocation functions when replacing the
|
| 177 |
allocation function. — *end note*]
|
| 178 |
|
| 179 |
+
*Preconditions:* `ptr` is a null pointer or its value represents the
|
| 180 |
+
address of a block of memory allocated by an earlier call to a (possibly
|
| 181 |
+
replaced) `operator new(std::size_t)` or
|
| 182 |
`operator new(std::size_t, std::align_val_t)` which has not been
|
| 183 |
invalidated by an intervening call to `operator delete`.
|
| 184 |
|
| 185 |
+
*Preconditions:* If an implementation has strict pointer
|
| 186 |
+
safety [[basic.stc.dynamic.safety]] then `ptr` is a safely-derived
|
| 187 |
+
pointer.
|
| 188 |
|
| 189 |
+
*Preconditions:* If the `alignment` parameter is not present, `ptr` was
|
| 190 |
+
returned by an allocation function without an `alignment` parameter. If
|
| 191 |
+
present, the `alignment` argument is equal to the `alignment` argument
|
| 192 |
+
passed to the allocation function that returned `ptr`. If present, the
|
| 193 |
+
`size` argument is equal to the `size` argument passed to the allocation
|
| 194 |
+
function that returned `ptr`.
|
| 195 |
|
| 196 |
*Required behavior:* A call to an `operator delete` with a `size`
|
| 197 |
parameter may be changed to a call to the corresponding
|
| 198 |
`operator delete` without a `size` parameter, without affecting memory
|
| 199 |
allocation.
|
|
|
|
| 220 |
``` cpp
|
| 221 |
void operator delete(void* ptr, const std::nothrow_t&) noexcept;
|
| 222 |
void operator delete(void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept;
|
| 223 |
```
|
| 224 |
|
| 225 |
+
*Effects:* The deallocation functions [[basic.stc.dynamic.deallocation]]
|
| 226 |
+
called by the implementation to render the value of `ptr` invalid when
|
| 227 |
+
the constructor invoked from a nothrow placement version of the
|
| 228 |
+
*new-expression* throws an exception.
|
|
|
|
| 229 |
|
| 230 |
*Replaceable:* A C++ program may define functions with either of these
|
| 231 |
function signatures, and thereby displace the default versions defined
|
| 232 |
by the C++ standard library.
|
| 233 |
|
| 234 |
+
*Preconditions:* `ptr` is a null pointer or its value represents the
|
| 235 |
+
address of a block of memory allocated by an earlier call to a (possibly
|
| 236 |
+
replaced) `operator new(std::size_t)` or
|
| 237 |
`operator new(std::size_t, std::align_val_t)` which has not been
|
| 238 |
invalidated by an intervening call to `operator delete`.
|
| 239 |
|
| 240 |
+
*Preconditions:* If an implementation has strict pointer
|
| 241 |
+
safety [[basic.stc.dynamic.safety]] then `ptr` is a safely-derived
|
| 242 |
+
pointer.
|
| 243 |
|
| 244 |
+
*Preconditions:* If the `alignment` parameter is not present, `ptr` was
|
| 245 |
+
returned by an allocation function without an `alignment` parameter. If
|
| 246 |
+
present, the `alignment` argument is equal to the `alignment` argument
|
| 247 |
+
passed to the allocation function that returned `ptr`.
|
|
|
|
| 248 |
|
| 249 |
*Default behavior:* Calls `operator delete(ptr)`, or
|
| 250 |
`operator delete(ptr, alignment)`, respectively.
|
| 251 |
|
| 252 |
#### Array forms <a id="new.delete.array">[[new.delete.array]]</a>
|
| 253 |
|
| 254 |
``` cpp
|
| 255 |
+
[[nodiscard]] void* operator new[](std::size_t size);
|
| 256 |
+
[[nodiscard]] void* operator new[](std::size_t size, std::align_val_t alignment);
|
| 257 |
```
|
| 258 |
|
| 259 |
+
*Effects:* The allocation functions [[basic.stc.dynamic.allocation]]
|
| 260 |
+
called by the array form of a *new-expression*[[expr.new]] to allocate
|
| 261 |
+
`size` bytes of storage. The second form is called for a type with
|
| 262 |
+
new-extended alignment, and the first form is called otherwise. [^33]
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
*Replaceable:* A C++ program may define functions with either of these
|
| 265 |
function signatures, and thereby displace the default versions defined
|
| 266 |
by the C++ standard library.
|
| 267 |
|
|
|
|
| 271 |
|
| 272 |
*Default behavior:* Returns `operator new(size)`, or
|
| 273 |
`operator new(size, alignment)`, respectively.
|
| 274 |
|
| 275 |
``` cpp
|
| 276 |
+
[[nodiscard]] void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
|
| 277 |
+
[[nodiscard]] void* operator new[](std::size_t size, std::align_val_t alignment,
|
| 278 |
+
const std::nothrow_t&) noexcept;
|
| 279 |
```
|
| 280 |
|
| 281 |
*Effects:* Same as above, except that these are called by a placement
|
| 282 |
version of a *new-expression* when a C++ program prefers a null pointer
|
| 283 |
result as an error indication, instead of a `bad_alloc` exception.
|
|
|
|
| 285 |
*Replaceable:* A C++ program may define functions with either of these
|
| 286 |
function signatures, and thereby displace the default versions defined
|
| 287 |
by the C++ standard library.
|
| 288 |
|
| 289 |
*Required behavior:* Return a non-null pointer to suitably aligned
|
| 290 |
+
storage [[basic.stc.dynamic]], or else return a null pointer. Each of
|
| 291 |
these nothrow versions of `operator new[]` returns a pointer obtained as
|
| 292 |
if acquired from the (possibly replaced) corresponding non-placement
|
| 293 |
function. This requirement is binding on any replacement versions of
|
| 294 |
these functions.
|
| 295 |
|
|
|
|
| 303 |
void operator delete[](void* ptr, std::size_t size) noexcept;
|
| 304 |
void operator delete[](void* ptr, std::align_val_t alignment) noexcept;
|
| 305 |
void operator delete[](void* ptr, std::size_t size, std::align_val_t alignment) noexcept;
|
| 306 |
```
|
| 307 |
|
| 308 |
+
*Effects:* The deallocation functions [[basic.stc.dynamic.deallocation]]
|
| 309 |
+
called by the array form of a *delete-expression* to render the value of
|
| 310 |
+
`ptr` invalid.
|
| 311 |
|
| 312 |
*Replaceable:* A C++ program may define functions with any of these
|
| 313 |
function signatures, and thereby displace the default versions defined
|
| 314 |
by the C++ standard library.
|
| 315 |
|
|
|
|
| 320 |
|
| 321 |
[*Note 1*: The default behavior below may change in the future, which
|
| 322 |
will require replacing both deallocation functions when replacing the
|
| 323 |
allocation function. — *end note*]
|
| 324 |
|
| 325 |
+
*Preconditions:* `ptr` is a null pointer or its value represents the
|
| 326 |
+
address of a block of memory allocated by an earlier call to a (possibly
|
| 327 |
+
replaced) `operator new[](std::size_t)` or
|
| 328 |
`operator new[](std::size_t, std::align_val_t)` which has not been
|
| 329 |
invalidated by an intervening call to `operator delete[]`.
|
| 330 |
|
| 331 |
+
*Preconditions:* If an implementation has strict pointer
|
| 332 |
+
safety [[basic.stc.dynamic.safety]] then `ptr` is a safely-derived
|
| 333 |
+
pointer.
|
| 334 |
|
| 335 |
+
*Preconditions:* If the `alignment` parameter is not present, `ptr` was
|
| 336 |
+
returned by an allocation function without an `alignment` parameter. If
|
| 337 |
+
present, the `alignment` argument is equal to the `alignment` argument
|
| 338 |
+
passed to the allocation function that returned `ptr`. If present, the
|
| 339 |
+
`size` argument is equal to the `size` argument passed to the allocation
|
| 340 |
+
function that returned `ptr`.
|
| 341 |
|
| 342 |
*Required behavior:* A call to an `operator delete[]` with a `size`
|
| 343 |
parameter may be changed to a call to the corresponding
|
| 344 |
`operator delete[]` without a `size` parameter, without affecting memory
|
| 345 |
allocation.
|
|
|
|
| 357 |
``` cpp
|
| 358 |
void operator delete[](void* ptr, const std::nothrow_t&) noexcept;
|
| 359 |
void operator delete[](void* ptr, std::align_val_t alignment, const std::nothrow_t&) noexcept;
|
| 360 |
```
|
| 361 |
|
| 362 |
+
*Effects:* The deallocation functions [[basic.stc.dynamic.deallocation]]
|
| 363 |
+
called by the implementation to render the value of `ptr` invalid when
|
| 364 |
+
the constructor invoked from a nothrow placement version of the array
|
| 365 |
+
*new-expression* throws an exception.
|
|
|
|
| 366 |
|
| 367 |
*Replaceable:* A C++ program may define functions with either of these
|
| 368 |
function signatures, and thereby displace the default versions defined
|
| 369 |
by the C++ standard library.
|
| 370 |
|
| 371 |
+
*Preconditions:* `ptr` is a null pointer or its value represents the
|
| 372 |
+
address of a block of memory allocated by an earlier call to a (possibly
|
| 373 |
+
replaced) `operator new[](std::size_t)` or
|
| 374 |
`operator new[](std::size_t, std::align_val_t)` which has not been
|
| 375 |
invalidated by an intervening call to `operator delete[]`.
|
| 376 |
|
| 377 |
+
*Preconditions:* If an implementation has strict pointer
|
| 378 |
+
safety [[basic.stc.dynamic.safety]] then `ptr` is a safely-derived
|
| 379 |
+
pointer.
|
| 380 |
|
| 381 |
+
*Preconditions:* If the `alignment` parameter is not present, `ptr` was
|
| 382 |
+
returned by an allocation function without an `alignment` parameter. If
|
| 383 |
+
present, the `alignment` argument is equal to the `alignment` argument
|
| 384 |
+
passed to the allocation function that returned `ptr`.
|
|
|
|
| 385 |
|
| 386 |
*Default behavior:* Calls `operator delete[](ptr)`, or
|
| 387 |
`operator delete[](ptr, alignment)`, respectively.
|
| 388 |
|
| 389 |
#### Non-allocating forms <a id="new.delete.placement">[[new.delete.placement]]</a>
|
| 390 |
|
| 391 |
+
These functions are reserved; a C++ program may not define functions
|
| 392 |
+
that displace the versions in the C++ standard library [[constraints]].
|
| 393 |
+
The provisions of [[basic.stc.dynamic]] do not apply to these reserved
|
| 394 |
placement forms of `operator new` and `operator delete`.
|
| 395 |
|
| 396 |
``` cpp
|
| 397 |
+
[[nodiscard]] void* operator new(std::size_t size, void* ptr) noexcept;
|
| 398 |
```
|
| 399 |
|
| 400 |
*Returns:* `ptr`.
|
| 401 |
|
| 402 |
*Remarks:* Intentionally performs no other action.
|
|
|
|
| 411 |
```
|
| 412 |
|
| 413 |
— *end example*]
|
| 414 |
|
| 415 |
``` cpp
|
| 416 |
+
[[nodiscard]] void* operator new[](std::size_t size, void* ptr) noexcept;
|
| 417 |
```
|
| 418 |
|
| 419 |
*Returns:* `ptr`.
|
| 420 |
|
| 421 |
*Remarks:* Intentionally performs no other action.
|
|
|
|
| 424 |
void operator delete(void* ptr, void*) noexcept;
|
| 425 |
```
|
| 426 |
|
| 427 |
*Effects:* Intentionally performs no action.
|
| 428 |
|
| 429 |
+
*Preconditions:* If an implementation has strict pointer
|
| 430 |
+
safety [[basic.stc.dynamic.safety]] then `ptr` is a safely-derived
|
| 431 |
+
pointer.
|
| 432 |
|
| 433 |
*Remarks:* Default function called when any part of the initialization
|
| 434 |
in a placement *new-expression* that invokes the library’s non-array
|
| 435 |
+
placement operator new terminates by throwing an exception [[expr.new]].
|
|
|
|
| 436 |
|
| 437 |
``` cpp
|
| 438 |
void operator delete[](void* ptr, void*) noexcept;
|
| 439 |
```
|
| 440 |
|
| 441 |
*Effects:* Intentionally performs no action.
|
| 442 |
|
| 443 |
+
*Preconditions:* If an implementation has strict pointer
|
| 444 |
+
safety [[basic.stc.dynamic.safety]] then `ptr` is a safely-derived
|
| 445 |
+
pointer.
|
| 446 |
|
| 447 |
*Remarks:* Default function called when any part of the initialization
|
| 448 |
in a placement *new-expression* that invokes the library’s array
|
| 449 |
+
placement operator new terminates by throwing an exception [[expr.new]].
|
|
|
|
| 450 |
|
| 451 |
#### Data races <a id="new.delete.dataraces">[[new.delete.dataraces]]</a>
|
| 452 |
|
| 453 |
For purposes of determining the existence of data races, the library
|
| 454 |
versions of `operator new`, user replacement versions of global
|
| 455 |
`operator new`, the C standard library functions `aligned_alloc`,
|
| 456 |
`calloc`, and `malloc`, the library versions of `operator delete`, user
|
| 457 |
replacement versions of `operator delete`, the C standard library
|
| 458 |
function `free`, and the C standard library function `realloc` shall not
|
| 459 |
+
introduce a data race [[res.on.data.races]]. Calls to these functions
|
| 460 |
that allocate or deallocate a particular unit of storage shall occur in
|
| 461 |
a single total order, and each such deallocation call shall happen
|
| 462 |
+
before [[intro.multithread]] the next allocation (if any) in this order.
|
|
|
|
| 463 |
|
| 464 |
### Storage allocation errors <a id="alloc.errors">[[alloc.errors]]</a>
|
| 465 |
|
| 466 |
#### Class `bad_alloc` <a id="bad.alloc">[[bad.alloc]]</a>
|
| 467 |
|
| 468 |
``` cpp
|
| 469 |
namespace std {
|
| 470 |
class bad_alloc : public exception {
|
| 471 |
public:
|
| 472 |
+
// see [exception] for the specification of the special member functions
|
|
|
|
|
|
|
| 473 |
const char* what() const noexcept override;
|
| 474 |
};
|
| 475 |
}
|
| 476 |
```
|
| 477 |
|
| 478 |
The class `bad_alloc` defines the type of objects thrown as exceptions
|
| 479 |
by the implementation to report a failure to allocate storage.
|
| 480 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
``` cpp
|
| 482 |
const char* what() const noexcept override;
|
| 483 |
```
|
| 484 |
|
| 485 |
*Returns:* An *implementation-defined* NTBS.
|
| 486 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
#### Class `bad_array_new_length` <a id="new.badlength">[[new.badlength]]</a>
|
| 488 |
|
| 489 |
``` cpp
|
| 490 |
namespace std {
|
| 491 |
class bad_array_new_length : public bad_alloc {
|
| 492 |
public:
|
| 493 |
+
// see [exception] for the specification of the special member functions
|
| 494 |
const char* what() const noexcept override;
|
| 495 |
};
|
| 496 |
}
|
| 497 |
```
|
| 498 |
|
| 499 |
The class `bad_array_new_length` defines the type of objects thrown as
|
| 500 |
exceptions by the implementation to report an attempt to allocate an
|
| 501 |
array of size less than zero or greater than an *implementation-defined*
|
| 502 |
+
limit [[expr.new]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
|
| 504 |
``` cpp
|
| 505 |
const char* what() const noexcept override;
|
| 506 |
```
|
| 507 |
|
| 508 |
*Returns:* An *implementation-defined* NTBS.
|
| 509 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
#### Type `new_handler` <a id="new.handler">[[new.handler]]</a>
|
| 511 |
|
| 512 |
``` cpp
|
| 513 |
using new_handler = void (*)();
|
| 514 |
```
|
| 515 |
|
| 516 |
The type of a *handler function* to be called by `operator new()` or
|
| 517 |
+
`operator new[]()` [[new.delete]] when they cannot satisfy a request for
|
| 518 |
+
additional storage.
|
| 519 |
|
| 520 |
*Required behavior:* A `new_handler` shall perform one of the following:
|
| 521 |
|
| 522 |
- make more storage available for allocation and then return;
|
| 523 |
- throw an exception of type `bad_alloc` or a class derived from
|
|
|
|
| 548 |
[*Note 1*: This may be a null pointer value. — *end note*]
|
| 549 |
|
| 550 |
### Pointer optimization barrier <a id="ptr.launder">[[ptr.launder]]</a>
|
| 551 |
|
| 552 |
``` cpp
|
| 553 |
+
template<class T> [[nodiscard]] constexpr T* launder(T* p) noexcept;
|
| 554 |
```
|
| 555 |
|
| 556 |
+
*Mandates:* `!is_function_v<T> && !is_void_v<T>` is `true`.
|
| 557 |
+
|
| 558 |
+
*Preconditions:* `p` represents the address *A* of a byte in memory. An
|
| 559 |
+
object *X* that is within its lifetime [[basic.life]] and whose type is
|
| 560 |
+
similar [[conv.qual]] to `T` is located at the address *A*. All bytes of
|
| 561 |
+
storage that would be reachable through the result are reachable through
|
| 562 |
+
`p` (see below).
|
| 563 |
|
| 564 |
*Returns:* A value of type `T*` that points to `X`.
|
| 565 |
|
| 566 |
*Remarks:* An invocation of this function may be used in a core constant
|
| 567 |
expression whenever the value of its argument may be used in a core
|
| 568 |
+
constant expression. A byte of storage *b* is reachable through a
|
| 569 |
+
pointer value that points to an object *Y* if there is an object *Z*,
|
| 570 |
+
pointer-interconvertible with *Y*, such that *b* is within the storage
|
| 571 |
+
occupied by *Z*, or the immediately-enclosing array object if *Z* is an
|
| 572 |
+
array element.
|
| 573 |
|
| 574 |
[*Note 1*: If a new object is created in storage occupied by an
|
| 575 |
existing object of the same type, a pointer to the original object can
|
| 576 |
+
be used to refer to the new object unless its complete object is a const
|
| 577 |
+
object or it is a base class subobject; in the latter cases, this
|
| 578 |
+
function can be used to obtain a usable pointer to the new object.
|
| 579 |
See [[basic.life]]. — *end note*]
|
| 580 |
|
| 581 |
[*Example 1*:
|
| 582 |
|
| 583 |
``` cpp
|
| 584 |
+
struct X { int n; };
|
| 585 |
+
const X *p = new const X{3};
|
| 586 |
const int a = p->n;
|
| 587 |
+
new (const_cast<X*>(p)) const X{5}; // p does not point to new object [basic.life] because its type is const
|
| 588 |
const int b = p->n; // undefined behavior
|
| 589 |
const int c = std::launder(p)->n; // OK
|
| 590 |
```
|
| 591 |
|
| 592 |
— *end example*]
|