std::unique_ptr
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Déclaré dans l'en-tête <memory>
|
||
template< class T, |
(1) | (depuis C++11) |
template < class T, |
(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éeOriginal: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 desiredThe 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) | |
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) | |
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) | |
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) | |
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) | |
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) | |
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) | |
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) | |
vérifie si un objet est actuellement géré (fonction membre publique) | |
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) | |
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) |
[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) | |
(C++11) |
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) |
[modifier] Classes d'aide
(C++11) |
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) |
[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 }