<new>
functions
get_new_handler
new_handler get_new_handler() noexcept;
Remarks
Returns the current new_handler
.
launder
template <class T>
constexpr T* launder(T* ptr) noexcept;
Parameters
ptr
The address of a byte in memory that holds an object whose type is similar to T.
Return Value
A value of type T* that points to X.
Remarks
Also referred to as a pointer optimization barrier.
Used as a constant expression when the value of its argument may be used in a constant expression. A byte of storage is reachable through a pointer value that points to an object if within the storage occupied by another object, an object with a similar pointer.
Example
struct X { const int n; };
X *p = new X{3};
const int a = p->n;
new (p) X{5}; // p does not point to new object because X::n is const
const int b = p->n; // undefined behavior
const int c = std::launder(p)->n; // OK
nothrow
Provides an object to be used as an argument for the nothrow
versions of new
and delete
.
extern const std::nothrow_t nothrow;
Remarks
The object is used as a function argument to match the parameter type std::nothrow_t.
Example
See operator new
and operator new[]
for examples of how std::nothrow_t
is used as a function parameter.
set_new_handler
Installs a user function that is to be called when operator new fails in its attempt to allocate memory.
new_handler set_new_handler(new_handler Pnew) throw();
Parameters
Pnew
The new_handler
to be installed.
Return Value
0 on the first call and the previous new_handler
on subsequent calls.
Remarks
The function stores Pnew
in a static new
handler pointer that it maintains, then returns the value previously stored in the pointer. The new
handler is used by operator new
.
Example
// new_set_new_handler.cpp
// compile with: /EHsc
#include<new>
#include<iostream>
using namespace std;
void __cdecl newhandler( )
{
cout << "The new_handler is called:" << endl;
throw bad_alloc( );
return;
}
int main( )
{
set_new_handler (newhandler);
try
{
while ( 1 )
{
new int[5000000];
cout << "Allocating 5000000 ints." << endl;
}
}
catch ( exception e )
{
cout << e.what( ) << endl;
}
}
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
Allocating 5000000 ints.
The new_handler is called:
bad allocation