前言

std::function 作为标准库提供的函数指针,使用起来还是比较方便的,不过在使用过程中有一些需要注意的细节,这里做一个简单的记录。

基本使用

  • 头文件: #include <functional>
  • 语法:std::function<return_type(args_type)>
    #include <iostream>
    #include <functional>
    using namespace std;
    void func(int i, int g) {int count = 10;while (count > 0) {cout << "func: " << i  << " " << g<< endl;sleep(1);}
    }int main() {std::function<void(int,int)> job = func;job(10,10);return 0;
    }
    

    其中return_type 是函数指针的返回值类型,像上面案例中func函数的void类型。
    其中args_type 是函数指针的参数类型,有多个函数参数,就可以有多个数据类型。

函数指针作为参数 需要注意的事项

  1. 如果直接使用一个已经存在的函数来进行传参数,则需要声明调用者函数参数的std::function 为const类型。
    具体也就是类似 我们使用"dfafasd" 初始化一个 void test_string(std::string& res) 引用类型的字符串参数。这个时候因为是引用传递,而传递的时候却没有用变量,而是使用了一个常量,则需要保证这个函数的声明参数中有一个const ,才能将常量数据的地址正确得放在常量区域。即 正确的声明应该如 void test_string(const std::string& res)

    同理,std::function在使用一个已经存在的函数初始化的时候类似。如下代码是正确的:

    void  stl_func(const std::function<void(int, int)>& job, int num, int g) {job(num, g);
    }
    void func(int i, int g) {int count = 10;while (count > 0) {cout << "func: " << i  << " " << g<< endl;sleep(1);}
    }int main() {stl_func(func,10,1);return 0;
    }
    

    如果这个时候去掉了 stl_func 函数参数中的 const 声明,则会报如下错误:

    candidate function not viable: no know conversion from 'void()' to 'std::function<void(int,int)> &'
    for 1'st argument.
    
  2. 使用一个函数指针变量 来进行参数传递,则不需要调用者函数参数的 std::function 为const。
    还是如上代码,修改之后:

    void  stl_func(std::function<void(int, int)>& job, int num, int g) {job(num, g);
    }
    void func(int i, int g) {int count = 10;while (count > 0) {cout << "func: " << i  << " " << g<< endl;sleep(1);}
    }int main() {std::function<void(int, int)> job = func;// 使用函数指针 变量进行传递就没有问题stl_func(job, 10, 10);return 0;
    }
    
  3. 在类的成员函数之间 将 std::function 作为参数传递时需要注意 如下使用过程:

    #include <iostream>
    #include <functional>using namespace std;
    void stl_func_bak(std::function<void()>& job) {job();
    }class Cb {public:static void cb_func() {while(1) {cout << "cb_func: " << 10 << endl;usleep(100000);}}void stl_cb_func() {// 函数成员作为传递的参数需要确保编译期间就能够分配好函数内存// 函数栈需要在编译期间获得内存,所以需要声明 cb_func 成员函数 为 staticstl_func_bak(Cb::cb_func);}
    };int main() {Cb* cb = new Cb();cb->stl_cb_func();return 0;
    }
    

欢迎大家补充使用过程中遇到的问题

C++ std::function<void(int)> 和 std::function<void()> 作为函数参数的注意事项相关推荐

  1. linux c 字符串转int,Linux c/c+编程--std::string str; int转str

    1, 首先在使用std::string 时,需要include 哪个文件? 需要区分#include 和 #include #include 声明的是C语言的字符串处理函数,例如strcpy, str ...

  2. C语言 main 函数参数 main(int argc, char *argv[]) - C语言零基础入门教程

    目录 一.main 函数写法 二.main 函数参数简介 三.使用 main 函数参数 1.打印 main 函数参数 a.直接运行 exe 文件 b.打开 cmd 命令行窗口执行 exe 文件 c.打 ...

  3. C++——包装器std::function与绑定器std::bind

    C++--包装器std::function与绑定器std::bind 1.可调用对象的包装器 std::function是可调用对象的包装器.它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可 ...

  4. 【c++】4.std::shared_ptr、std::make_shared、 .get() 、.data()、void *p 的用法、裸指针

    std::shared_ptr.std::make_shared. .get() ..data().void *p 的用法.裸指针 (1)shared_ptr能够记录对象被引用的次数,主要被用来管理动 ...

  5. 【C++】40. std::array与int a[]方式创建数组的区别

    C++11中std::array的使用 std::array<type,size> array1; std::array<int,3> array1和 int a[]={1,2 ...

  6. Boost:在GPU上对int的std :: vector进行排序

    Boost:在GPU上对int的std :: vector进行排序 实现功能 C++实现代码 实现功能 Boost的compute模块,在GPU上对int的std :: vector进行排序 C++实 ...

  7. error: implicit instantiation of undefined template ‘std::vector<int>‘

    原始代码如下: #include <iostream> using namespace std;int main() {std::cout << "Hello, Wo ...

  8. c语言prog.c: in function 'main':,C语言 tr1 :: function和tr1 :: bind

    我将以下内容放入Ideone.com(和codepad.org): #include #include #include struct A { A(const std::string& n) ...

  9. g++ -std=c++_在C ++ std库中使用sort()

    g++ -std=c++ 介绍 (Introduction) Hey there! Today we are going to discuss the sort() function in the s ...

最新文章

  1. 基于椭圆拟合的环岛识别方法
  2. 折腾一天总结下安装centos的安装方法。。。
  3. python界面长什么样子-图形界面
  4. C# 语法练习(4): 类型转换
  5. ksrot php_php中ksort函数的功能起什么作用呢?
  6. phpcms v9 打开网站特别慢 增加数据库缓存方法
  7. 【恋上数据结构】回溯、剪枝(八皇后、n皇后)、LeetCode51.N皇后、LeetCode52.N皇后 II
  8. OpenSuSE 网络配置
  9. hdu4734 F(x)
  10. 计量经济学计算机答案第一章,计量经济学计算机作业
  11. 2017第25届春季中西部(重庆)医疗器械展览会会刊(参展商名录)
  12. 矩阵列的线性组合公式
  13. Cannot start container web: iptables failed: iptables -t nat -A DOCKER -p tcp -d 0/0 --dport 32797
  14. 12c rac在安装grid软件执行root.sh脚本的时候出现PRCT-1431,CLSRSC-180
  15. 用keil编写C语言流水灯程序,简述关于Keil、STM32 用C++编写流水灯程序
  16. oracle查询sql走索引吗,Oracle SQL不走索引小记
  17. R语言|如何进行t检验
  18. 深入实践 ES6 Proxy Reflect
  19. LeetCode Java刷题笔记—876. 链表的中间结点
  20. 广告营销的三大制胜法则

热门文章

  1. vue.js安装过程(npm安装)
  2. MVC 中的 ViewModel
  3. JS+CSS点击弹出登陆框代码
  4. 《帝企鹅日记》观后感
  5. Android模拟器学framework和driver之传感器篇1(linux sensor driver)
  6. EF-Entity Framework 相关技术点收集贴
  7. php去除字符串首尾空格(包括全角)(转)
  8. (C#加密)幻术-大踲无形
  9. SQL2000联机丛书:使用和维护数据仓库
  10. 基于FCN,U-Net的深度学习医学影像分割算法(细胞分割算法)以及传统算法分析