function::operator=

替换存储的可调用的对象。

function& operator=(null_ptr_type npc);
function& operator=(const function& right);
template<class Fty>
    function& operator=(Fty fn);
template<class Fty>
    function& operator=(reference_wrapper<Fty> fnref);

参数

  • npc
    一个空指针常数。

  • right
    要复制的函数对象。

  • fn
    要包装的可调用的对象。

  • fnref
    可调用参考中的对象包装。

备注

运算符每个将用作该操作数传递的可调用的对象替换 *this 保存的可调用的对象。

示例

 

// std_tr1__functional__function_operator_as.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; 
    fn1 = 0; 
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl; 
 
    fn1 = neg; 
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl; 
    std::cout << "val == " << fn1(3) << std::endl; 
 
    fn1 = fn0; 
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl; 
    std::cout << "val == " << fn1(3) << std::endl; 
 
    fn1 = std::cref(fn1); 
    std::cout << std::boolalpha << "empty == " << !fn1 << std::endl; 
    std::cout << "val == " << fn1(3) << std::endl; 
 
    return (0); 
    } 
 
  

要求

标头: <functional>

命名空间: std

请参见

参考

function Class

function::function

function::swap