Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

function::swap

 

Swap two callable objects.

void swap(function& right);

right

The function object to swap with.

The member function swaps the target objects between *this and right. It does so in constant time and throws no exceptions.

Example

 

// std_tr1__functional__function_swap.cpp 
// compile with: /EHsc 
#include <functional> 
#include <iostream> 

int neg(int val) 
    { 
    return (-val); 
    } 

int main() 
    { 
    std::function<int (int)> fn0(neg); 
    std::cout << std::boolalpha << "empty == " << !fn0 << std::endl; 
    std::cout << "val == " << fn0(3) << std::endl; 

    std::function<int (int)> fn1; 
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl; 
    std::cout << std::endl; 

    fn0.swap(fn1); 
    std::cout << std::boolalpha << "empty == " << !fn0 << std::endl; 
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl; 
    std::cout << "val == " << fn1(3) << std::endl; 

    return (0); 
    } 

empty == false
val == -3
empty == true

empty == true
empty == false
val == -3

Requirements

Header: <functional>

Namespace: std

Show:
© 2017 Microsoft