funciones<new>

get_new_handler

new_handler get_new_handler() noexcept;

Comentarios

Devuelve el objeto actual new_handler.

launder

template <class T>
    constexpr T* launder(T* ptr) noexcept;

Parámetros

ptr
Dirección de un byte en la memoria que contiene un objeto cuyo tipo es similar a T.

Valor devuelto

Valor de tipo T* que apunta a X.

Comentarios

También se denomina como una barrera de optimización de puntero.

Se usa como una expresión constante cuando se puede usar el valor de su argumento en una expresión constante. Un byte de almacenamiento es accesible a través de un valor de puntero que apunta a un objeto si dentro del almacenamiento ocupado por otro objeto esta un objeto con un puntero similar.

Ejemplo

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

Proporciona un objeto que se usará como argumento para las versiones nothrow de new y delete.

extern const std::nothrow_t nothrow;

Comentarios

El objeto se usa como un argumento de función para que coincida con el tipo de parámetro std::nothrow_t.

Ejemplo

Consulte operator new y operator new[] para obtener ejemplos de cómo std::nothrow_t se usa como parámetro de función.

set_new_handler

Instala una función de usuario que se va a llamar cuando el nuevo operador no pueda asignar una memoria.

new_handler set_new_handler(new_handler Pnew) throw();

Parámetros

Pnew
El new_handler que se va a instalar.

Valor devuelto

0 en la primera llamada y el valor new_handler anterior en las llamadas posteriores.

Comentarios

La función almacena Pnew en un puntero del newcontrolador estático que mantiene y después devuelve el valor almacenado previamente en el puntero. El controlador new es usado por operator new.

Ejemplo

// 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