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

提供: C++入門
移動: 案内検索
(ページの作成:「<!-- vim: filetype=mediawiki --> std::unique_ptr は、C++11で規定されたスマートポインタです。 読み方 __TOC__ == 概要 == == イン...」)
 
(Daemon がページ「Std unique ptr」を「Std::unique ptr」に、リダイレクトを残さずに移動しました)
(相違点なし)

2013年3月10日 (日) 22:12時点における版


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

読み方

概要

インストール

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

unique_ptr_1.cpp の例

ソースコード 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_auto_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_auto_ptr" << std::endl;
        test_auto_ptr ();
        std::cout << "end test_auto_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_auto_ptr
void C::doit()
C::~C()
end test_auto_ptr


関連項目