title | description | ms.date | f1_keywords | |||
---|---|---|---|---|---|---|
<new> functions | Learn more about: <new> functions | 11/04/2016 |
|
new_handler get_new_handler() noexcept;
Returns the current new_handler
.
template <classT> constexpr T* launder(T* ptr) noexcept;
ptr
The address of a byte in memory that holds an object whose type is similar to T.
A value of type T* that points to X.
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.
structX { constint n; }; X *p = new X{3}; constint a = p->n; new (p) X{5}; // p does not point to new object because X::n is constconstint b = p->n; // undefined behaviorconstint c = std::launder(p)->n; // OK
Provides an object to be used as an argument for the nothrow
versions of new
and delete
.
externconst std::nothrow_t nothrow;
The object is used as a function argument to match the parameter type std::nothrow_t.
See operator new
and operator new[]
for examples of how std::nothrow_t
is used as a function parameter.
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();
Pnew
The new_handler
to be installed.
0 on the first call and the previous new_handler
on subsequent calls.
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
.
// new_set_new_handler.cpp// compile with: /EHsc #include<new> #include<iostream>usingnamespacestd;void __cdecl newhandler( ) { cout << "The new_handler is called:" << endl; throwbad_alloc( ); return; } intmain( ) { set_new_handler (newhandler); try { while ( 1 ) { newint[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