参考:原文:C++中的各种可调用对象 (更舒服的排版:C++中的各种可调用对象

这篇博客真的总结的很好,强烈推荐

 

--------------------------------------------------------一些笔记---------------------------------------------------------------------------

c++ 可调用对象

c++ 可调用对象

c++ 可调用对象

c++ 可调用对象

c++ 可调用对象

这里也可以不指明lambda的返回类型,即去掉   ->int

感受一下lambda和普通函数定义的差别:

int sum(int x, int y){return x+y;}
auto sum =[](int x, int y){return x+y;}

c++ 可调用对象

c++ 可调用对象

这里对类成员函数的支持使用std::bind,因为类成员函数的参数列表中有this指针

c++ 可调用对象

具体的使用 std::function 描述所有可调用实体code:

#include <iostream>
#include <functional>

std::function<int(int, int)> SumFunction;

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

class Calcu
{
public:
    int base = 20;
    // 类的成员方法,参数包含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 = 2; 
    int y = 5;

    // 普通函数
    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 = 10;
    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;

    getchar();
    return 0;
}
View Code

相关文章:

  • 2021-08-24
  • 2021-11-16
  • 2022-12-23
  • 2021-07-21
  • 2021-10-15
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
  • 2018-04-24
  • 2021-07-25
  • 2022-01-21
相关资源
相似解决方案