「std::unique ptr」の版間の差分
提供: C++入門
細 (Daemon がページ「Std unique ptr」を「Std::unique ptr」に、リダイレクトを残さずに移動しました) |
(→関連項目) |
||
| (同じ利用者による、間の7版が非表示) | |||
| 行1: | 行1: | ||
| − | + | [[std::unique_ptr]] は、[[C++11]]で規定された[[スマートポインタ]]です。 | |
| − | + | ||
| − | + | ||
| − | std::unique_ptr | + | '''読み方''' |
| − | + | ;[[std::unique_ptr]]:えすてぃーでぃー ゆにーく ぴーてぃーあーる | |
| − | + | ||
__TOC__ | __TOC__ | ||
== 概要 == | == 概要 == | ||
| + | [[std::unique_ptr]] は、動的に確保されたポインタを格納し、[[std::unique_ptr]] がスコープから外れたとき、メモリを delete します。 | ||
| + | [[std::auto_ptr]] では、配列を扱えませんでした。[[std::unique_ptr]] では、配列も扱えます。 | ||
| + | <syntaxhighlight lang="cpp"> | ||
| + | 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(); // なにもしません。 | ||
| + | </syntaxhighlight> | ||
| − | + | [[std::move]]については、[[std::move]]をご参照ください。 | |
| + | == メンバ関数 == | ||
| + | {|class="wikitable" | ||
| + | |+ メンバ関数 | ||
| + | |- | ||
| + | |(constructor) | ||
| + | | | ||
| + | |- | ||
| + | |(destructor) | ||
| + | |unique_ptrを解放します。 | ||
| + | |- | ||
| + | |[[std::unique_ptr::operator=|operator=]] | ||
| + | |unique_ptr をアサインします。 | ||
| + | |- | ||
| + | |[[std::unique_ptr::get|get]] | ||
| + | |ポインタを取得します | ||
| + | |- | ||
| + | |[[std::unique_ptr::get_deleter|get_deleter]] | ||
| + | |デリータを取得します | ||
| + | |- | ||
| + | |operator bool | ||
| + | |空でないかチェックします。 | ||
| + | |- | ||
| + | |[[std::unique_ptr::release|release]] | ||
| + | |ポインタの所有権をリリースします。 | ||
| + | |- | ||
| + | |[[std::unique_ptr::reset|reset]] | ||
| + | |ポインタをリセットします。 | ||
| + | |- | ||
| + | |[[std::unique_ptr::swap|swap]] | ||
| + | |ほかの[[std::unique_ptr]]のオブジェクトと内容をスワップします。 | ||
| + | |} | ||
| + | |||
| + | |||
| + | == インストール == | ||
| + | FreeBSDの場合は、新しいバージョン [[g++]] が必要です。gcc48あたりをインストールしてください。 | ||
| + | |||
| + | == 単純な例 == | ||
| + | === ソースコード unique_ptr_1.cpp === | ||
<syntaxhighlight lang="cpp"> | <syntaxhighlight lang="cpp"> | ||
#include <iostream> | #include <iostream> | ||
| 行37: | 行79: | ||
}; | }; | ||
| − | void | + | void test_ptr() { |
try { | try { | ||
std::unique_ptr<C> c(new C()); | std::unique_ptr<C> c(new C()); | ||
| 行49: | 行91: | ||
main(int argc, char const* argv[]) | main(int argc, char const* argv[]) | ||
{ | { | ||
| − | std::cout << "call | + | std::cout << "call test_ptr" << std::endl; |
| − | + | test_ptr (); | |
| − | std::cout << "end | + | std::cout << "end test_ptr" << std::endl; |
return 0; | return 0; | ||
| 行58: | 行100: | ||
=== コンパイル === | === コンパイル === | ||
| − | |||
==== FreeBSD ==== | ==== FreeBSD ==== | ||
| − | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
% g++48 --version |head -1 | % g++48 --version |head -1 | ||
| 行68: | 行108: | ||
==== CentOS ==== | ==== CentOS ==== | ||
| − | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
$ g++ --version | head -1 | $ g++ --version | head -1 | ||
| 行76: | 行115: | ||
=== 実行例 === | === 実行例 === | ||
| − | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
% ./unique_ptr_1 | % ./unique_ptr_1 | ||
| − | call | + | call test_ptr |
void C::doit() | void C::doit() | ||
C::~C() | C::~C() | ||
| − | end | + | end test_ptr |
</syntaxhighlight> | </syntaxhighlight> | ||
| + | == 配列を扱う例 == | ||
| + | === ソースコード unique_ptr_array_1.cpp === | ||
| + | <syntaxhighlight lang="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; | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | === コンパイル === | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | g++48 -std=c++11 unique_ptr_array_1.cpp -o unique_ptr_array_1 | ||
| + | </syntaxhighlight> | ||
| + | === 実行例 === | ||
| + | <syntaxhighlight lang="bash"> | ||
| + | % ./unique_ptr_array_1 | ||
| + | call test_ptr | ||
| + | C::~C() | ||
| + | C::~C() | ||
| + | C::~C() | ||
| + | end test_ptr | ||
| + | </syntaxhighlight> | ||
| + | == 関連項目 == | ||
| + | * [[std::make_unique]] | ||
* [[スマートポインタ]] | * [[スマートポインタ]] | ||
* [[C++11]] | * [[C++11]] | ||
* [[C++ライブラリ]] | * [[C++ライブラリ]] | ||
| + | {{smartpointer}} | ||
| + | <!-- vim: filetype=mediawiki | ||
| + | --> | ||
2014年11月9日 (日) 16:28時点における最新版
std::unique_ptr は、C++11で規定されたスマートポインタです。
読み方
- std::unique_ptr
- えすてぃーでぃー ゆにーく ぴーてぃーあーる
目次
概要
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(); // なにもしません。
std::moveについては、std::moveをご参照ください。
メンバ関数
| (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