C++ の名前付き要件: ValueSwappable (C++11 以降)
From cppreference.com
この型の2つのオブジェクトは、間接参照することができ、その結果得られる値は、std::swap とユーザー定義の swap() の両方が可視であるコンテキストにおいて、非修飾関数呼び出し swap() を使用して入れ替えることができます。
[編集] 要件
型 T が ValueSwappable であるのは、
-
Tが LegacyIterator の要件を満たしている場合です。 - 型
Tの任意の非参照オブジェクトx(つまり、末尾イテレータ以外の任意の値) について、*xが Swappable の要件を満たしている場合です。
多くの標準ライブラリ関数は、引数が ValueSwappable を満たすことを期待しており、これは、標準ライブラリがスワップを実行するたびに、using std::swap; swap(*iter1, *iter2); と同等のものを使用することを意味します。
[編集] 例
このコードを実行
#include <iostream> #include <vector> class IntVector { std::vector<int> v; // IntVector& operator=(IntVector); // not assignable (C++98 way) public: IntVector& operator=(IntVector) = delete; // not assignable void swap(IntVector& other) { v.swap(other.v); } }; void swap(IntVector& v1, IntVector& v2) { v1.swap(v2); } int main() { IntVector v1, v2; // IntVector is Swappable, but not MoveAssignable IntVector* p1 = &v1; IntVector* p2 = &v2; // IntVector* is ValueSwappable std::iter_swap(p1, p2); // OK: iter_swap requires ValueSwappable // std::swap(v1, v2); // compiler error! std::swap requires MoveAssignable }
[編集] 関連項目
| (C++20) |
2つの indirectly_readable 型が参照する値を交換できることを規定する(コンセプト) |