学习大纲


Ref: 【c++】可调用对象(Callable Objects)

Five kinds of callable objects:

  1. Functions.
  2. Pointers to functions.
  3. Objects of a class that overloads ().
  4. Objects created by bind.
  5. Objects created by lamdba expressions.

函数、函数指针

1-2. Functions and Pointers to functions

普通函数

bool range5to10(int val)  // <-- bool (*p_func)(int) 改为函数指针是一个道理
{
   return (5 <= val && val <= 10);
} vector<int> vec{ , , , , , , , }; auto presult = find_if(vec.begin(), vec.end(), range5to10); if (presult == vec.end())
{
cout << "no" << endl;
}
else
{
cout << "yes" << endl;
}

仿函数

模板de仿函数

更高逼格的template,至少能在类型上更加灵活一些。

template <class T, T lb, T ub>
struct range {
bool operator() (T val) {
return (lb <=val && val <=ub);
}
};

auto presult = find_if(vec.begin(), vec.end(), range<int, 5, 10>{});

3. Object of class

对象de仿函数

class range {
public:
range(int l, int u): lb(l), ub(u) {  // <-- 这里的构建函数的使用体现了其优势:初始化能多做些事情
} bool operator() (int val) {
return (lb <=val && val <=ub);
} private:
int lb, ub;
}; auto presult = find_if(vec.begin(), vec.end(), range{5, 10});

以及补充的三个例子。

struct MyPlus{
int operator() (const int &a , const int &b) const {
return a + b;
}
};
int main()
{
MyPlus a;
cout << MyPlus()(,) << endl;   // 1、通过产生临时对象 调用重载运算符
cout << a.operator()(,) << endl;  // 2、通过对象 显示调用重载运算符
cout << a(,) << endl;   // 3、通过对象 隐示地调用重载运算符
return ;
}

std::function

4. Bind

占位符

一个变量的占位符, 用于函数绑定时使用。

void f(int a, int b, int c)
{
cout << a << " " << b << " " << c << endl;
} auto g1 = bind(f, placeholders::_1, placeholders::_2, );
g1(, );

能绑定什么

Ref: C++11----std::bind/std::placeholder

std::bind 是用来绑定函数调用的参数的,解决的需求 是:

我们有时候可能不会一次性获得调用某个函数的全部参数,通过这个函数,我们可以将部分调用参数提前绑定到函数身上成为一个新的对象,然后在参数齐全后,再完成调用。

#include <functional>
#include <iostream>
int foo(int a, int b) {
return a + b;
}
class Mybind {
public:
int operator() (int a, int b)
{
return a + b;
}
};
int main() {
// 1.将参数一:100绑定到函数 foo 上,但是使用 std::placeholders::_1 来对第一个参数进行占位
auto bindFoo = std::bind(foo, std::placeholders::_1, 100);
// 这时调用 bindFoo 时,只需要提供第一个参数即可
std::cout << bindFoo() << std::endl;

// 2.将参数一:100绑定到lambda上,但是使用 std::placeholders::_1 来对第一个参数进行占位
auto f1 = std::bind([](int a, int b)->int {return a + b; }, std::placeholders::_1, 100);
std::cout << f1() << std::endl;

// 3.将参数一:100绑定到对象上,但是使用 std::placeholders::_1 来对第一个参数进行占位
Mybind m;
auto f2 = std::bind(m, std::placeholders::_1, );
std::cout << f2() << std::endl; std::cin.get();
return ;
}

提示:注意 auto 关键字的妙用。有时候我们可能不太熟悉一个函数的返回值类型,但是我们却可以通过 auto 的使用来规避这一问题的出现。

其中:绑定参数技术多用于设计模式中的适配器模式

参数绑定规则

<示范一>

fun1说明:占位符->第一个参数和函数第一个参数匹配(int),第二个参数和第二个参数匹配(char),第三个参数和第三个参数匹配。

fun2说明:占位符->第二个参数和函数第一个参数匹配(int),第三个参数和第二个参数匹配(char),第一个参数和第三个参数匹配。

fun3说明:占位符->第一个参数和函数第一个参数匹配(int),第二个参数和第二个参数匹配(char),第三个参数默认为98.77

#include <functional>
#include <iostream> int TestFunc(int a, char c, float f)
{
std::cout << a << std::endl;
std::cout << c << std::endl;
std::cout << f << std::endl;
return a;
} int main(void)
{
auto fun1 = std::bind(TestFunc, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
auto fun2 = std::bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
auto fun3 = std::bind(TestFunc, std::placeholders::_1, std::placeholders::_2, 98.77); fun1(, 'C',100.1);
fun2(100.1, , 'C');
fun3(,'C'); return 0;
}

<示范二>

这里placeholders的位置发生变化,看似对结果没有影响。

 #include <functional>
#include <iostream>
#include <vector>
#include <algorithm> using namespace std; class range {
public:
bool operator() (int lb, int ub, int val) {
cout << "lb = " << lb << endl;
cout << "ub = " << ub << endl;
cout << "val = " << val << endl;
return (lb <= val && val <= ub);
}
}; int main()
{
vector<int> vec = {,,,,,,,,,};
auto presult = find_if(vec.begin(), vec.end(), std::bind(range{}, , , std::placeholders::_1));
auto second = find_if(vec.begin(), vec.end(). std::bind(range{}, std::placeholders::_1, , ));  // 注意,placeholders放在第一位也可以。 cout << *presult << endl;
return ;
}

