C++函数对象-函数包装器-(std::function)(二)- 为内容赋值(std::function<R(Args...)>::operator=)

任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。


函数包装器

std::function 提供存储任意类型函数对象的支持。

包装具有指定函数调用签名的任意类型的可调用对象

类模板 std::function 是通用多态函数封装器。 std::function 的实例能存储、复制及调用任何可调用 (Callable) 目标——函数、 lambda 表达式、 bind 表达式或其他函数对象,还有指向成员函数指针和指向数据成员指针。

存储的可调用对象被称为 std::function目标。若 std::function 不含目标,则称它为。调用 std::function目标导致抛出 std::bad_function_call 异常。

std::function 满足可复制构造 (CopyConstructible) 和可复制赋值 (CopyAssignable) 。

成员类型

类型 定义
result_type R
argument_type(C++17 中弃用)(C++20 中移除) 若 sizeof...(Args)==1 且 TArgs... 中首个且唯一的类型,则为 T
first_argument_type(C++17 中弃用)(C++20 中移除) 若 sizeof...(Args)==2 且 T1Args... 中二个类型的第一个,则为 T1
second_argument_type(C++17 中弃用)(C++20 中移除) 若 sizeof...(Args)==2 且 T2Args... 中二个类型的第二个,则为 T2

为内容赋值

std::function::operator=

function& operator=( const function& other );

(1) (C++11 起)

function& operator=( function&& other );

(2) (C++11 起)

function& operator=( std::nullptr_t );

(3) (C++11 起)
(C++17 前)

function& operator=( std::nullptr_t ) noexcept;

(C++17 起)

template< class F >
function& operator=( F&& f );

(4) (C++11 起)

template< class F >
function& operator=( std::reference_wrapper f ) noexcept;

(5) (C++11 起)

赋值新目标std::function

1) 赋值 other目标副本,如同以执行 function(other).swap(*this);

2) 移动 other目标到 *this 。 other 在有未指定值的合法状态。

3) 舍弃当前目标。 *this 在调用后为

4) 设置 *this 的目标为可调用的 f ,如同以执行 function(std::forward(f)).swap(*this); 。此运算符不参与重载决议,除非 f 对于参数类型 Args... 和返回类型 R 可调用 (Callable) 。 (C++14 起)

5) 设置 *this 的目标f 的副本,如同以执行 function(f).swap(*this);

参数

other - 要复制其目标的另一 std::function 对象
f - 用以初始化目标的可调用对象
类型要求
- F 必须满足可调用 (Callable) 的要求。

返回值

*this

注意

虽然 C++17 中从 std::function 移除了以前的分配器支持,这些赋值运算符使用默认分配器,而不是 *thisother 的分配器(见 LWG #2386 )。

调用示例

#include 
#include 

int main()
{
    std::cout << std::boolalpha;
    std::function function1 = [](int num)
    {
        return num % 2 == 0;
    };
    //赋值新目标给 std::function 。
    //1) 赋值 other 的目标副本,如同以执行 function(other).swap(*this);
    std::function function2 = function1;
    std::cout << typeid(function2).name() << "  function2(1023): " << function2(1023) << std::endl;

    //2) 移动 other 的目标到 *this 。 other 在有未指定值的合法状态。
    std::function function3 = std::move(function2);
    std::cout << typeid(function3).name() << "  function3(1024): " << function3(1024) << std::endl;
    std::cout << typeid(function2).name() << "  function2 bool : " << (function2 ? true : false) << std::endl;
    std::cout << typeid(function3).name() << "  function3 bool : " << (function3 ? true : false) << std::endl;

    //3) 舍弃当前目标。 *this 在调用后为空。
    std::function function4 = std::nullptr_t(nullptr);
    std::cout << typeid(function4).name() << "  function4 bool : " << (function4 ? true : false) << std::endl;

    //4) 设置 *this 的目标为可调用的 f ,如同以执行 function(std::forward(f)).swap(*this); 。
    //此运算符不参与重载决议,除非 f 对于参数类型 Args... 和返回类型 R 可调用 (Callable) 。 (C++14 起)
    auto function5 = [](int num)
    {
        return num % 2 == 1;
    };
    std::cout << typeid(function5).name() << "  function5 bool : " << (function5 ? true : false) << std::endl;
    std::function function6 = std::move(function5);
    std::cout << typeid(function5).name() << "  function5 bool : " << (function5 ? true : false) << std::endl;
    std::cout << typeid(function6).name() << "  function6(1024): " << function6(1024) << std::endl;
    std::cout << typeid(function6).name() << "  function6 bool : " << (function6 ? true : false) << std::endl;

    //5) 设置 *this 的目标为 f 的副本,如同以执行 function(f).swap(*this);
    std::function function7 = std::reference_wrapper>(function6);
    std::cout << typeid(function7).name() << "  function7(1024): " << function7(1024) << std::endl;
    std::cout << typeid(function7).name() << "  function7 bool : " << (function7 ? true : false) << std::endl;

    return 0;
}

输出

St8functionIFbiEE  function2(1023): false
St8functionIFbiEE  function3(1024): true
St8functionIFbiEE  function2 bool : false
St8functionIFbiEE  function3 bool : true
St8functionIFbiEE  function4 bool : false
Z4mainEUliE0_  function5 bool : true
Z4mainEUliE0_  function5 bool : true
St8functionIFbiEE  function6(1024): false
St8functionIFbiEE  function6 bool : true
St8functionIFbiEE  function7(1024): false
St8functionIFbiEE  function7 bool : true

你可能感兴趣的:(#,std::function,c++,标准库模板,STL,函数对象,函数包装器,function)