C++ 可调用对象

先提一下lambda

lambda表达式是c++11的新特性,匿名函数对象

形式如下:

cpp 复制代码
[ capture ] ( params ) opt -> ret { body; };

capture:捕获列表

params:参数列表

opt:函数选项

ret:返回值类型

body:函数体

cpp 复制代码
//这种写法指定返回值int  是尾置类型推导的写法,当我们在函数执行前不知道返回值
//是什么类型时可以用这种方式指定返回值
auto f = [](int a) -> int {return a + 1;};
cout << f(3) << endl;

//auto f(int) ->bool { return true;}
//== bool f(int) {return true;}

\[\]:不捕获任何外部变量;

\&:以引用方式捕获所有外部变量;

=:以值方式捕获所有外部变量;

var1, var2, ...:指定捕获特定的外部变量;

\&, var1, var2, ...:以引用方式捕获所有外部变量,并指定捕获特定的外部变量;

=, \&var1, \&var2, ...:以值方式捕获所有外部变量,并以引用方式捕获特定的外部变量。

返回值有时可以省略,可以自动推导

cpp 复制代码
auto f1 = [](int a, int b) { return a < b; };
cout << f1(1,2);

lambda原理是创建了一个匿名类,并重载了函数调用运算符(),相当于仿函数,实现了一个operator()方法

cpp 复制代码
auto print = []{cout << "Hello World!" << endl; };
相当于
class print_class
{
public:
    void operator()(void) const
    {
        cout << "Hello World!" << endl;
    }
};
auto print = print_class(); //此时print是一个函数对象,或者说仿函数

c++11使用std::function描述C++中所有可调用实体,它是可调用对象的包装器

cpp 复制代码
#include<functional>

// 声明一个返回值为int,参数为两个int的可调用对象类型
std::function <int(int, int)> Func;

// 普通函数
int add(int a, int b) { return a + b; }

// lambda表达式
auto mod = [](int a, int b){ return a % b; };

// 函数对象类
struct divide{
    int operator()(int denominator, int divisor){
        return denominator/divisor;
    }
};

 //类成员函数
class Work
{
    public:
        int func(int a ,int b)
        {
               return a*b;     
        }
}
int main()
{
    Func a = add;
    a(2,3);
    Func b = mod;
    Func c = divide;
    Work w;   //  成员函数   哪个类对象的  参数占位符1     占位2
    Func d = bind(&Work::func,  &w,   placeholders::_1 , placeholders::_2);
    d(3,4);
    Func e = bind(&Work::func,&w,3,4);  //bind出来应该是function<int()>类型,不需要参数了
    e(10,20); //结果还是3*4
    //e(); 由于Func是std::function<int(int, int)> 所以必须有两个int参数
    
    auto f = bind(&Work::func,&w,3,4);
    f(); //这样就可以   结果也是3*4
}

在其中用到bind函数,bind函数可以对已有函数进行封装,主要应用场景是需要接收一个函数作为参数,但已有的格式不符。 bind返回一个function

bind将参数和函数绑定,变成无参函数

bind将类成员函数和类对象绑定,一般想要传入类成员函数作为参数都用这个办法

如果返回值不符,可以用lambda表达式调用函数,忽略返回值以及添加返回值

参数格式不对或者需要增加也可以用lambda

相关推荐
fqbqrr5 小时前
2606C++,C++构的多态
开发语言·c++
biter down5 小时前
从 0 到 1 搭建 Python 接口自动化测试框架(博客系统实战)
开发语言·python
小欣加油5 小时前
leetcode56 合并区间
c++·算法·leetcode·职场和发展
Yolo_TvT6 小时前
C++:析构函数
c++
threelab7 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
武器大师727 小时前
lv_binding_js 代码解读
开发语言·javascript·ecmascript
不知名的老吴7 小时前
线程的生命周期之线程“插队“
java·开发语言·python
Hello:CodeWorld8 小时前
C 风格变参 vs C++ 变参模板:核心区别与选型指南
c语言·c++·算法
kaikaile19958 小时前
数字全息图处理系统(C# 实现)
开发语言·c#