名前空間
変種
操作

std::unique_ptr::unique_ptr

提供: cppreference.com
< cpp‎ | memory‎ | unique ptr

 
 
ユーティリティライブラリ
支援を入力します (basic types, RTTI, type traits)
動的なメモリ管理
エラー処理
プログラムユーティリティ
可変個引数関数
日付と時刻
関数オブジェクト
initializer_list(C++11)
bitset
hash(C++11)
関係演算子
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>=
ペアとタプル
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)
フォワード、スワップ、および移動
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)
 
動的なメモリ管理
低レベルのメモリ管理
アロケータ
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)
ストレージを初期化されていない
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
スマートポインタ
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(廃止予定)
owner_less(C++11)
enable_shared_from_this(C++11)
bad_weak_ptr(C++11)
default_delete(C++11)
ガベージコレクションをサポートしています
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)
その他
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ライブラリ
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に
メンバ関数
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=
修飾子
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
オブザーバー
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[]
非メンバ関数
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
 
constexpr unique_ptr();
(1) (C++11およびそれ以降)
explicit unique_ptr( pointer p );
(2) (C++11およびそれ以降)
unique_ptr( pointer p, d1 );
(3) (C++11およびそれ以降)
unique_ptr( pointer p, d2 );
(4) (C++11およびそれ以降)
unique_ptr( unique_ptr&& u );
(5) (C++11およびそれ以降)
constexpr unique_ptr( nullptr_t );
(6) (C++11およびそれ以降)
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );
(7) (C++11およびそれ以降)
template< class U >
unique_ptr( auto_ptr<U>&& u );
(8) (C++11およびそれ以降)
1)
空のstd::unique_ptrを構築し.
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.
2)
格納されているデリータを初期値-pとして格納されたポインタを初期化して、電話を所有しているstd::unique_ptrを構築し.
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.
3-4)
std::unique_ptrを所有pオブジェクトがpに格納されたポインタを初期化して(Dが参照型であるかどうかによって異なります)を以下のようにデリータのDを初期化し、構築します
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.
a)
Dは非参照型Aある場合、シグネチャは次のとおりです
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.
unique_ptr(pointer p, const A& d);
unique_ptr(pointer p, A&& d);
b)
Dが左辺値参照型のA&ある場合、シグネチャは次のとおりです
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.
unique_ptr(pointer p, A& d);
unique_ptr(pointer p, A&& d);
c)
Dが左辺値参照型のconst A&ある場合、シグネチャは次のとおりです
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.
unique_ptr(pointer p, const A& d);
unique_ptr(pointer p, const A&& d);
5)
unique_ptruからの譲渡により*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.
6)
空のstd::unique_ptrを構築し.
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.
7)
unique_ptrからuが指定デリータ(*this)で構成されてu、所有権を移すことによってEを構築します。それはEは、次のように参照型であるかどうかによって異なります
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.
a)
Eが参照型である場合、このデリータは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.
b)
Eは非参照型である場合、このデリータは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.
8)
格納されたポインタがunique_ptrで初期化されu.release()を構築し、格納されているデリータは、値に初期化された.
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.

[編集] パラメータ

p -
オブジェクトへのポインタは、管理する
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 -
デリータオブジェクトを破棄するために使用する
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 -
から所有権を取得する別のスマートポインタ
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.

[編集] 例外

noexcept specification:  
noexcept
  (C++11およびそれ以降)

[編集]

#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();
 
}