C++11很吊的新特性!std::function
ztj100 2025-01-14 19:12 21 浏览 0 评论
std::function简介
std::function是一个函数包装器,该函数包装器模板能包装任何类型的可调用实体,如普通函数,函数对象,lamda表达式等。包装器可拷贝,移动等,并且包装器类型仅仅依赖于调用特征,而不依赖于可调用元素自身的类型。std::function是C++11的新特性,包含在头文件<functional>中。
一个std::function类型对象实例可以包装下列这几种可调用实体:函数、函数指针、成员函数、静态函数、lamda表达式和函数对象。std::function对象实例可被拷贝和移动,并且可以使用指定的调用特征来直接调用目标元素。当std::function对象实例未包含任何实际可调用实体时,调用该std::function对象实例将抛出std::bad_function_call异常。
std::function实战
std::function模板类声明
template<class _Rp, class ..._ArgTypes>
class _LIBCPP_TEMPLATE_VIS function<_Rp(_ArgTypes...)>
: public __function::__maybe_derive_from_unary_function<_Rp(_ArgTypes...)>,
public __function::__maybe_derive_from_binary_function<_Rp(_ArgTypes...)>
{ ... }
std::function模板类成员函数声明
typedef _Rp result_type;
// construct/copy/destroy:
_LIBCPP_INLINE_VISIBILITY
function() _NOEXCEPT { }
_LIBCPP_INLINE_VISIBILITY
function(nullptr_t) _NOEXCEPT {}
function(const function&);
function(function&&) _NOEXCEPT;
template<class _Fp, class = _EnableIfCallable<_Fp>>
function(_Fp);
#if _LIBCPP_STD_VER <= 14
template<class _Alloc>
_LIBCPP_INLINE_VISIBILITY
function(allocator_arg_t, const _Alloc&) _NOEXCEPT {}
template<class _Alloc>
_LIBCPP_INLINE_VISIBILITY
function(allocator_arg_t, const _Alloc&, nullptr_t) _NOEXCEPT {}
template<class _Alloc>
function(allocator_arg_t, const _Alloc&, const function&);
template<class _Alloc>
function(allocator_arg_t, const _Alloc&, function&&);
template<class _Fp, class _Alloc, class = _EnableIfCallable<_Fp>>
function(allocator_arg_t, const _Alloc& __a, _Fp __f);
#endif
function& operator=(const function&);
function& operator=(function&&) _NOEXCEPT;
function& operator=(nullptr_t) _NOEXCEPT;
template<class _Fp, class = _EnableIfCallable<_Fp>>
function& operator=(_Fp&&);
~function();
// function modifiers:
void swap(function&) _NOEXCEPT;
#if _LIBCPP_STD_VER <= 14
template<class _Fp, class _Alloc>
_LIBCPP_INLINE_VISIBILITY
void assign(_Fp&& __f, const _Alloc& __a)
{function(allocator_arg, __a, _VSTD::forward<_Fp>(__f)).swap(*this);}
#endif
// function capacity:
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
return static_cast<bool>(__f_);
}
// deleted overloads close possible hole in the type system
template<class _R2, class... _ArgTypes2>
bool operator==(const function<_R2(_ArgTypes2...)>&) const = delete;
template<class _R2, class... _ArgTypes2>
bool operator!=(const function<_R2(_ArgTypes2...)>&) const = delete;
public:
// function invocation:
_Rp operator()(_ArgTypes...) const;
#ifndef _LIBCPP_NO_RTTI
// function target access:
const std::type_info& target_type() const _NOEXCEPT;
template <typename _Tp> _Tp* target() _NOEXCEPT;
template <typename _Tp> const _Tp* target() const _NOEXCEPT;
#endif // _LIBCPP_NO_RTTI
从成员函数里我们知道std::function对象实例不允许进行==和!=比较操作,std::function模板类实例最终调用成员函数_Rp operator()(_ArgTypes...) const进而调用包装的调用实体。
1、std::function包装函数指针
定义一个std::function<int(int)>对象实例
std::function<int(int)> callback;
std::function对象实例包装函数指针
int (*fun_ptr)(int);
int fun1(int a){
return a;
}
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
fun_ptr = fun1; //函数指针fun_ptr指向fun1函数
callback = fun_ptr; //std::function对象包装函数指针
std::cout << callback(10) << std::endl; //std::function对象实例调用包装的实体
return 0;
}
2、std::function包装函数
int fun1(int a){
return a;
}
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
callback = fun1; //std::function包装函数
std::cout << callback(42) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
3、std::function包装模板函数
template<typename T>
T fun2(T a){
return a + 2;
}
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
callback = fun2<int>; //std::function包装模板函数
std::cout << callback(10) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
4、std::function包装函数对象
struct add{
int operator()(int x){
return x + 9;
}
};
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
callback = add(); //std::function包装对象函数
std::cout << callback(2) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
5、std::function包装lamda表达式
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
auto fun3 = [](int a) {return a * 2;}; //lamda表达式
callback = fun3; //std::function包装lamda表达式
std::cout << callback(9) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
6、std::function包装模板对象函数
template <typename T>
struct sub{
T operator()(T a){
return a - 8;
}
};
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
callback = sub<int>(); //std::function包装模板对象函数
std::cout << callback(2) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
7、std::function包装模板对象静态函数
template <typename T>
struct foo2{
static T foo(T a){
return a * 4;
}
};
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
callback = foo2<int>::foo; //std::function包装模板对象静态函数
std::cout << callback(3) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
8、std::function包装对象静态函数
struct foo1{
static int foo(int a){
return a * 3;
}
};
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
callback = foo1::foo; //std::function包装对象静态函数
std::cout << callback(5) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
9、std::function包装类成员函数
struct foo3{
int foo(int a){
return a * a;
}
};
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
foo3 test_foo1;
callback = std::bind(&foo3::foo, test_foo1, std::placeholders::_1); //std::function包装类成员函数
std::cout << callback(9) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
这里我们用到了std::bind,C++11中std::bind函数的意义就如字面上的意思一样,用来绑定函数调用的某些参数。std::bind的思想其实是一种延迟计算的思想,将可调用对象保存起来,然后在需要的时候再调用。而且这种绑定是非常灵活的,不论是普通函数还是函数对象还是成员函数都可以绑定,而且其参数可以支持占位符。
这里的std::placeholders::_1是一个占位符,且绑定第一个参数,若可调用实体有2个形参,那么绑定第二个参数的占位符是std::placeholders::_2。
10、std::function包装模板类成员函数
template <typename T>
struct foo4{
T foo(T a){
return a * 6;
}
};
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
foo4<int> test_foo2;
callback = std::bind(&foo4<int>::foo, test_foo2, std::placeholders::_1); //std::function包装模板类成员函数
std::cout << callback(7) << std::endl; //std::function对象实例调用包装的调用实体
return 0;
}
11、std::function拷贝、移动
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
std::function<int(int)> callback2 = callback; //拷贝赋值运算符
std::cout << callback2(7) << std::endl;
std::function<int(int)>&& callback3 = std::move(callback); //移动赋值运算符
std::cout << callback3(7) << std::endl;
std::cout << callback(7) << std::endl;
std::function<int(int)> callback4(callback); //拷贝
std::cout << callback4(7) << std::endl;
return 0;
}
相关推荐
- 其实TensorFlow真的很水无非就这30篇熬夜练
-
好的!以下是TensorFlow需要掌握的核心内容,用列表形式呈现,简洁清晰(含表情符号,<300字):1.基础概念与环境TensorFlow架构(计算图、会话->EagerE...
- 交叉验证和超参数调整:如何优化你的机器学习模型
-
准确预测Fitbit的睡眠得分在本文的前两部分中,我获取了Fitbit的睡眠数据并对其进行预处理,将这些数据分为训练集、验证集和测试集,除此之外,我还训练了三种不同的机器学习模型并比较了它们的性能。在...
- 机器学习交叉验证全指南:原理、类型与实战技巧
-
机器学习模型常常需要大量数据,但它们如何与实时新数据协同工作也同样关键。交叉验证是一种通过将数据集分成若干部分、在部分数据上训练模型、在其余数据上测试模型的方法,用来检验模型的表现。这有助于发现过拟合...
- 深度学习中的类别激活热图可视化
-
作者:ValentinaAlto编译:ronghuaiyang导读使用Keras实现图像分类中的激活热图的可视化,帮助更有针对性...
- 超强,必会的机器学习评估指标
-
大侠幸会,在下全网同名[算法金]0基础转AI上岸,多个算法赛Top[日更万日,让更多人享受智能乐趣]构建机器学习模型的关键步骤是检查其性能,这是通过使用验证指标来完成的。选择正确的验证指...
- 机器学习入门教程-第六课:监督学习与非监督学习
-
1.回顾与引入上节课我们谈到了机器学习的一些实战技巧,比如如何处理数据、选择模型以及调整参数。今天,我们将更深入地探讨机器学习的两大类:监督学习和非监督学习。2.监督学习监督学习就像是有老师的教学...
- Python 模型部署不用愁!容器化实战,5 分钟搞定环境配置
-
你是不是也遇到过这种糟心事:花了好几天训练出的Python模型,在自己电脑上跑得顺顺当当,一放到服务器就各种报错。要么是Python版本不对,要么是依赖库冲突,折腾半天还是用不了。别再喊“我...
- 神经网络与传统统计方法的简单对比
-
传统的统计方法如...
- 自回归滞后模型进行多变量时间序列预测
-
下图显示了关于不同类型葡萄酒销量的月度多元时间序列。每种葡萄酒类型都是时间序列中的一个变量。假设要预测其中一个变量。比如,sparklingwine。如何建立一个模型来进行预测呢?一种常见的方...
- 苹果AI策略:慢哲学——科技行业的“长期主义”试金石
-
苹果AI策略的深度原创分析,结合技术伦理、商业逻辑与行业博弈,揭示其“慢哲学”背后的战略智慧:一、反常之举:AI狂潮中的“逆行者”当科技巨头深陷AI军备竞赛,苹果的克制显得格格不入:功能延期:App...
- 时间序列预测全攻略,6大模型代码实操
-
如果你对数据分析感兴趣,希望学习更多的方法论,希望听听经验分享,欢迎移步宝藏公众号...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)