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<>代替函数指针,并不会影响旧有客户端程序的编码方式。

C++11中万能的可调用类型声明std::function<...>的更多相关文章

  1. callable object与新增的function相关 C++11中万能的可调用类型声明std::function<...>

    在c++11中,一个callable object(可调用对象)可以是函数指针.lambda表达式.重载()的某类对象.bind包裹的某对象等等,有时需要统一管理一些这几类对象,新增的function ...

  2. C++11新特性应用--实现延时求值(std::function和std::bind)

    说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...

  3. C++11中的std::function

    看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard::KeyCode, Event*)> onKeyPressed; std::fun ...

  4. 【转】C++11中的std::function

    原文地址:http://www.jellythink.com/archives/771 看看这段代码 先来看看下面这两行代码: std::function<void(EventKeyboard: ...

  5. C++中的各种可调用对象

    概述 一组执行任务的语句都可以视为一个函数,一个可调用对象.在程序设计的过程中,我们习惯于把那些具有复用性的一组语句抽象为函数,把变化的部分抽象为函数的参数. 函数的使用能够极大的极少代码重复率,提高 ...

  6. C++11中std::function的使用

    class template std::function is a general-purpose polymorphic function wrapper. Instances of std::fu ...

  7. 浅谈C++11中的多线程(一)

    摘要 本篇文章围绕以下几个问题展开: 进程和线程的区别 何为并发?C++中如何解决并发问题?C++中多线程的基本操作 同步互斥原理以及多进程和多线程中实现同步互斥的两种方法 Qt中的多线程应用 c++ ...

  8. 一起学习c++11——c++11中的新增的容器

    c++11新增的容器1:array array最早是在boost中出现:http://www.boost.org/doc/libs/1_61_0/doc/html/array.html 当时的初衷是希 ...

  9. Effective Modern C++翻译(6)-条款5:auto比显示的类型声明要更好

        在概念上说,auto关键字和它看起来一样简单,但是事实上,它要更微妙一些的.使用auto会让你在声明变量时省略掉类型,同时也会防止了手动类型声明带来的正确性和性能上的困扰:虽然按照语言预先定义 ...

随机推荐

  1. OpenGL笔记<第一章> 构建 GLSL class

    恭喜,我们终于很扎实地完成了第一章——glsl 入门 不幸的是,it's not the basic of GLSL shader ,我们下一节开篇,basic of GLSL shader 在下一章 ...

  2. awk 提取列

    awk '{OFS="";print(substr($0,1,6),substr($0,74,18),substr($0,15,3),substr($0,18,8))}' inpu ...

  3. 如何定义最佳 Cache-Control 策略

    定义最佳 Cache-Control 策略 按照以上决策树为您的应用使用的特定资源或一组资源确定最佳缓存策略.在理想的情况下,您的目标应该是在客户端上缓存尽可能多的响应,缓存尽可能长的时间,并且为每个 ...

  4. [ 原创 ]学习笔记-Android 中关于Cursor类的介绍

    此博文转载自:http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html 主讲Cursor的用法 使用过 SQLite 数据库的童 ...

  5. 制作Linux内核

    <linux内核简介> <linux系统架构> 系统架构 用户部分: 应用程序:GNU C 库内核部分:系统调用接口.内核.体系结构相关代码(与硬件相关的代码) 划分原因:不同 ...

  6. js date 相关

  7. Educational Codeforces Round 44 (Rated for Div. 2)

    题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...

  8. ZOJ 3623 Battle Ships DP

    B - Battle Ships Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  9. ZOJ 3622 Magic Number 打表找规律

    A - Magic Number Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Subm ...

  10. 使用清华大学开源软件镜像AOSP的“每月更新初始化包”更新指定版本的Android源码

    参照官方教程:Tsinghua Open Source Mirror 1. 下载了repo工具 mkdir  ~/bin PATH = ~/bin:$PATH curl  https://storag ...