std::swap(std::function)(3)
std::swap(std::function)(3) π
okay so this page is from the C++ Standard Libary manual β that's the manual name, spelling and all, no cap β and it's all about the std::swap overload for std::function. Bestie, it's literally a swap for your polymorphic function wrappers. π«Ά
β¨ NAME β¨
std::swap(std::function) - std::swap(std::function)
(yes, the name section is giving minimalist icon behavior. we respect it. π )
πΈ Synopsis πΈ
Defined in header <functional>
template< class R, class... Args >
void swap( std::function<R(Args...)>& lhs, std::function<R(Args...)>&
rhs ) noexcept;
(since C++11) β¨
Overloads the std::swap algorithm for std::function. Exchanges the state of lhs with that of rhs. Effectively calls lhs.swap(rhs).
π Parameters π
- lhs, rhs β polymorphic function wrappers whose states to swap
π Return value π
(none) β literally nothing, not a single value, it's giving void energy. π«Ά
π¬ Example π¬
// Run this code
#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::function<void(const char*, int)> f1{foo};
std::function<void(const char*, int)> f2{bar};
f1("f1", 1);
f2("f2", 2);
std::cout << "std::swap(f1, f2);\n";
std::swap(f1, f2);
f1("f1", 1);
f2("f2", 2);
}
π₯οΈ Output π₯οΈ
foo("f1", 1)
bar("f2", 2)
std::swap(f1, f2);
bar("f1", 1)
foo("f2", 2)
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 2062Β· Applied toC++11Β· Behavior as published: overload ofswapforfunctionwas not required to benoexceptΒ· Correct behavior: required
π See also π
swapβ swaps the contents (public member function)swap(std::move_only_function)(C++23)β overloads thestd::swapalgorithm (function)
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::swap(std::function)(3) πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms