Namespace
Varianti

std::unique_ptr

Da cppreference.com.
< cpp‎ | memory


 
 
Utilità libreria
Tipo di supporto (basic types, RTTI, type traits)
Gestione della memoria dinamica
La gestione degli errori
Programma di utilità
Funzioni variadic
Data e ora
Funzione oggetti
initializer_list(C++11)
bitset
hash(C++11)
Gli operatori relazionali
Original:
Relational operators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
rel_ops::operator!=
rel_ops::operator>
rel_ops::operator<=
rel_ops::operator>=
Coppie e tuple
Original:
Pairs and tuples
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pair
tuple(C++11)
piecewise_construct_t(C++11)
piecewise_construct(C++11)
Swap, in avanti e spostare
Original:
Swap, forward and move
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
swap
forward(C++11)
move(C++11)
move_if_noexcept(C++11)
declval(C++11)
 
Gestione della memoria dinamica
Basso livello di gestione della memoria
Allocatori
Original:
Allocators
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
allocator
allocator_traits(C++11)
allocator_arg_t(C++11)
allocator_arg(C++11)
uses_allocator(C++11)
scoped_allocator_adaptor(C++11)
Non inizializzata stoccaggio
Original:
Uninitialized storage
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
uninitialized_copy
uninitialized_copy_n(C++11)
uninitialized_fill
uninitialized_fill_n
raw_storage_iterator
get_temporary_buffer
return_temporary_buffer
Puntatori intelligenti
Original:
Smart pointers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr(C++11)
shared_ptr(C++11)
weak_ptr(C++11)
auto_ptr(deprecato)
owner_less(C++11)
enable_shared_from_this(C++11)
bad_weak_ptr(C++11)
default_delete(C++11)
Garbage collection supporto
Original:
Garbage collection support
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declare_reachable(C++11)
undeclare_reachable(C++11)
declare_no_pointers(C++11)
undeclare_no_pointers(C++11)
pointer_safety(C++11)
get_pointer_safety(C++11)
Varie
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
pointer_traits(C++11)
addressof(C++11)
align(C++11)
C Library
Original:
C Library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
std :: unique_ptr
Membri funzioni
Original:
Member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr::unique_ptr
unique_ptr::~unique_ptr
unique_ptr::operator=
Modificatori
Original:
Modifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr::release
unique_ptr::reset
unique_ptr::swap
Osservatori
Original:
Observers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr::get
unique_ptr::get_deleter
unique_ptr::operator bool
unique_ptr::operator*
unique_ptr::operator->
unique_ptr::operator[]
Non membri funzioni
Original:
Non-member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
operator==
operator!=
operator<
operator>
operator<=
operator>
operator>=
std::swap
std::hash
 
Defined in header <memory>
template<

    class T,
    class Deleter = std::default_delete<T>

> class unique_ptr;
(1) (dal C++11)
template <

    class T,
    class Deleter

> class unique_ptr<T[],Deleter>;
(2) (dal C++11)
std::unique_ptr è un puntatore intelligente che:
Original:
std::unique_ptr is a smart pointer that:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • conserva la proprietà esclusiva di un oggetto attraverso un puntatore, e
    Original:
    retains sole ownership of an object through a pointer, and
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • distrugge la punta-di opporsi quando il unique_ptr esce dall'ambito.
    Original:
    destroys the pointed-to object when the unique_ptr goes out of scope.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
unique_ptr non è copiabile o copia-assegnabili, due istanze di unique_ptr non può gestire lo stesso oggetto. Un non-const unique_ptr può trasferire la proprietà dell'oggetto gestito in un altro unique_ptr. Un const std::unique_ptr non può essere trasferito, limitando la durata dell'oggetto è riuscito a l'ambito in cui è stato creato il puntatore. Quando il unique_ptr è distrutto, dispone di dell'oggetto attraverso Deleter.
Original:
unique_ptr is not copyable or copy-assignable, two instances of unique_ptr cannot manage the same object. A non-const unique_ptr can transfer the ownership of the managed object to another unique_ptr. A const std::unique_ptr cannot be transferred, limiting the lifetime of the managed object to the scope in which the pointer was created. When the unique_ptr is destroyed, it disposes of the object through Deleter.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Ci sono due versioni di std::unique_ptr:
Original:
There are two versions of std::unique_ptr:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
gestisce la durata di un singolo oggetto, ad esempio assegnato con il nuovo
Original:
manages the lifetime of a single object, e.g. allocated with new
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
gestisce la durata di un array con una lunghezza di esecuzione, ad esempio allocato con new []
Original:
manages the lifetime of an array with a runtime length, e.g. allocated with new[]
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Gli usi tipici di std::unique_ptr includono
Original:
Typical uses of std::unique_ptr include
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • fornire sicurezza eccezione a classi e funzioni che gestiscono gli oggetti con tempo di vita dinamico, garantendo l'eliminazione sia uscita normale e l'uscita attraverso eccezione.
    Original:
    providing exception safety to classes and functions that handle objects with dynamic lifetime, by guaranteeing deletion on both normal exit and exit through exception.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • passando la proprietà degli oggetti di proprietà unico con durata di vita dinamico in funzioni
    Original:
    passing ownership of uniquely-owned objects with dynamic lifetime into functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • acquisire la proprietà degli oggetti di proprietà unico con durata di vita dinamico dalle funzioni
    Original:
    acquiring ownership of uniquely-owned objects with dynamic lifetime from functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • come il tipo di elemento in movimento-aware contenitori, come std::vector, che contengono puntatori a oggetti allocati dinamicamente, ad esempio Se il comportamento polimorfico si desidera
    Original:
    as the element type in move-aware containers, such as std::vector, which hold pointers to dynamically-allocated objects, e.g. if polymorphic behavior is desired
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Indice

