Namespaces
Variants
Views
Actions

std::unique_ptr::unique_ptr

From cppreference.com
< cpp‎ | memory‎ | unique ptr
Revision as of 14:33, 16 April 2014 by Cubbi (Talk | contribs)

 
 
 
 
 
constexpr unique_ptr();
(1) (since C++11)
constexpr unique_ptr( nullptr_t );
(1) (since C++11)
explicit unique_ptr( pointer p );
(2) (since C++11)
unique_ptr( pointer p, /* see below */ d1 );
(3) (since C++11)
unique_ptr( pointer p, /* see below */ d2 );
(4) (since C++11)
unique_ptr( unique_ptr&& u );
(5) (since C++11)
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );
(6) (since C++11)
template< class U >
unique_ptr( auto_ptr<U>&& u );
(7) (since C++11)
1) Constructs a std::unique_ptr that owns nothing. Value-initializes the stored pointer and the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.
2) Constructs a std::unique_ptr which owns p, initializing the stored pointer with p and value-initializing the stored deleter. Requires that Deleter is DefaultConstructible and that construction does not throw an exception.
3-4) 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)
a) If D is non-reference type A, then the signatures are:
unique_ptr(pointer p, const A& d); (requires that Deleter is nothrow-CopyConstructible)
unique_ptr(pointer p, A&& d); (requires that Deleter is nothrow-MoveConstructible)
b) If D is an lvalue-reference type A&, then the signatures are:
unique_ptr(pointer p, A& d);
unique_ptr(pointer p, A&& d);
c) If D is an lvalue-reference type const A&, then the signatures are:
unique_ptr(pointer p, const A& d);
unique_ptr(pointer p, const A&& d);
5) Constructs a unique_ptr by transferring ownership from u to *this. If Deleter is not a reference type, requires that it is nothrow-MoveConstructible (if Deleter is a reference, get_deleter() and u.get_deleter() after move construction reference the same value)
6) 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:
a) if E is a reference type, this deleter is copy constructed from u's deleter (requires that this construction does not throw)
b) if E is a non-reference type, this deleter is move constructed from u's deleter (requires that this construction does not throw)
This constructor only participates in overload resolution if all of the following is true:
a) unique_ptr<U, E>::pointer is implicitly convertible to pointer
b) U is not an array type
c) Either Deleter is a reference type and E is the same type as D, or Deleter is not a reference type and E is implicitly convertible to D
7) Constructs a unique_ptr where the stored pointer is initialized with u.release() and the stored deleter is value-initialized. This constructor only participates in overload resolution if U* is implicitly convertible to T* and Deleter is the same type as std::default_delete<T>.

Parameters

p - a pointer to an object to manage
d1,d2 - a deleter to use to destroy the object
u - another smart pointer to acquire the ownership from

Exceptions

noexcept specification:  
noexcept
  

Example

#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 type and d is a non-const rvalue expression
    std::unique_ptr<Foo, D> up3(new Foo, D()); // D must be MoveConstructible
    up3.reset();
 
    // D is not an lvalue reference type and d is an lvalue expression
    D d2;
    std::unique_ptr<Foo, D> up4(new Foo, d2); // D must be CopyConstructible
    up4.reset();
 
    // D is an lvalue reference type and d is an lvalue expression
    D d3;
    std::unique_ptr<Foo, D&> up5(new Foo, d3); // up5 holds a reference to d3
    up5.reset();
 
    // D is a const lvalue reference type and d is an lvalue expression
    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(std::move(up7)); // ownership is transferred 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(std::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(std::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();
}

Output:

Example constructor(1)...
 
Example constructor(2)...
Foo...
~Foo...
 
Example constructor(3&4)...
Foo...
call D move constructor... 
Call delete for Foo object...
~Foo...
 
Foo...
call D const copy constructor... 
Call delete for Foo object...
~Foo...
 
Foo...
Call delete for Foo object...
~Foo...
 
Foo...
Call delete for Foo object...
~Foo...
 
Example constructor(5)...
Foo...
~Foo...
 
Example constructor(6)...
 
Example constructor(7)...
Foo...
call D const copy constructor... 
call D move constructor... 
Call delete for Foo object...
~Foo...
 
Foo...
call D copy constructor... 
Call delete for Foo object...
~Foo...
 
Example constructor(8)...
Foo...
~Foo...