Espaces de noms
Variantes
Affichages
Actions

std::unique_ptr

De cppreference.com
< cpp‎ | memory


 
 
 
La gestion dynamique de la mémoire
Faible niveau de gestion de la mémoire
Répartiteurs
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 initialisée stockage
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
Pointeurs intelligents
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 (obsolète)
owner_less (C++11)
enable_shared_from_this (C++11)
bad_weak_ptr (C++11)
default_delete (C++11)
Soutien garbage collection
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)
Divers
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)
Bibliothèque C
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
Les fonctions membres
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=
Modificateurs
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
Des observateurs
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[]
Tiers fonctions
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
 
Déclaré dans l'en-tête <memory>
template<

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

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

    class T,
    class Deleter

> class unique_ptr<T[],Deleter>;
(2) (depuis C++11)

std::unique_ptr est un pointeur intelligent qui :

  • Garantit la propriété unique d'un objet à travers un pointeur, et
  • détruit l'objet pointé lorsque le unique_ptr devient inaccessible.

Unique_ptr n'est pas copiable ou assignable par copie, deux instances de unique_ptr ne peuvent pas gérer le même objet. Un unique_ptr non-constant peut transférer la propriété de l'objet géré à un autre unique_ptr. Un const std::unique_ptr ne peut pas être transféré, limitant ainsi la durée de vie de l'objet géré à la portée dans laquelle le pointeur a été créé. Lorsque le unique_ptr est détruit, il dispose de l'objet grâce à Deleter .


Il existe deux versions de std::unique_ptr :

1) gère la durée de vie d'un objet unique, par exemple alloué par new

2) gère la durée de vie d'un tableau avec une longueur spécifiée à l'exécution, par exemple, alloué par new []


Les utilisations classiques de std::unique_ptr peuvent être :

  • s'assurer que les objets alloués dynamiquement dans des fonctions ou des classes sont

bel et bien détruits lorsqu'ils deviennent inaccessibles, même lorsqu'une exception est levée. (exception safety)

  • la transmission de la propriété d'objets alloués dynamiquement à des fonctions.
  • l'acquisition de la propriété d'objets alloués dynamiquement depuis des fonctions.
  • que le type de l'élément en mouvement au courant des récipients, tels que std::vector, qui contiennent des pointeurs vers des objets alloués dynamiquement, par exemple si le comportement polymorphique est souhaitée
    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.

Sommaire

[modifier] Types membres

Type membre Définition
pointer std::remove_reference<D>::type::pointer si ce type existe, T* sinon
element_type T, le type de l'objet géré par cette unique_ptr
deleter_type Deleter, le foncteur ou une référence lvalue sur fonction ou sur le foncteur, à appeler par le destructeur

[modifier] Fonctions membres

construit un nouveau 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.

(fonction membre publique) [edit]
Détruit l'objet géré si tel est présente
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.

(fonction membre publique) [edit]
assigne la 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.

(fonction membre publique) [edit]
Modificateurs
renvoie un pointeur sur l'objet géré et libère la propriété
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.

(fonction membre publique) [edit]
remplace l'objet géré
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.

(fonction membre publique) [edit]
permute les objets gérés
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.

(fonction membre publique) [edit]
Accesseurs
renvoie un pointeur sur l'objet géré
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.

(fonction membre publique) [edit]
retourne le deleter qui est utilisé pour la destruction de l'objet géré
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.

(fonction membre publique) [edit]
vérifie si un objet est actuellement géré
(fonction membre publique) [edit]
Version à objet unique de unique_ptr <T>
déréférence le pointeur à l'objet géré
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.

(fonction membre publique) [edit]
Version tableau de unique_ptr <T[]>
fournit un accès indexé à la matrice gérée
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.

(fonction membre publique) [edit]

[modifier] Fonctions tiers

compare à un autre ou avec 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.

(fonction générique) [edit]
l'algorithme spécialisé 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.

(fonction générique) [edit]

[modifier] Classes d'aide

soutien de hachage pour 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 générique spécialisée) [edit]

[modifier] Exemple

#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 possède Foo
    if (p1) p1->bar();
 
    {
        std::unique_ptr<Foo> p2(std::move(p1));  // maintenant p2 possède Foo
        f(*p2);
 
        p1 = std::move(p2);  // la propriété retourne à p1
        std::cout << "destruction de p2...\n";
    }
 
    if (p1) p1->bar();
 
    // L'instance de Foo est détruite lorsque p1 devient inaccessible
}