任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。
函数包装器
std::function 提供存储任意类型函数对象的支持。
包装具有指定函数调用签名的任意类型的可调用对象
std::function
template< class > | (C++11 起) | |
template< class R, class... 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_type | R |
argument_type (C++17 中弃用)(C++20 中移除) | 若 sizeof...(Args)==1 且 T 是 Args... 中首个且唯一的类型,则为 T |
first_argument_type (C++17 中弃用)(C++20 中移除) | 若 sizeof...(Args)==2 且 T1 是 Args... 中二个类型的第一个,则为 T1 |
second_argument_type (C++17 中弃用)(C++20 中移除) | 若 sizeof...(Args)==2 且 T2 是 Args... 中二个类型的第二个,则为 T2 |
获得 std::function 所存储的目标的typeid
std::function<R(Args...)>::target_type
const std::type_info& target_type() const noexcept; | (C++11 起) |
返回存储的函数的类型。
参数
(无)
返回值
若存储的函数拥有 T
类型,则为 typeid(T) ,否则为 typeid(void) 。
获得指向 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 起) |
与空指针比较 std::function
。空 function (即无可调用目标的 function )比较相等,非空 function 比较不相等。
参数
f | - | 要比较的 std::function |
返回值
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