swap(std::m...ly_function)(3)
π NAME π
okay bestie, you're in the C++ Standard Libary manual, section 3, and today's page is swap(std::move_only_function). It's giving hidden-friend energy, and honestly? iconic. π
swap(std::move_only_function) β swap(std::move_only_function). periodt.
β¨ Synopsis β¨
Here's the declaration, character-for-character:
friend void swap( move_only_function& lhs, move_only_function& rhs )
(since C++23)
noexcept;
So what's the vibe? This overloads the std::swap algorithm for std::move_only_function. It exchanges the state of lhs with that of rhs. Effectively calls lhs.swap(rhs). slay. β¨
Real talk though: this function is not visible to ordinary unqualified or qualified lookup β nope, not even a little. It can only be found by argument-dependent lookup when std::move_only_function<FunctionType> is an associated class of the arguments. iykyk.
π«Ά Parameters π«Ά
lhs,rhsβstd::move_only_functionobjects whose states to swap.
π« Return value π«
(none) β literally nothing comes back, bestie. π«Ά
πΈ Example πΈ
Run this code, bestie:
// 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)
π See also π
swapβ swaps the targets of twostd::move_only_functionobjects (C++23) (public member function)std::swap(std::function)β specializes thestd::swapalgorithm (C++11) (function template)
kisses and noexcept vibes π β source/version: http://cppreference.com, date: 2024.06.10
π¦ 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