「std::unique ptr」の版間の差分
提供: C++入門
細 (Daemon がページ「Std unique ptr」を「Std::unique ptr」に、リダイレクトを残さずに移動しました) |
|||
| 行11: | 行11: | ||
== 概要 == | == 概要 == | ||
| + | [[std::auto_ptr]] では、配列を扱えませんでした。 | ||
| + | [[std::unique_ptr]] では、配列も扱えます。 | ||
== インストール == | == インストール == | ||
| 行16: | 行18: | ||
FreeBSDの場合は、新しいバージョン [[g++]] が必要です。gcc48あたりをインストールしてください。 | FreeBSDの場合は、新しいバージョン [[g++]] が必要です。gcc48あたりをインストールしてください。 | ||
| − | == | + | == 単純な例 == |
=== ソースコード unique_ptr_1.cpp === | === ソースコード unique_ptr_1.cpp === | ||
| 行37: | 行39: | ||
}; | }; | ||
| − | void | + | void test_ptr() { |
try { | try { | ||
std::unique_ptr<C> c(new C()); | std::unique_ptr<C> c(new C()); | ||
| 行49: | 行51: | ||
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; | ||
| 行79: | 行81: | ||
<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> | ||
| + | |||
| + | |||
| + | |||
| + | == 配列を扱う例 == | ||
| + | |||
| + | === ソースコード 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> | </syntaxhighlight> | ||
2013年3月11日 (月) 23:45時点における版
std::unique_ptr は、C++11で規定されたスマートポインタです。
読み方
目次
概要
std::auto_ptr では、配列を扱えませんでした。 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