java voidfunction,使用`std :: function< void(...)>`来调用非void函数
发布日期:2022-03-15 11:49:53 浏览次数:22 分类:技术文章

本文共 1867 字,大约阅读时间需要 6 分钟。

A while ago I used std::function pretty much like this:

std::function func = [](int i) -> int { return i; };

Basically, I did this because I wanted to store different function objects in a std::function, but I didn't want to restrict the return types of these functions. Since this seemed to work, I went with it. But I'm not convinced that it is safe to use, and I haven't been able to find any documentation on it. Does anyone know whether this usage is legitimate? Or more generally, what the rules are for the object which can safely be assigned to a std::function?

Edit

For clarification, the issue I'm concerned with is that the lambda function returns an int, while func is declared with return type void. I'm not sure if this is OK, especially once a call to func() is made.

解决方案

Your code has undefined behavior. It may or may not work as you expect. The reason it has undefined behavior is because of 20.8.11.2.1 [func.wrap.func.con]/p7:

Requires: F shall be CopyConstructible. f shall be Callable (20.8.11.2) for argument types ArgTypes and return type R.

For f to be Callable for return type R, f must return something implicitly convertible to the return type of the std::function (void in your case). And int is not implicitly convertible to void.

I would expect your code to work on most implementations. However on at least one implementation (libc++), it fails to compile:

test.cpp:7:30: error: no viable conversion from 'int (int)' to 'std::function'

std::function ff = f;

^ ~

Ironically the rationale for this behavior stems from another SO question.

The other question presented a problem with std::function usage. The solution to that problem involved having the implementation enforce the Requires: clause at compile time. In contrast, the solution to this question's problem is forbidding the implementation from enforcing the Requires: clause.

转载地址:https://blog.csdn.net/weixin_34574003/article/details/118839641 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:启动计算机管理服务,是哪一个启动项控制了计算机管理--服务下的wireless zero configuration无法开机自行启动!如何解决,为盼...
下一篇:冯偌依曼计算机的基本原理是,03级计算机专业《计算机组成原理》试卷A

发表评论

最新留言

很好
[***.229.124.182]2024年04月05日 07时19分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【Android Linux内存及性能优化】(十) 进程冷启动和热启动的区别 2019-04-26
【高通SDM660平台 Android 10.0】(20) --- Actuator 与 Kernel Actuator代码分析 2019-04-26
【高通SIM卡】 单卡配置 2019-04-26
iperf工具吞吐量测试 2019-04-26
【高通SDM660平台 Android 10.0】(19) --- Camera_focus、Camera_snapshot、volume_up 按键工作原理分析 2019-04-26
【高通SDM660平台 Android 10.0】(18) --- Camera start_session() 过程分析 2019-04-26
【高通SDM660平台 Android 10.0】(21) --- 高通Camera persist使用手册 2019-04-26
【高通SDM660平台 Android 10.0】(22) --- Flashlight 及 Kernel Flashlight 代码分析 2019-04-26
【LeetCode #7 题解】 整数反转 2019-04-26
【经典算法实现 1】选择排序法 O(n^2) 2019-04-26
【经典算法实现 2】递归 2019-04-26
进程同步机制(软件同步,硬件同步,信号量,管程) 2019-04-26
操作系统进程状态和状态转换详解 2019-04-26
【LeetCode #8 题解】 字符串转换整数 (atoi) 2019-04-26
【LeetCode #9 题解】 回文数 2019-04-26
经典的进程同步问题-----哲学家进餐问题详解 2019-04-26
【FFmpeg解码实战】(5)实现FFmpeg4.3 + SDL2视频播放器(添加独立线程和队列)(C++) 2019-04-26
【车机xxx视频需求实现 1】 - 需求分析 及 实现思路(车内DMS/AVR/ROA摄像头) 2019-04-26
【LeetCode #47 题解】 带重复全排列 II(递归回溯法、非递归实现) 2019-04-26
【车机xxx视频需求实现 2】 - 车内DMS/AVR/ROA三个摄像头虚拟化代码实现 2019-04-26