std::unique_ptr::unique_ptr
De cppreference.com
|
|
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. |
<metanoindex/>
<tbody> </tbody> constexpr unique_ptr(); |
(1) | (depuis C++11) |
explicit unique_ptr( pointer p ); |
(2) | (depuis C++11) |
unique_ptr( pointer p, d1 ); |
(3) | (depuis C++11) |
unique_ptr( pointer p, d2 ); |
(4) | (depuis C++11) |
unique_ptr( unique_ptr&& u ); |
(5) | (depuis C++11) |
constexpr unique_ptr( nullptr_t ); |
(6) | (depuis C++11) |
template< class U, class E > unique_ptr( unique_ptr<U, E>&& u ); |
(7) | (depuis C++11) |
template< class U > unique_ptr( auto_ptr<U>&& u ); |
(8) | (depuis C++11) |
1)
Construit un
std::unique_ptr vide .Original:
Constructs an empty
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.
You can help to correct and verify the translation. Click here for instructions.
2)
Construit un
std::unique_ptr qui détient p, l'initialisation du pointeur stocké avec p et la valeur de l'initialisation du stockés deleter .Original:
Constructs a
std::unique_ptr which owns p, initializing the stored pointer with p and value-initializing the stored deleter.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
3-4)
Construit un objet qui possède
std::unique_ptr p, l'initialisation du pointeur stocké avec p et initialiser un D deleter comme ci-dessous (dépend si D est un type référence)Original:
Constructs a
std::unique_ptr object which owns p, initializing the stored pointer with p and initializing a deleter D as below (depends upon whether D is a reference type)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
a)
Si
D est A type non-référence, les signatures sont:Original:
If
D is non-reference type A, then the signatures are:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr(pointer p, const A& d);unique_ptr(pointer p, A&& d);
b)
Si
D est une lvalue de type A&-référence, les signatures sont:Original:
If
D is an lvalue-reference type A&, then the signatures are:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr(pointer p, A& d);unique_ptr(pointer p, A&& d);
c)
Si
D est une lvalue de type const A&-référence, les signatures sont:Original:
If
D is an lvalue-reference type const A&, then the signatures are:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
unique_ptr(pointer p, const A& d);unique_ptr(pointer p, const A&& d);
5)
Construit un
unique_ptr en transférant la propriété à partir de u *this .Original:
Constructs a
unique_ptr by transferring ownership from u to *this.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
6)
Construit un
std::unique_ptr vide .Original:
Constructs an empty
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.
You can help to correct and verify the translation. Click here for instructions.
7)
Construit un
unique_ptr par le transfert de propriété à partir de u *this, où u est réalisé avec un suppresseur spécifié (E). Cela dépend si E est un type référence, comme suit:Original:
Constructs a
unique_ptr by transferring ownership from u to *this, where u is constructed with a specified deleter (E). It depends upon whether E is a reference type, as following:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
a)
si
E est un type référence, cette deleter est construit à partir d'une copie de deleter u .Original:
if
E is a reference type, this deleter is copy constructed from u's deleter.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
b)
si
E est un type non-référence, ce suppresseur est construit à partir de déménagement suppresseur de u .Original:
if
E is a non-reference type, this deleter is move constructed from u's deleter.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
8)
Construit un
unique_ptr où le pointeur stocké est initialisé avec u.release() et le deleter est stocké valeur initialisée .Original:
Constructs a
unique_ptr where the stored pointer is initialized with u.release() and the stored deleter is value-initialized.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Paramètres
| p | - | un pointeur vers un objet à gérer
Original: a pointer to an object to manage The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| d1,d2 | - | un deleter à utiliser pour détruire l'objet
Original: a deleter to use to destroy the object The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| u | - | un autre pointeur intelligent d'acquérir la propriété de
Original: another smart pointer to acquire the ownership from The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Exceptions
Exemple
#include <iostream>
#include <memory>
struct Foo {
Foo() { std::cout << "Foo...\n"; }
~Foo() { std::cout << "~Foo...\n\n"; }
};
struct D{
D(){};
D(const D& other){
std::cout << "call D const copy constructor... \n";
}
D(D& other){
std::cout << "call D copy constructor... \n";
}
D(D&& other){
std::cout << "call D move constructor... \n";
}
void operator () (Foo* p) const {
std::cout << "Call delete for Foo object...\n";
delete p;
};
};
int main()
{
//constructor (1)
std::cout << "Example constructor(1)...\n\n";
std::unique_ptr<Foo> up;
//constructor (2)
std::cout << "Example constructor(2)...\n";
Foo* f = new Foo();
std::unique_ptr<Foo> up2(f); //up2 now owns f
up2.reset();
//constructor (3&4)
std::cout << "Example constructor(3&4)...\n";
//D is not an lvalue-reference - d is a non-const rvalue
std::unique_ptr<Foo, D> up3(new Foo(), D()); //D must be MoveConstructible
up3.reset();
//D is not an lvalue-refernce - d is a left value
D d2;
std::unique_ptr<Foo, D> up4(new Foo(), d2); //D must be Copyconstructible
up4.reset();
//D is a left value reference type
D d3;
std::unique_ptr<Foo, D&> up5(new Foo(), d3); //up3 holds a reference to d3
up5.reset();
//D is a const left value reference type
const D d4;
std::unique_ptr<Foo, const D&> up6(new Foo(), d4);
up6.reset();
//constructor (5)
std::cout << "Example constructor(5)...\n";
std::unique_ptr<Foo> up7(new Foo());
std::unique_ptr<Foo> up8(move(up7)); //ownership is transfered to up8
up8.reset();
//constructor 6
std::cout << "Example constructor(6)...\n\n";
std::unique_ptr<Foo> up9(nullptr);
//constructor 7 - D is move constructed
D d;
std::cout << "Example constructor(7)...\n";
std::unique_ptr<Foo, D> up10(new Foo(), d); //D is not a reference
std::unique_ptr<Foo, D> up11(move(up10)); //D is move constructed
up11.reset();
//constructor 7 - D is copy constructed
std::unique_ptr<Foo, D&> up12(new Foo(), d); //D is a reference
std::unique_ptr<Foo, D> up13(move(up12)); //D is copy constructed
up13.reset();
//constructor 8
std::cout << "Example constructor(8)...\n";
std::auto_ptr<Foo> up14(new Foo());
std::unique_ptr<Foo> up15(move(up14));
up15.reset();
}