C++ 概念: ValueSwappable
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
可以解除引用此类型的两个对象,和所得到的值,可以在上下文中使用不合格的函数调用swap()交换两个std::swap和用户定义swap()s是可见.
Original:
Two objects of this type can be dereferenced and the resulting values can be swapped using unqualified function call swap() in the context where both std::swap and the user-defined swap()s are visible.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 要求
T型,
ValueSwappable如果Original:
Type T is
ValueSwappable ifThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
1)
T类型Iterator满足要求Original:
Type
T satisfies the Iterator requirementsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
2)
x的类型(即,结束迭代器以外的任何值)对于任何dereferencable的对象T,*x满足Swappable要求.Original:
For any dereferencable object
x of type T (that is, any value other than the end iterator), *x satisfies the Swappable requirements.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
许多标准库函数期望他们的论据,以满足
ValueSwappable,这意味着,任何时间进行交换的标准库,它使用的是等效的using std::swap; swap(*iter1, *iter2):.Original:
Many standard library functions expect their arguments to satisfy
ValueSwappable, which means that any time the standard library performs a swap, it uses the equivalent of using std::swap; swap(*iter1, *iter2):.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 为例
#include <iostream> #include <vector> class IntVector { std::vector<int> v; IntVector& operator=(IntVector); // not assignable public: 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 }