swap(std::move_only_function)
From cppreference.com
< cpp | utility | functional | move only function
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
Function objects
(C++11)
(C++23)
(C++26)
(C++26)
(C++11)
(C++11)
(C++20)(C++23)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++20)(C++20)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
friend void swap( std::move_only_function & lhs, std::move_only_function & rhs ) noexcept;
(since C++23)
Overloads the std::swap algorithm for std::move_only_function. Exchanges the state of lhs with that of rhs. Effectively calls lhs.swap(rhs).
This function is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::move_only_function<FunctionType>
is an associated class of the arguments.
Contents
[edit] Parameters
lhs, rhs
-
std::move_only_function
objects whose states to swap
[edit] Return value
(none)
[edit] Example
Run this code
#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); // finds the hidden friend f1("f1", 1); f2("f2", 2); }
Output:
foo("f1", 1) bar("f2", 2) std::ranges::swap(f1, f2); bar("f1", 1) foo("f2", 2)