C++ std::function<void(int)> 和 std::function<void()> 作为函数参数的注意事项
前言
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 是函数指针的参数类型,有多个函数参数,就可以有多个数据类型。
函数指针作为参数 需要注意的事项
如果直接使用一个已经存在的函数来进行传参数,则需要声明调用者函数参数的
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.
使用一个函数指针变量 来进行参数传递,则不需要调用者函数参数的
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; }
在类的成员函数之间 将 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()> 作为函数参数的注意事项相关推荐
- linux c 字符串转int,Linux c/c+编程--std::string str; int转str
1, 首先在使用std::string 时,需要include 哪个文件? 需要区分#include 和 #include #include 声明的是C语言的字符串处理函数,例如strcpy, str ...
- C语言 main 函数参数 main(int argc, char *argv[]) - C语言零基础入门教程
目录 一.main 函数写法 二.main 函数参数简介 三.使用 main 函数参数 1.打印 main 函数参数 a.直接运行 exe 文件 b.打开 cmd 命令行窗口执行 exe 文件 c.打 ...
- C++——包装器std::function与绑定器std::bind
C++--包装器std::function与绑定器std::bind 1.可调用对象的包装器 std::function是可调用对象的包装器.它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可 ...
- 【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能够记录对象被引用的次数,主要被用来管理动 ...
- 【C++】40. std::array与int a[]方式创建数组的区别
C++11中std::array的使用 std::array<type,size> array1; std::array<int,3> array1和 int a[]={1,2 ...
- Boost:在GPU上对int的std :: vector进行排序
Boost:在GPU上对int的std :: vector进行排序 实现功能 C++实现代码 实现功能 Boost的compute模块,在GPU上对int的std :: vector进行排序 C++实 ...
- error: implicit instantiation of undefined template ‘std::vector<int>‘
原始代码如下: #include <iostream> using namespace std;int main() {std::cout << "Hello, Wo ...
- 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) ...
- g++ -std=c++_在C ++ std库中使用sort()
g++ -std=c++ 介绍 (Introduction) Hey there! Today we are going to discuss the sort() function in the s ...
最新文章
- 基于椭圆拟合的环岛识别方法
- 折腾一天总结下安装centos的安装方法。。。
- python界面长什么样子-图形界面
- C# 语法练习(4): 类型转换
- ksrot php_php中ksort函数的功能起什么作用呢?
- phpcms v9 打开网站特别慢 增加数据库缓存方法
- 【恋上数据结构】回溯、剪枝(八皇后、n皇后)、LeetCode51.N皇后、LeetCode52.N皇后 II
- OpenSuSE 网络配置
- hdu4734 F(x)
- 计量经济学计算机答案第一章,计量经济学计算机作业
- 2017第25届春季中西部(重庆)医疗器械展览会会刊(参展商名录)
- 矩阵列的线性组合公式
- Cannot start container web: iptables failed: iptables -t nat -A DOCKER -p tcp -d 0/0 --dport 32797
- 12c rac在安装grid软件执行root.sh脚本的时候出现PRCT-1431,CLSRSC-180
- 用keil编写C语言流水灯程序,简述关于Keil、STM32 用C++编写流水灯程序
- oracle查询sql走索引吗,Oracle SQL不走索引小记
- R语言|如何进行t检验
- 深入实践 ES6 Proxy Reflect
- LeetCode Java刷题笔记—876. 链表的中间结点
- 广告营销的三大制胜法则