Share via


function::operator=

Replaces the stored callable object.

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);

Parameters

  • npc
    A null pointer constant.

  • right
    The function object to copy.

  • fn
    The callable object to wrap.

  • fnref
    The callable object reference to wrap.

Remarks

The operators each replace the callable object held by *this with the callable object passed as the operand.

Example

 

// 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); 
    } 
 
empty == false
val == -3
empty == true
empty == false
val == -3
empty == false
val == -3
empty == false
val == -3

Requirements

Header: <functional>

Namespace: std

See Also

Reference

function Class

function::function

function::swap