C++函数对象-函数包装器-(std::function)(四)- 检查是否包含了有效的目标 - 调用其目标

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


函数包装器

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

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

std::function

template< class >
class function; /* 不定义 */

(C++11 起)

template< class R, class... Args >
class function<R(Args...)>;

(C++11 起)

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

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

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

成员类型

类型定义
result_typeR
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<R(Args...)>::operator bool

explicit operator bool() const noexcept;

(C++11 起)

检查 *this 是否存储可调用函数对象,即非空。

参数

(无)

返回值

若 *this 存储可调用函数对象则为 true ,否则为 false 。

调用示例

#include <iostream>
#include <functional>

bool function(int num)
{
    return num % 2 == 1;
}

int main()
{
    std::cout << std::boolalpha;
    //检查 *this 是否存储可调用函数对象,即非空。
    std::function<bool(int)> function1;
    std::cout << typeid(function1).name() << "  function1 bool : " << (function1 ? true : false) << std::endl;

    std::function<bool(int)> function2 = [](int num)
    {
        return num % 2 == 0;
    };
    std::cout << typeid(function2).name() << "  function2(1024): " << function2(1024) << std::endl;
    std::cout << typeid(function2).name() << "  function2 bool : " << (function2 ? true : false) << std::endl;

    std::function<bool(int)> function3(function);
    std::cout << typeid(function3).name() << "  function3(1024): " << function3(1024) << std::endl;
    std::cout << typeid(function3).name() << "  function3 bool : " << (function3 ? true : false) << std::endl;

    return 0;
}

输出

St8functionIFbiEE  function1 bool : false
St8functionIFbiEE  function2(1024): true
St8functionIFbiEE  function2 bool : true
St8functionIFbiEE  function3(1024): false
St8functionIFbiEE  function3 bool : true

调用其目标

std::function<R(Args...)>::operator()

R operator()( Args... args ) const;

(C++11 起)

以参数 args 调用存储的可调用函数目标。

等效于进行 INVOKE<R>(f, std::forward<Args>(args)...) ,其中 f*this 的目标对象,且 INVOKE 是描述于可调用 (Callable) 的操作。

参数

args-传递给存储的可调用函数目标的参数

返回值

R 为 void 则为无。否则为存储的可调用对象的调用返回值。

异常

  • 若不存储可调用函数目标,即 !*this == true ,则抛出 std::bad_function_call 。

调用示例

#include <iostream>
#include <functional>

bool function(int num)
{
    return num % 2 == 1;
}

int main()
{
    std::cout << std::boolalpha;
    //以参数 args 调用存储的可调用函数目标。
    //等效于进行 INVOKE<R>(f, std::forward<Args>(args)...) ,
    //其中 f 是 *this 的目标对象,且 INVOKE 是描述于可调用 (Callable) 的操作。

    std::function<bool(int)> function1;
    std::cout << typeid(function1).name() << "  function1 bool : " << (function1 ? true : false) << std::endl;

    std::function<bool(int)> function2 = [](int num)
    {
        return num % 2 == 0;
    };
    std::cout << typeid(function2).name() << "  function2(1024): " << function2(1024) << std::endl;
    std::cout << typeid(function2).name() << "  function2 bool : " << (function2 ? true : false) << std::endl;

    std::function<bool(int)> function3(function);
    std::cout << typeid(function3).name() << "  function3(1024): " << function3(1024) << std::endl;
    std::cout << typeid(function3).name() << "  function3 bool : " << (function3 ? true : false) << std::endl;

    std::cout << typeid(function1).name() << "  function1(1024): " << function1(1024) << std::endl;

    return 0;
}

输出

St8functionIFbiEE  function1 bool : false
St8functionIFbiEE  function2(1024): true
St8functionIFbiEE  function2 bool : true
St8functionIFbiEE  function3(1024): false
St8functionIFbiEE  function3 bool : true
terminate called after throwing an instance of 'std::bad_function_call'
  what():  bad_function_call

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值