std::function

初步认识,到底是个啥? 

std::function 在C++11后加入标准,可以用它来描述C++中所有可调用实体,它是 可调用对象的包装器,声明如下:

#include <functional> 

比 “函数指针” 更强大?

std::function 强大的地方在于,它能够 兼容所有具有相同参数类型的函数实体。

相比较于函数指针,std::function能兼容带捕获的lambda函数,而且对类成员函数提供支持。

能力展示,遛一遛?

可以承接各种类型的函数,只要参数一致。

#include <iostream>
#include <functional> // std::function
std::function<int(int, int)> SumFunction; // 普通函数
int func_sum(int a, int b) {
return a + b;
} // 类(成员、静态)函数
class Calcu
{
public:
int base = ;
// 类的成员方法,参数包含this指针
int class_func_sum(const int a, const int b) const { return this->base + a + b; };
// 类的静态成员方法,不包含this指针
static int class_static_func_sum(const int a, const int b) { return a + b; };
}; // 仿函数
class ImitateAdd
{
public:
int operator() (const int a, const int b) const { return a + b; };
}; // lambda函数
auto lambda_func_sum = [](int a, int b) -> int { return a + b; }; // 函数指针
int (*func_pointer)(int, int); int main(void)
{
int x = ;
int y = ; // 普通函数
SumFunction = func_sum;
int sum = SumFunction(x, y);
std::cout << "func_sum:" << sum << std::endl; // 类成员函数
Calcu obj;
SumFunction = std::bind(&Calcu::class_func_sum, obj, std::placeholders::_1, std::placeholders::_2); // 绑定this对象
sum = SumFunction(x, y);
std::cout << "Calcu::class_func_sum:" << sum << std::endl; // 类静态函数
SumFunction = Calcu::class_static_func_sum;
sum = SumFunction(x, y);
std::cout << "Calcu::class_static_func_sum:" << sum << std::endl; // lambda函数
SumFunction = lambda_func_sum;
sum = SumFunction(x, y);
std::cout << "lambda_func_sum:" << sum << std::endl; // 带捕获的lambda函数
int base = ;
auto lambda_func_with_capture_sum = [&base](int x, int y)->int { return x + y + base; };
SumFunction = lambda_func_with_capture_sum;
sum = SumFunction(x, y);
std::cout << "lambda_func_with_capture_sum:" << sum << std::endl; // 仿函数
ImitateAdd imitate;
SumFunction = imitate;
sum = SumFunction(x, y);
std::cout << "imitate func:" << sum << std::endl; // 函数指针
func_pointer = func_sum;
SumFunction = func_pointer;
sum = SumFunction(x, y);
std::cout << "function pointer:" << sum << std::endl; return ;
}

Lambda 函数

5. Lambda Functions

初步认识

可参考:[c++] C Language Features

“Lambda 表达式” (lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。

Lambda表达式可以表示闭包(注意和数学传统意义上的不同)。

返回值

由于Lambda的类型是唯一的,不能通过类型名来显式声明对应的对象,但可以利用auto关键字和类型推导:

auto f=[](int a,int b){return a>b;};

或者,返回值类型转换。

std::cout << [](float f)        { return std::abs(f); } (-3.5);
std::cout << [](float f) -> int { return std::abs(f); } (-3.5);

这个语句与前面的不同之处在于,lambda 表达式的返回时不是 float 而是 int。

第一个返回3.5;第二个返回3。

Return Type for Lambdas

参数

基本定义

[]        // 不捕获任何外部变量
[=] // 以值的形式捕获所有外部变量
[&] // 以引用形式捕获所有外部变量
[x, &y] // x 以传值形式捕获,y 以引用形式捕获
[=, &z] // z 以引用形式捕获,其余变量 以传值形式捕获
[&, x] // x 以值的形式捕获,其余变量 以引用形式捕获

捕获外部变量

float f0 = 1.0;
std::cout << [=](float f) { return f0 + std::abs(f); } (-3.5); 传值:其输出值是 4.5 --------------------------------------------------------------------------------- float f0 = 1.0;
std::cout << [&](float f) { return f0 += std::abs(f); } (-3.5);
std::cout << '\n' << f0 << '\n'; 传引用:输出值是 4.5 和 4.5 --------------------------------------------------------------------------------- float f0 = 1.0;
std::cout << [=](float f) mutable { return f0 += std::abs(f); } (-3.5);
std::cout << '\n' << f0 << '\n'; 如果以传值的形式捕获外部变量,那么,lambda 体不允许修改外部变量。
你会觉得输出值是什么呢?答案是,4.5 和 1.0。 --------------------------------------------------------------------------------- float f0 = 1.0f;
float f1 = 10.0f;
std::cout << [=, &f0](float a) { return f0 += f1 + std::abs(a); } (-3.5);
std::cout << '\n' << f0 << '\n'; 混合机制:这个例子的输出是 14.5 和 14.5。

不捕获外部变量

** 捕获 **
auto f = [x](int a,int b){return a>x;}); // x被捕获复制
int x=,y=;
auto g = [&](int x){return ++y;});    // y被捕获引用,调用g后会修改y,需要注意y的生存期 (y是返回值)
auto g = [&y](int x) {return ++y;});   // 这样也是可以的貌似。 ** 未捕获 **
bool(*fp)(int,int) = [](int a,int b){return a>b;}); // 不捕获时才可转换为“函数指针”

