callable object与新增的function相关 C++11中万能的可调用类型声明std::function<...>
在c++11中,一个callable object(可调用对象)可以是函数指针、lambda表达式、重载()的某类对象、bind包裹的某对象等等,有时需要统一管理一些这几类对象,新增的function就是为此而生。function位于functional头文件,可以看做是那几类callable object的抽象表示。
#include<iostream>
#include<functional>
using namespace std; int f_add(int i,int j){return i+j;} auto l_add = [](int i,int j){return i+j;}; class c_add{
public:
c_add()=default;
int operator()(int i,int j){return i+j;}
}; int main()
{
function<int(int,int)> f1 = f_add;
function<int(int,int)> f2 = l_add;
function<int(int,int)> f3 = c_add(); cout<<"f1 add: "<<f1(,)<<endl;
cout<<"f2="" add:="" "<<f2(,)<<endl;
cout<<"f3="" "<<f3(,)<<endl;
cout<<"end="" in="" main\n";
}
//f_add是普通的函数,l_add是lambda表达式,c_add重载了()操作符,都能起到计算两个int和的效果,他们都可以用function对象来“存储”。这东西虽然很方便,但总感觉平时不会用到太多。
C++11中万能的可调用类型声明std::function<...>
在C++11中,callable object 包括传统C函数,C++成员函数,函数对象(实现了()运算符的类的实例),lambda表达式(特殊函数对象)共4种。程序设计,特别是程序库设计时,经常需要涉及到回调,如果针对每种不同的callable object单独进行声明类型,代码将会非常散乱,也不灵活。如下示例:
#include <iostream>
#include <functional>
using namespace std; // 传统C函数
int c_function(int a, int b)
{
return a + b;
} // 函数对象
class Functor
{
public:
int operator()(int a, int b)
{
return a + b;
}
}; int main(int argc, char** argv)
{
int(*f)(int, int); // 声明函数类型,赋值只能是函数指针
f = c_function;
cout << f(, ) << endl; Functor ff = Functor(); // 声明函数对象类型,赋值只能是函数对象
cout << ff(, ) << endl;
}
幸运的是,C++标准库的头文件里定义了std::function<>模板,此模板可以容纳所有类型的callable object.示例代码如下:
#include <iostream>
#include <functional>
using namespace std; // 传统C函数
int c_function(int a, int b)
{
return a + b;
} // 函数对象
class Functor
{
public:
int operator()(int a, int b)
{
return a + b;
}
}; int main(int argc, char** argv)
{
// 万能可调用对象类型
std::function<int(int, int)> callableObject; // 可以赋值为传统C函数指针
callableObject = c_function;
cout << callableObject(, ) << endl; // 可以赋值为函数对象
Functor functor;
callableObject = functor;
cout << callableObject(, ) << endl; // 可以赋值为lambda表达式(特殊函数对象)
callableObject = [](int a, int b){
return a + b;
};
cout << callableObject(, ) << endl;
}
std::function<>的这种多态能力确实很强,这样可以定义一个回调列表,而列表的元素可接受的可调用物类型并不相同。如下:
#include <iostream>
#include <functional>
#include <list>
using namespace std; // 传统C函数
int c_function(int a, int b)
{
return a + b;
} // 函数对象
class Functor
{
public:
int operator()(int a, int b)
{
return a + b;
}
}; int main(int argc, char** argv)
{
Functor functor;
std::list<std::function<int(int, int)>> callables; callables.push_back(c_function);
callables.push_back(functor);
callables.push_back([](int x, int y)->int{
return x + y;
}); for (const auto& e : callables)
{
cout << e(, ) << endl;
}
}
对于使用C回调机制的程序库来说,C++的std::function<>能兼容传统C函数指针,所以库这一端使用std::function<>代替函数指针,并不会影响旧有客户端程序的编码方式。
callable object与新增的function相关 C++11中万能的可调用类型声明std::function<...>的更多相关文章
- C++11中万能的可调用类型声明std::function<...>
在C++11中,callable object 包括传统C函数,C++成员函数,函数对象(实现了()运算符的类的实例),lambda表达式(特殊函数对象)共4种.程序设计,特别是程序库设计时,经常需要 ...
- C++11新特性应用--实现延时求值(std::function和std::bind)
说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...
- 剖析std::function接口与实现
目录 前言 一.std::function的原理与接口 1.1 std::function是函数包装器 1.2 C++注重运行时效率 1.3 用函数指针实现多态 1.4 std::function的接 ...
- C++11中std::function的使用
class template std::function is a general-purpose polymorphic function wrapper. Instances of std::fu ...
- C++11 std::bind std::function 高级使用方法
从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ # ...
- std::function 的使用说明
转自: https://www.cnblogs.com/heartchord/p/5017071.html //////////////////// std::function 参考资料 • cp ...
- std::function
参考资料 • cplusplus.com:http://www.cplusplus.com/reference/functional/function/ • cppreference.com:http ...
- C++ 11 std::function std::bind使用
cocos new 出新的项目之后,仔细阅读代码,才发现了一句3.0区别于2.0的代码: auto closeItem = MenuItemImage::create( "CloseNorm ...
- c++11特性与cocos2d-x 3.0之std::bind与std::function
昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0 ...
随机推荐
- 第一个python程序hello.py
使用vim编辑代码: #!/usr/bin/python2.7 #-*-coding:utf-8-*- name = raw_input('请输入你的名字:') print 'Hello,',name ...
- 解决VS2010链接错误:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
网上有多种解决办法,我用最懒的一种,系统是Win7 X86,方法如下: C:\Windows\Microsoft.NET\Framework\v4.0.30319\cvtres.exe C:\Prog ...
- delphi 获取图片某一像素的颜色值
前言:在VCL里有GetPixel函数,可直接用,在FMX里直接用这个函数没有定义,在FMX的library中找这个函数在FMX.Graphics.TBitmapData.GetPixel中 结果我引 ...
- 技术文档--svn
1.什么是版本控制,说出常见的版本控制系统及其区别版本控制它是一种软件工程籍以在开发的过程中,确保由不同人所编辑的同一档案都得到更新,它透过文档控制记录程序各个模块的改动,并为每次改动编上序号,并且编 ...
- Robots on a grid(DP+bfs())
链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=25585 Current Server Time: 2013-08-27 20:42:26 Ro ...
- Spring MVC静态资源处理(在applicationContex.xml文件中进行配置)
优雅REST风格的资源URL不希望带 .html 或 .do 等后缀.由于早期的Spring MVC不能很好地处理静态资源,所以在web.xml中配置DispatcherServlet的请求映射,往往 ...
- Linux splint命令
一.简介 splint是一个针对C语言的开源程序静态分析工具. 二.安装配置 1)yum安装 yum install -y splint 2)源码安装 http://www.splint.org/ 配 ...
- css3写的实用表单美化
<!DOCTYPE html> <!--[if IE 7 ]> <html lang="en" class="ie7"> & ...
- SQLSERVER监控复制并使用数据库邮件功能发告警邮件
SQLSERVER监控复制并使用数据库邮件功能发告警邮件 最近熬出病来了,都说IT行业伤不起,不说了,说回今天的正题 正题 上个月月底的时候因为要搬迁机房,需要将一个数据信息数据库先搬到我们的机房,然 ...
- 搭建wordpress开发环境
安装php PHP下载地址:http://windows.php.net/download/ 和MySQL一样,下载包是一个zip压缩包,只需解压缩即可.将它解压缩到 D:\WNMP\php-5.5. ...