C++11很吊的新特性!std::function
ztj100 2025-01-14 19:12 18 浏览 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;
}
相关推荐
- 使用Python编写Ping监测程序(python 测验)
-
Ping是一种常用的网络诊断工具,它可以测试两台计算机之间的连通性;如果您需要监测某个IP地址的连通情况,可以使用Python编写一个Ping监测程序;本文将介绍如何使用Python编写Ping监测程...
- 批量ping!有了这个小工具,python再也香不了一点
-
号主:老杨丨11年资深网络工程师,更多网工提升干货,请关注公众号:网络工程师俱乐部下午好,我的网工朋友。在咱们网工的日常工作中,经常需要检测多个IP地址的连通性。不知道你是否也有这样的经历:对着电脑屏...
- python之ping主机(python获取ping结果)
-
#coding=utf-8frompythonpingimportpingforiinrange(100,255):ip='192.168.1.'+...
- 网站安全提速秘籍!Nginx配置HTTPS+反向代理实战指南
-
太好了,你直接问到重点场景了:Nginx+HTTPS+反向代理,这个组合是现代Web架构中最常见的一种部署方式。咱们就从理论原理→实操配置→常见问题排查→高级玩法一层层剖开说,...
- Vue开发中使用iframe(vue 使用iframe)
-
内容:iframe全屏显示...
- Vue3项目实践-第五篇(改造登录页-Axios模拟请求数据)
-
本文将介绍以下内容:项目中的public目录和访问静态资源文件的方法使用json文件代替http模拟请求使用Axios直接访问json文件改造登录页,配合Axios进行登录请求,并...
- Vue基础四——Vue-router配置子路由
-
我们上节课初步了解Vue-router的初步知识,也学会了基本的跳转,那我们这节课学习一下子菜单的路由方式,也叫子路由。子路由的情况一般用在一个页面有他的基础模版,然后它下面的页面都隶属于这个模版,只...
- Vue3.0权限管理实现流程【实践】(vue权限管理系统教程)
-
作者:lxcan转发链接:https://segmentfault.com/a/1190000022431839一、整体思路...
- swiper在vue中正确的使用方法(vue中如何使用swiper)
-
swiper是网页中非常强大的一款轮播插件,说是轮播插件都不恰当,因为它能做的事情太多了,swiper在vue下也是能用的,需要依赖专门的vue-swiper插件,因为vue是没有操作dom的逻辑的,...
- Vue怎么实现权限管理?控制到按钮级别的权限怎么做?
-
在Vue项目中实现权限管理,尤其是控制到按钮级别的权限控制,通常包括以下几个方面:一、权限管理的层级划分...
- 【Vue3】保姆级毫无废话的进阶到实战教程 - 01
-
作为一个React、Vue双修选手,在Vue3逐渐稳定下来之后,是时候摸摸Vue3了。Vue3的变化不可谓不大,所以,本系列主要通过对Vue3中的一些BigChanges做...
- Vue3开发极简入门(13):编程式导航路由
-
前面几节文章,写的都是配置路由。但是在实际项目中,下面这种路由导航的写法才是最常用的:比如登录页面,服务端校验成功后,跳转至系统功能页面;通过浏览器输入URL直接进入系统功能页面后,读取本地存储的To...
- vue路由同页面重定向(vue路由重定向到外部url)
-
在Vue中,可以使用路由的重定向功能来实现同页面的重定向。首先,在路由配置文件(通常是`router/index.js`)中,定义一个新的路由,用于重定向到同一个页面。例如,我们可以定义一个名为`Re...
- 那个 Vue 的路由,路由是干什么用的?
-
在Vue里,路由就像“页面导航的指挥官”,专门负责管理页面(组件)的切换和显示逻辑。简单来说,它能让单页应用(SPA)像多页应用一样实现“不同URL对应不同页面”的效果,但整个过程不会刷新网页。一、路...
- Vue3项目投屏功能开发!(vue投票功能)
-
最近接了个大屏项目,产品想在不同的显示器上展示大屏项目不同的页面,做出来的效果图大概长这样...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 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)