1. 几种可调用对象(Callable Objects)

(1)普通函数指针类成员的函数指针

(2)具有operator()成员函数的类对象(仿函数)。如c++11中的std::function类模板,本质上就是一个仿函数

(3)可被转换为函数指针的类对象(需要重载类型转换操作符operator type(),其中type为目标类型:函数指针。这种写法与不带参的type operator()(void)效果等价)。

【编程实验】可调用对象示例

//test1.cpp

#include <iostream>
using namespace std; //1. 普通函数
void func(void)
{
} //2. 仿函数对象
struct Foo
{
void operator()(void)
{ }
}; //3. 可转换为函数指针的类
struct Bar
{
using fr_t = void(*)(void); //function return type: 函数返回值类型 static void func(void)
{
} //将类对象转化为函数指针。
//(本质上还是仿函数,只不过返回值是函数指针(fr_t)。
operator fr_t(void) //重载类型转换操作符,效果上等价于fr_t operator()(void)
{
return func;
}
}; //4. 类的成员函数
struct Test
{
int m;
void mem_func(void)
{ }
}; int main()
{
//1. 普通函数
void(*pfunc)(void) = &func;
pfunc(); //2.仿函数
Foo foo;
foo(); //3.可转换为函数指针的类
Bar bar;
bar(); //4.类成员函数指针
void(Test::*pmem_func)(void) = &Test::mem_func;
int Test::*pm = &Test::m; Test t;
(t.*pmem_func)();
t.*pm = ; return ;
}

2. 仿函数类的分析(如: binder2nd<T>、less<T>)

(1)以count_if算法的调用为例

  ①调用count_if函数时,要向其传入容器的相关迭代器以及第3个参数,该参数是一个判断表达式,在本例中是一个binder2nd类型的仿函数对象

  ②每次遍历时,会调用binder2nd仿函数对象的operator(),并向其传入(*first)参数。该对象内部保存着由外部传入的less仿函数对象的引用及该函数对象的第2个参数值40,它们分别保存在op变量和value变量中。

  ③在binder2nd仿函数对象的operator()中,会调用op仿函数 (less对象)并传入*first和40,从而可以判断*first和40的大小。

(2)相关源代码以说明

/***********************************************************************/
//以下两个类模板主要用于定义函数参数和返回值的类型。让子类可以回答
//函数适配器(adapter)可能向其询问的这两个问题。
//只有一个参数的函数类模板
template <class Arg, class Result>
struct unary_function {
typedef Arg argument_type; //参数类型
typedef Result result_type; //返回值类型
}; //两个参数的函数类模板
template <class Arg1, class Arg2, class Result>
struct binary_function {
typedef Arg1 first_argument_type; //第1个参数类型
typedef Arg2 second_argument_type; //第2个参数类型
typedef Result result_type; //返回值类型
}; /***********************************************************************/
//统计指定范围的元素个数的算法
template <class InputIter, class Predicate>
typename iterator_traits<InputIter>::difference_type
count_if(InputIter first, InputIter last, Predicate pred) {
typename iterator_traits<InputIter>::difference_type n = ; //计数器
for ( ; first != last; ++first)
if (pred(*first)) //如果元素带入pred判断的结果为true,计数器加1
++n;
return n;
} /***********************************************************************/
//仿函数对象:用于比较x和y的大小。x<y则返回真,否则假。
//注意:该类继承自binary_function类,表达他有两个参数和一个返回值。
template <class T>
struct less : public binary_function<T, T, bool>
{
bool operator()(const Tp& x, const T& y) const { return x < y; }
}; //仿函数类:用于将可调用对象(如函数)以及其参数保存并封装成一个类
//注意:该类继承自unary_function类,表示只有一个参数和返回值。
// binder2nd顾名思义就是要将参数绑定在op的第2个参数上,所以要求
// Operation这个类必须继承自binary_function。
template <class Operation>
class binder2nd
: public unary_function<typename Operation::first_argument_type,
typename Operation::result_type> {
protected:
Operation op; //可调用对象(如函数) //Operation类必须继承自binary_function类,才能回答下列关于第2个参数类型的提问。
typename Operation::second_argument_type value;//函数的第2个参数
public:
//构造函数
binder2nd(const Operation& x,
const typename Operation::second_argument_type& y)
: op(x), value(y) {}
typename Operation::result_type
operator()(const typename Operation::first_argument_type& x) const {
return op(x, value); //转向调用被保存对象的operator()函数
}
}; /***********************************************************************/
//辅助函数,让用户间接地,当然也更方便调用binder2nd(op,x);
//如果直接调用,如binder2nd<Operation>(less<int>(), 40)则还需要填写出Operation,根据
//binder2nd模板的定义,该值就是op的类型,此例中即less<int>的类型,这不好判断。
//而如果用以下的bind2nd函数,可以让编译器根据op(即less<int>)自动推导出Operation类型。
template <class Operation, class T>
inline binder2nd<Operation> bind2nd(const Operation& op, const T& x)
{
typedef typename Operation::second_argument_type arg2_type;
return binder2nd<Operation>(op, arg2_type(x));
}

bind2nd函数模板、binder2nd类模板等

3. binder2nd类模板和bind2nd函数模板(重点)

(1)binder2nd类模板

  ①重载了operator()操作符,所以该类的对象是一个函数对象(仿函数)

  ②该类用于保存外部传入的函数(或仿函数)以及其参数。它是一个Wrapper类,用于包装和改造传入的函数(或仿函数),然后形成一个新的仿函数对象

  ③继承自unary_function模板,表示该类最终绑定外部函数及参数后,仍然是一个函数对象(仿函数)。但新函数对象与传入的函数相比,其参数个数减少为1个

