名前空間
変種
操作

「cpp/memory/unique ptr/swap」の版間の差分

提供: cppreference.com
< cpp‎ | memory‎ | unique ptr
(r2.7.3) (ロボットによる 追加: de, en, es, fr, it, pt, ru, zh)
 
(1人の利用者による、間の2版が非表示)
1行: 1行:
{{tr_note}}
 
 
{{cpp/memory/unique_ptr/title|swap}}
 
{{cpp/memory/unique_ptr/title|swap}}
 
{{cpp/memory/unique_ptr/navbar}}
 
{{cpp/memory/unique_ptr/navbar}}
{{ddcl | notes={{mark since c++11}} | 1=
+
{{ddcl | sincec++11 | 1=
void swap(unique_ptr& other);
+
void swap(unique_ptr& other) ;
 
}}
 
}}
  
{{tr|スワップ別{{c|unique_ptr}}オブジェクトと管理対象オブジェクトと関連付けられたdeleters{{tt|other}}.|Swaps the managed objects and associated deleters with another {{c|unique_ptr}} object {{tt|other}}.}}
+
{{tt|}} {{c|unique_ptr}} {{tt|other}}
  
===パラメータ===
+
======
{{param list begin}}
+
{{begin}}
{{param list item | other |{{tr| 管理対象オブジェクトととデリータを交換するために別の{{c|unique_ptr}}オブジェクト| another {{c|unique_ptr}} object to swap the managed object and the deleter with}}}}
+
{{| other | {{c|unique_ptr}} オブジェクト }}
{{param list end}}
+
{{end}}
  
===値を返します===
+
======
{{tr|(なし)|(none)}}
+
()
 
+
===例外===
+
{{noexcept}}
+
  
 
===例===
 
===例===

2018年3月22日 (木) 05:10時点における最新版

 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
動的メモリ管理
スマートポインタ
(C++11)
(C++11)
(C++11)
(C++17未満)
(C++11)
アロケータ
メモリリソース
未初期化記憶域
ガベージコレクションサポート
その他
(C++20)
(C++11)
(C++11)
C のライブラリ
低水準のメモリ管理
 
 
void swap(unique_ptr& other) noexcept;
(C++11以上)

*this の管理対象オブジェクトおよび紐付けられているデリータを、別の unique_ptr オブジェクト other と入れ替えます。

[編集] 引数

other - 管理対象オブジェクトとデリータを入れ替える別の unique_ptr オブジェクト

[編集] 戻り値

(なし)

[編集]

#include <iostream>
#include <memory>
 
struct Foo {
    Foo(int _val) : val(_val) { std::cout << "Foo...\n"; }
    ~Foo() { std::cout << "~Foo...\n"; }
    int val;
};
 
int main()
{
    std::unique_ptr<Foo> up1(new Foo(1));
    std::unique_ptr<Foo> up2(new Foo(2));
 
    up1.swap(up2);
 
    std::cout << "up1->val:" << up1->val << std::endl;
    std::cout << "up2->val:" << up2->val << std::endl;
}

出力:

Foo...
Foo...
up1->val:2
up2->val:1
~Foo...
~Foo...