「std::unique ptr」の版間の差分

提供: C++入門
移動: 案内検索
行38: 行38:
 
|unique_ptrを解放します。
 
|unique_ptrを解放します。
 
|-
 
|-
|operator=
+
|[[std::unique_ptr::operator=|operator=]]
 
|unique_ptr をアサインします。
 
|unique_ptr をアサインします。
 
|-
 
|-
|get
+
|[[std::unique_ptr::get|get]]
 
|ポインタを取得します
 
|ポインタを取得します
 
|-
 
|-
|get_deleter
+
|[[std::unique_ptr::get_deleter|get_deleter]]
 
|デリータを取得します
 
|デリータを取得します
 
|-
 
|-
行50: 行50:
 
|空でないかチェックします。
 
|空でないかチェックします。
 
|-
 
|-
|release
+
|[[std::unique_ptr::release|release]]
 
|ポインタの所有権をリリースします。
 
|ポインタの所有権をリリースします。
 
|-
 
|-
|reset
+
|[[std::unique_ptr::reset|reset]]
 
|ポインタをリセットします。
 
|ポインタをリセットします。
 
|-
 
|-
|swap
+
|[[std::unique_ptr::swap|swap]]
 
|ほかの[[std::unique_ptr]]のオブジェクトと内容をスワップします。
 
|ほかの[[std::unique_ptr]]のオブジェクトと内容をスワップします。
|-
 
|
 
|
 
 
|}
 
|}
  

2013年3月16日 (土) 14:47時点における版


std::unique_ptr は、C++11で規定されたスマートポインタです。

読み方

概要

std::unique_ptr は、動的に確保されたポインタを格納し、std::unique_ptr がスコープから外れたとき、メモリを delete します。

std::auto_ptr では、配列を扱えませんでした。 std::unique_ptr では、配列も扱えます。

std::unique_ptr<int> p1(new int(5));
 
std::unique_ptr<int> p2 = p1; // これは、コンパイルエラーになる。
 
std::unique_ptr<int> p3 = std::move(p1); // 所有権を移動する。p3は、メモリを所有する。p1は、無効になる。
 
p3.reset(); //メモリが delete される
p1.reset(); // なにもしません。

メンバ関数

メンバ関数
(constructor)
(destructor) unique_ptrを解放します。
operator= unique_ptr をアサインします。
get ポインタを取得します
get_deleter デリータを取得します
operator bool 空でないかチェックします。
release ポインタの所有権をリリースします。
reset ポインタをリセットします。
swap ほかのstd::unique_ptrのオブジェクトと内容をスワップします。


インストール

FreeBSDの場合は、新しいバージョン g++ が必要です。gcc48あたりをインストールしてください。

単純な例

ソースコード unique_ptr_1.cpp

#include <iostream>
#include <exception>
#include <memory>
 
class C {
        public:
                C() {
                }
                ~C() {
                        std::cout << __PRETTY_FUNCTION__ << std::endl;
                }
                void doit (){
                        std::cout << __PRETTY_FUNCTION__ << std::endl;
                }
};
 
void test_ptr() {
        try {
                std::unique_ptr<C> c(new C());
                c->doit();
        } catch (std::exception &ex) {
                std::cerr << ex.what () << std::endl;
        }
}
 
int
main(int argc, char const* argv[])
{
        std::cout << "call test_ptr" << std::endl;
        test_ptr ();
        std::cout << "end test_ptr" << std::endl;
 
        return 0;
}

コンパイル

FreeBSD

% g++48 --version |head -1
g++48 (FreeBSD Ports Collection) 4.8.0 20130210 (experimental)
% g++48 -std=c++11 unique_ptr_1.cpp -o unique_ptr_1

CentOS

$ g++ --version | head -1
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
$ g++ -std=c++0x    unique_ptr_1.cpp   -o unique_ptr_1

実行例

% ./unique_ptr_1
call test_ptr
void C::doit()
C::~C()
end test_ptr


配列を扱う例

ソースコード unique_ptr_array_1.cpp

#include <iostream>
#include <exception>
#include <memory>
 
class C {
        public:
                C() {
                }
                ~C() {
                        std::cout << __PRETTY_FUNCTION__ << std::endl;
                }
                void doit (){
                        std::cout << __PRETTY_FUNCTION__ << std::endl;
                }
};
 
void test_ptr() {
        try {
                std::unique_ptr<C[]> c(new C[3]);
        } catch (std::exception &ex) {
                std::cerr << ex.what () << std::endl;
        }
}
 
int
main(int argc, char const* argv[])
{
        std::cout << "call test_ptr" << std::endl;
        test_ptr ();
        std::cout << "end test_ptr" << std::endl;
 
        return 0;
}

コンパイル

g++48 -std=c++11    unique_ptr_array_1.cpp   -o unique_ptr_array_1

実行例

% ./unique_ptr_array_1
call test_ptr
C::~C()
C::~C()
C::~C()
end test_ptr


関連項目