(2)bind2nd函数模板

  ①bind2nd是一个函数模板,用于简化对binder2nd的操作。

  ②函数的返回值是一个binder2nd对象,该对象是个函数对象(仿函数),也是一个可调用对象。

(3)bind1st、bind2nd函数模板

  ①bind1st用于将参数绑定在被绑定函数的第1个参数上,返回新的函数对象。

  ②bind2nd用于将参数绑定在被绑定函数的第2个参数上,返回新的函数对象。

第10课 std::bind和std::function(1)_可调用对象的更多相关文章

  1. 第11课 std::bind和std::function(2)_std::bind绑定器

    1. 温故知新:std::bind1st和std::bind2nd (1)bind1st.bind2nd首先它们都是函数模板,用于将参数绑定到可调用对象(如函数.仿函数等)的第1个或第2个参数上. ( ...

  2. c++11特性与cocos2d-x 3.0之std::bind与std::function

    昨天同事让帮忙写一小功能,才发现cocos2d-x 3.0 和 cocos2d-x 3.0rc0 差别还是相当大的. 发现Label这一个控件,3.0就比rc0版本多了一个创建函数,更为关键的是3.0 ...

  3. std::bind和std::function

    std::bind 用于绑定一个函数,返回另外一种调用方式的函数对象 ,可以改变参数顺序 和个数,特别是在多线程的程序中,经常用它将函数进行包装,然后打包发送给工作线程,让工作线程去执行我们的任务. ...

  4. 第12课 std::bind和std::function(3)_std::function可调用对象包装器

    1. std::function (1)首先是一个类模板,用于包装可调用对象.可以容纳除了类成员(函数)指针之外的所有可调用对象. (2)可以将普通函数,lambda表达式和函数对象类统一起来.尽管它 ...

  5. std::bind与std::ref, why and how

    首先解释下为什么有时候需要bind. 我们可以用bind从函数T add(T a, T b)造出个inc()来,即把b写死为1.这个例子本身比较傻,但有不傻的应用. template<typen ...

  6. 第19课 lambda vs std::bind

    一. std::bind (一)std::bind实现的关键技术 [编程实验]探索bind原理,实现自己的bind函数 #include <iostream> #include <t ...

  7. C++11 std::bind std::function 高级使用方法

    从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ # ...

  8. C++ 中std::function 、std::bind的使用和lambda的使用

    std::function是可调用对象的包装器:std::bind是将可点用对象和其参数一起进行绑定,且绑定后的结果可以使用std::function对象进行保存,并延迟调用到需要调用的时候: 在C+ ...

  9. C++ 11 std::function std::bind使用

    cocos new 出新的项目之后,仔细阅读代码,才发现了一句3.0区别于2.0的代码: auto closeItem = MenuItemImage::create( "CloseNorm ...

随机推荐

  1. ASP.NET MVC中常用的ActionResult类型

    常见的ActionResult 1.ViewResult 表示一个视图结果,它根据视图模板产生应答内容.对应得Controller方法为View. 2.PartialViewResult 表示一个部分 ...

  2. super and this

    super 指向父类的一个指针, 引用父类中的属性,方法或者构造函数 public class Father { String name ; Father(String myName){ name = ...

  3. MySQL 中 utf8 和 utf8mb4 的使用以及字符集相关(原文优秀,必读)

    MySQL 在 5.5.3 之后 (查看版本:select version();) 增加了这个utf8mb4的编码,mb4 就是 most bytes 4 的意思,支持的字节数最大为 4,即专门用来兼 ...

  4. jmeter—JDBC request动态参数设置

    jmeter—JDBC request动态参数设置 重要参数说明: Variable Name:数据库连接池的名字,需要与JDBC Connection Configuration的Variable ...

  5. Zookeeper 四字命令 Four Letter Words

    1.zk可以通过它自身提供的简写命令来服务器进行交互 需要使用到nc命令,安装yum install nc echo  [commond] | nc [ip] [port] 2.[stat]  查看z ...

  6. 深入理解ASP.NET MVC(8)

    系列目录 过滤器上下文参数 前一节提到了四种MVC内建过滤器,它们无一例外都在关键的方法中提供了叫filterContext的参数,尽管它们各自类型不同,但是都继承自ControllerContext ...

  7. Hanlp分词之CRF中文词法分析详解

    这是另一套基于CRF的词法分析系统,类似感知机词法分析器,提供了完善的训练与分析接口. CRF的效果比感知机稍好一些,然而训练速度较慢,也不支持在线学习. 默认模型训练自OpenCorpus/pku9 ...

  8. Hanlp在java中文分词中的使用介绍

    项目结构 该项目中,.jar和data文件夹和.properties需要从官网/github下载,data文件夹下载 项目配置 修改hanlp.properties: 1 #/Test/src/han ...

  9. Spring Cloud 快速教程

    官方:http://projects.spring.io/spring-cloud/ 中文:https://springcloud.cc/ https://blog.csdn.net/forezp/a ...

  10. linux Centos 服务器之间NFS文件共享挂载

    linux Centos 6.9服务器之间文件共享挂载 目的:因为服务器设置了负载均衡,多服务器的文件上传必然要同步,这里的目的把服务器1设置为主文件服务器 服务器1:192.168.1.100(共享 ...