Namespaces
Variants

swap (std::move_only_function)

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( 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 ;
(начиная с C++23)

Перегружает алгоритм std::swap для std::move_only_function . Обменивает состояние lhs с состоянием rhs . Фактически вызывает lhs. swap ( rhs ) .

Эта функция не видна при обычном unqualified или qualified lookup , и может быть найдена только с помощью argument-dependent lookup , когда 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); // находит скрытый friend
    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 объектов
(публичная функция-член)
специализирует алгоритм std::swap
(шаблон функции)