Spec-Zone.ru › C++

std::function<R(Args...)>::swap

void swap( function& other ) noexcept;
(начиная с C++11)

Обменивает хранимые вызываемые объекты *this и other.

Параметры

other - обёртка функции, с которой следует обменять хранимый вызываемый объект

Возвращаемое значение

(отсутствует)

Пример

#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 << "f1.swap(f2);\n";
    f1.swap(f2);

    f1("f1", 1);
    f2("f2", 2);
}

Вывод:

foo("f1", 1)
bar("f2", 2)
f1.swap(f2);
bar("f1", 1)
foo("f2", 2)

См. также

swap
обменивает цели двух объектов std::move_only_function
(открытая функция-член std::move_only_function)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/utility/functional/function/swap

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API