[modifica] Membri tipi

Membro tipo
Original:
Member type
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Definition
pointer
std::remove_reference<D>::type::pointer se quel tipo esiste, altrimenti T *
Original:
std::remove_reference<D>::type::pointer if that type exists, otherwise T*
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
element_type
T, il tipo di oggetto gestito da questo unique_ptr
Original:
T, the type of the object managed by this unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
deleter_type
Deleter, l'oggetto funzione o il riferimento lvalue di funzionare o per oggetto funzione, di essere chiamato dal distruttore
Original:
Deleter, the function object or lvalue reference to function or to function object, to be called from the destructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[modifica] Membri funzioni

costruisce un nuovo unique_ptr
Original:
constructs a new unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
distrugge l'oggetto gestito se questo è presente
Original:
destructs the managed object if such is present
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
assegna il unique_ptr
Original:
assigns the unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
Modificatori
Original:
Modifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
restituisce un puntatore all'oggetto gestito e rilascia la proprietà
Original:
returns a pointer to the managed object and releases the ownership
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
sostituisce l'oggetto gestito
Original:
replaces the managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
swap gli oggetti gestiti
Original:
swaps the managed objects
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
Osservatori
Original:
Observers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
restituisce un puntatore all'oggetto gestito
Original:
returns a pointer to the managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
restituisce il deleter che viene utilizzato per la distruzione dell'oggetto gestito
Original:
returns the deleter that is used for destruction of the managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
controlla se è associato oggetto gestito
Original:
checks if there is associated managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
Singolo oggetto versione, unique_ptr <T>
Original:
Single-object version, unique_ptr<T>
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
dereferenzia puntatore all'oggetto gestito
Original:
dereferences pointer to the managed object
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]
Array versione, unique_ptr <T[]>
Original:
Array version, unique_ptr<T[]>
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
fornisce l'accesso indicizzato alla matrice gestita
Original:
provides indexed access to the managed array
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(membro pubblico funzione) [edit]

[modifica] Non membri funzioni

confronto ad un altro o con unique_ptr nullptr
Original:
compares to another unique_ptr or with nullptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(funzione di modello) [edit]
specializzata l'algoritmo std::swap
Original:
specializes the std::swap algorithm
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(funzione di modello) [edit]

[modifica] Helper classi

sostegno hash per std::unique_ptr
Original:
hash support for std::unique_ptr
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(classe modello di specializzazione) [edit]

[modifica] Esempio

#include <iostream>
#include <memory>
 
struct Foo {
    Foo() { std::cout << "Foo::Foo\n"; }
    ~Foo() { std::cout << "Foo::~Foo\n"; }
    void bar() { std::cout << "Foo::bar\n"; }
};
 
void f(const Foo &foo)
{
    std::cout << "f(const Foo&)\n";
}
 
int main()
{
    std::unique_ptr<Foo> p1(new Foo);  // p1 owns Foo
    if (p1) p1->bar();
 
    {
        std::unique_ptr<Foo> p2(std::move(p1));  // now p2 owns Foo
        f(*p2);
 
        p1 = std::move(p2);  // ownership returns to p1
        std::cout << "destroying p2...\n";
    }
 
    if (p1) p1->bar();
 
    // Foo instance is destroyed when p1 goes out of scope
}

Output:

Foo::Foo
Foo::bar
f(const Foo&)
destroying p2...
Foo::bar
Foo::~Foo