End.

[c++] Callable Objects的更多相关文章

  1. Callable Objects

    We learned in 7.11 that there are "array-like" objects that are not true arrays but can be ...

  2. c++11 Using Callable Objects, std::thread, std::bind, std::async, std::call_once

  3. is_callable Callbacks / Callables What is a “callable”? 可调用 回调函数

    PHP: Callback / Callable 类型 - Manual https://www.php.net/manual/zh/language.types.callable.php Callb ...

  4. C++11语法糖

    1.constexpr变量:声明为constexpr的变量一定是一个常量,新标准允许定义一种特殊的constexpr函数使得编译时就可计算结果,这样就能用constexpr函数去初始化constexp ...

  5. 【翻译十九】-java之执行器

    Executors In all of the previous examples, there's a close connection between the task being done by ...

  6. [Code::Blocks] Install wxWidgets & openCV

    The open source, cross platform, free C++ IDE. Code::Blocks is a free C++ IDE built to meet the most ...

  7. C++11lambda表达式

    [C++11lambda表达式] mutable 修饰符,用于修改[]中以值传递的变量,无mutable修饰符的话则不行. 使用示例: #include <vector> #include ...

  8. 在 tornado 中异步无阻塞的执行耗时任务

    在 tornado 中异步无阻塞的执行耗时任务 在 linux 上 tornado 是基于 epoll 的事件驱动框架,在网络事件上是无阻塞的.但是因为 tornado 自身是单线程的,所以如果我们在 ...

  9. Java Concurrency - 线程执行器

    Usually, when you develop a simple, concurrent-programming application in Java, you create some Runn ...

随机推荐

  1. String对象方法扩展

    /** *字符串-格式化 */ String.prototype.format = function(){ var args = arguments;//获取函数传递参数数组,以便在replace回调 ...

  2. .net 文件上传大小的设置

    直接在配置文件web.config 中进行如下配置,主要需要明白的就是 配置的 单位是 Byte,  所以一定计算清楚,不然会在这里纠结很久!!! <configuration> < ...

  3. C#中常用的读取xml的几种方法(转)

    本文完全来源于http://blog.csdn.net/tiemufeng1122/article/details/6723764,仅作个人学习之用. XML文件是一种常用的文件格式,例如WinFor ...

  4. jquery 回到顶部,简洁大方

    效果

  5. 导入导出Excel工具类ExcelUtil

    前言 前段时间做的分布式集成平台项目中,许多模块都用到了导入导出Excel的功能,于是决定封装一个ExcelUtil类,专门用来处理Excel的导入和导出 本项目的持久化层用的是JPA(底层用hibe ...

  6. Hibernate中使用Criteria查询

    忽略一些配置,测试代码如下: Session session= HibernateUtil.getSession(); Transaction tx= session.beginTransaction ...

  7. PHP基础知识之字符串运算符

    两个字符串相加用 . 运算符(类似于+),如$a="str1" . "str2";$b=$a . "str3";=>"str ...

  8. 介绍.NET 开发必备工具 .NET Portability Analyzer

    随着.NET的原来越开放,不仅仅是开源这么简单了,也意味着.NET程序员要关注越来越多的平台,涵盖.NET Mic Framework, Xamarin,Mono,.NET等等,从windows到li ...

  9. ABP理论学习之事件总线和领域事件

    返回总目录 本篇目录 事件总线 定义事件 触发事件 处理事件 句柄注册 取消注册 在C#中,我们可以在一个类中定义自己的事件,而其他的类可以注册该事件,当某些事情发生时,可以通知到该类.这对于桌面应用 ...

  10. Java NIO1:I/O模型概述

    I/O模型 在开始NIO的学习之前,先对I/O的模型有一个理解,这对NIO的学习是绝对有好处的.我画一张图,简单表示一下数据从外部磁盘向运行中进程的内存区域移动的过程: 这张图片明显忽略了很多细节,只 ...