swap (std::move_only_function)
From cppreference.net
<
cpp
|
utility
|
functional
|
move only function
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Function objects
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
friend
void
swap
(
std::
move_only_function
&
lhs,
std::
move_only_function
&
rhs
)
noexcept
;
|
(自 C++23 起) | |
为 std::move_only_function 重载 std::swap 算法。交换 lhs 与 rhs 的状态。实际调用 lhs. swap ( rhs ) 。
此函数对普通的
非限定查找
或
限定查找
不可见,仅当
std::move_only_function<FunctionType>
作为参数的关联类时,通过
实参依赖查找
才能找到该函数。
目录 |
参数
| lhs, rhs | - |
要交换状态的
std::move_only_function
对象
|
返回值
(无)
示例
运行此代码
#include <concepts> #include <functional> #include <iostream> void foo(const char* str, int x) { std::cout << "foo(\"" << str << "\", " << x << ")\n"; } void bar(const char* str, int x) { std::cout << "bar(\"" << str << "\", " << x << ")\n"; } int main() { std::move_only_function<void(const char*, int) const> f1{foo}; std::move_only_function<void(const char*, int) const> f2{bar}; f1("f1", 1); f2("f2", 2); std::cout << "std::ranges::swap(f1, f2);\n"; std::ranges::swap(f1, f2); // 找到隐藏的友元函数 f1("f1", 1); f2("f2", 2); }
输出:
foo("f1", 1)
bar("f2", 2)
std::ranges::swap(f1, f2);
bar("f1", 1)
foo("f2", 2)
另请参阅
交换两个
std::move_only_function
对象的目标
(公开成员函数) |
|
|
(C++11)
|
特化
std::swap
算法
(函数模板) |