任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
函数包装器
std::function 提供存储任意类型函数对象的支持。
包装具有指定函数调用签名的任意类型的可调用对象
std::function
| template< class > | (C++11 起) | |
| template< class R, class... Args > | (C++11 起) | 
类模板 
存储的可调用对象被称为 
成员类型
| 类型 | 定义 | 
| 若 sizeof...(Args)==1 且 | |
| 若 sizeof...(Args)==2 且 | |
| 若 sizeof...(Args)==2 且 | 
获得 std::function 所存储的目标的typeid
std::function<R(Args...)>::target_type
| const std::type_info& target_type() const noexcept; | (C++11 起) | 
返回存储的函数的类型。
参数
(无)
返回值
若存储的函数拥有 
获得指向 std::function 所存储的目标的指针
std::function<R(Args...)>::target
| template< class T > | (1) | (C++11 起) | 
| template< class T > | (2) | (C++11 起) | 
返回指向存储的可调用函数目标的指针。
参数
(无)
返回值
若 target_type() == typeid(T) 则为指向存储的函数的指针,否则为空指针。
比较 std::function 和 nullptr
operator==,!=(std::function)
| template< class R, class... ArgTypes > | (1) | (C++11 起) | 
| template< class R, class... ArgTypes > | (2) | (C++11 起) | 
| template< class R, class... ArgTypes > | (3) | (C++11 起) | 
| template< class R, class... ArgTypes > | (4) | (C++11 起) | 
与空指针比较 
参数
| f | - | 要比较的 | 
返回值
1-2) !f
3-4) (bool) f
调用示例
#include <iostream>
#include <functional>
bool function(int num)
{
    return num % 2 == 1;
}
int main()
{
    std::cout << std::boolalpha;
    std::function<bool(int)> function1(function);
    std::function<bool(int)> function2 = [](int num)
    {
        return num % 2 == 1;
    };
    //返回存储的函数的类型。
    std::cout << function1.target_type().name() << "  function1(1024): " << function1(1024) << std::endl;
    std::cout << typeid(function1).name() << "  function1 bool : " << (function1 ? true : false) << std::endl;
    std::cout << typeid(function1).name() << "  target_type()  : "
              << (function1.target_type() == typeid(bool(*)(int)) ? true : false) << std::endl;
    //返回指向存储的可调用函数目标的指针。
    std::cout << typeid(function1).name() << "  function1.target<bool(*)(int)>() : "
              << function1.target<bool(*)(int)>() << std::endl;
    std::cout << function2.target_type().name() << "  function2(1024): " << function2(1024) << std::endl;
    std::cout << typeid(function2).name() << "  function2 bool : " << (function2 ? true : false) << std::endl;
    std::cout << typeid(function2).name() << "  target_type()  : "
              << (function2.target_type() == typeid(bool(*)(int)) ? true : false) << std::endl;
    std::cout << typeid(function2).name() << "  function2.target<bool(*)(int)>() : "
              << function2.target<bool(*)(int)>() << std::endl;
    //与空指针比较 std::function 。空 function (即无可调用目标的 function )比较相等,非空 function 比较不相等。
    std::cout << "function1 == nullptr :   "  << (function1 == nullptr) << std::endl;
    std::cout << "function1 != nullptr :   "  << (function1 != nullptr) << std::endl;
    std::function<bool(int)> function3;
    std::cout << "function3 == nullptr :   "  << (function3 == nullptr) << std::endl;
    std::cout << "function3 != nullptr :   "  << (function3 != nullptr) << std::endl;
    return 0;
}
输出
PFbiE function1(1024): false St8functionIFbiEE function1 bool : true St8functionIFbiEE target_type() : true St8functionIFbiEE function1.target<bool(*)(int)>() : 0x62fe7c Z4mainEUliE_ function2(1024): false St8functionIFbiEE function2 bool : true St8functionIFbiEE target_type() : false St8functionIFbiEE function2.target<bool(*)(int)>() : 0 function1 == nullptr : false function1 != nullptr : true function3 == nullptr : true function3 != nullptr : false