swap 関数 <functional>

2 つの function オブジェクトを交換します。

template<class Fty>
void swap(function<Fty>& f1,
    function<Fty>& f2);

パラメーター

  • Fty
    関数オブジェクトによって制御される型。

  • f1
    1 つ目の関数オブジェクト。

  • f2
    2 つ目の関数オブジェクト。

解説

この関数は、f1.swap(f2) を返します。

使用例

 

// std_tr1__functional__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; 
 
    swap(fn0, 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); 
    } 
 
  

必要条件

ヘッダー : <functional>

名前空間: std

参照

関連項目

function クラス