Espacios de nombres
Variantes
Acciones

std::make_shared

De cppreference.com
 
 
Biblioteca de servicios
 
Gestión de memoria dinámica
Punteros inteligentes
(C++11)
(C++11)
(C++11)
(hasta C++17)
(C++11)
(C++23)
Asignadores de memoria
Recursos de memoria
Almacenamiento no inicializado
Algoritmos de memoria no inicializada
Algoritmos restringidos de memoria no inicializada
Apoyo para recolección de basura
(C++11)(hasta C++23)
(C++11)(hasta C++23)
(C++11)(hasta C++23)
(C++11)(hasta C++23)
(C++11)(hasta C++23)
(C++11)(hasta C++23)
Misceláneos
(C++20)
(C++11)
(C++11)
 
 
Definido en el archivo de encabezado <memory>
template< class T, class... Args >
shared_ptr<T> make_shared( Args... args );
Construye un objeto de tipo T y lo envuelve en una std::shared_ptr utilizando args como la lista de parámetros del constructor de T .
Original:
Constructs an object of type T and wraps it in a std::shared_ptr using args as the parameter list for the constructor of T.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Contenido

Parámetros

args -
lista de argumentos con los que va a ser una instancia de T construidos .
Original:
list of arguments with which an instance of T will be constructed.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Valor de retorno

std::shared_ptr de una instancia de tipo T .
Original:
std::shared_ptr of an instance of type T.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Excepciones

Puede lanzar std::bad_alloc o cualquier excepción lanzada por el contructor de T .
Original:
May throw std::bad_alloc or any exception thrown by the contructor of T.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Notas

Esta función asigna memoria para el objeto T y para el bloque de control de la shared_ptr con una asignación de memoria. En contraste, la declaración std::shared_ptr<T> p(new T(Args...)) realiza dos asignaciones de memoria, que puede incurrir en una sobrecarga innecesaria .
Original:
This function allocates memory for the T object and for the shared_ptr's control block with a single memory allocation. In contrast, the declaration std::shared_ptr<T> p(new T(Args...)) performs two memory allocations, which may incur unnecessary overhead.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Ejemplo

#include <iostream>
#include <memory>
 
void foo(std::shared_ptr<int> i)
{
    (*i)++;
}
 
int main()
{
    auto sp = std::make_shared<int>(10);
    foo(sp);
    std::cout << *sp << std::endl;
}

Salida:

11

Ver también

Construye un nuevo shared_ptr.
(función miembro pública) [editar]
Crea un puntero compartido que gestiona un nuevo objeto asignado usando un asignador.
(plantilla de función) [editar]