- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
This answer声明 std::bind
按值返回一个对象,并且 this comment暗示分配给 std::function
将导致堆分配存储 std::bind
返回的值。
有没有办法避免这种堆分配,将std::bind
的返回值直接按值传递给另一个函数?
如果是这样,方法签名将用什么替换 std::function
?
更明确地说,我有一个如下所示的函数。
void runThisFunction(std::function<void()> func);
假设有一个具有以下签名的函数 foo
。
void foo(int a, int b);
现在,我将按如下方式调用 runThisFunction
。
runThisFunction(std::bind(foo, 1, 2));
在此调用中,std::bind
的输出被转换为 std::function
,动态内存分配作为此过程的一部分发生。
是否可以将 std::function
替换为一些其他声明,这些声明将直接按值接收 std::bind
的输出,从而避免动态内存分配?
最佳答案
Is it possible to replace
std::function
with some other declaration that would would receive the output ofstd::bind
directly by value, thereby avoiding the dynamic memory allocation?
是的;但是 std::bind
返回的类型是未指定的,所以你需要使用模板来捕获类型;
template <typename F>
void runThisFunction(F func);
关于内存分配...
In this invocation, the output of
std::bind
is converted into anstd::function
, and dynamic memory allocation occurs as part of this process.
可以使用动态内存(但不能使用 always ),这取决于绑定(bind)到 std::function
中的仿函数的大小和实现的质量。
此外,C++ 规范有这个 §20.12.12.2.1/11 ;
[Note: Implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects, for example, where
f
is an object holding only a pointer or reference to an object and a member function pointer. — end note ]
即使有内存分配,我也不会太在意。除非代码对性能至关重要并且您已经对其进行了测量,否则所需的间接访问应该不是问题。
请记住,对于您的情况,bind
中绑定(bind)的 foo
是一个指针,很可能无论如何都不会进行动态内存分配。
I started looking at this because I measured cache misses on the conversion due to unexpected slowness detected through instrumentation
因此,您对性能有一些可衡量的担忧……有一些替代方法可以将 std::bind
与 std::function
结合使用。 std::bind
是有用的通用 Binder ,但这并不意味着它的性能也足够好 - 制作您自己的。自定义仿函数可能性能更高。基于 lambda 的实现也很好看。不要忘记函数 foo
也可以与 std::function
一起使用,然后你就完全放弃了仿函数/绑定(bind)器(注意,签名需要匹配).
在上述引用中提到的“小对象”优化开始之前,关于对象需要多“小”的附注似乎在库实现之间略有不同。 p>
这里是coliru (libstdc++),std::function
的参数大小需要为 16 个字节或更少,在 MSVC 上,限制是 32 字节(这两个看起来都是 32 位平台)。使用 clang++ (libc++) 64 位编译时,此限制为 24 字节......这实际上取决于实现,在需要进行新
分配之前,它们将允许多少空间。
我不确定性能有多重要,但也可以为您的目标计算此限制,然后应用优化,使 std::function
的参数保持低于这些限制;例如使用指向 struct
的指针或引用(也 std::ref
)作为参数(但必须注意不要将它们悬空)。
关于c++ - 是否可以直接接受 `std::bind` 的输出作为值,而不转换为 std::function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36617358/
我正在本地编程应用程序,但是当我迁移到服务器时,使用此行出现解析错误: if(!is_array($data[array_keys($data)[0]])) 返回值: Parse error: syn
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
我已经开始尝试用 C++ 学习 Winsock,但我遇到了一些问题。我首先遵循 MSDN 上的 Winsock 示例(一个基本的客户端-服务器)。 http://msdn.microsoft.com/
我有一段使用 epoll 的代码,但它有问题。当我运行它时,它给出输出:服务器套接字()没问题......服务器绑定(bind)()没问题......3个4个接受:无效参数 我在 ubuntu lin
我正在寻找一种方法来接受 $_GET 变量作为文件路径,例如 - /page/test将转换为 page.php?page=test .我已经搜索过,但找不到任何可以帮助我的东西,因此我在这里问。 我
我想要一个只接受从 0 到 9 和减号的浮点数的正则表达式。 请帮忙。 最佳答案 ^[-+]?[0-9]*\.?[0-9]+$ ^ - 字符串开头 [-+]? - 0 或 1 符号指示符 [0-9]*
请问如何接受\r\n无需将其更改为 \\r\\n , 与 fgets . 我想让程序翻译 \r\n到换行符而不是将其打印为字符串。 当前代码: char buff[1024]; printf("Msg
我正在编写一个 Cocoa 应用程序,该应用程序需要在其 Dock 图标上接受已安装卷的滴落。它不是基于文档的;我打算将每个卷分派(dispatch)到 application:openFiles 中
我在 SQLite 中发现了这种意外行为。 SQLite 似乎接受 SQL 连接语法中的任意关键字。如果我不小心键入了 natural join 而不是 natural join,则会生成笛卡尔积。这
我在 Windows 窗体的同一个窗体上有一个 TreeView 和一个多行文本框。我有拖放设置,以便我可以将节点从 TreeView 拖到文本框并将文本插入文本框(这是有效的)。 我想增强这一点,以
我正在创建一棵类似于 D3 Layout Tree 的树并尝试绑定(bind)我的自定义 JSON 对象。切换树节点的代码如下。 function toggleAll(d) { if (d.c
所以,我希望能够向我的 DOB 字段发送空选项。 这是我的表单生成器: ->add('birthDate', DateType::class, array( 'widg
错误可以在这里看到:http://djaffry.selfip.com:8080/ 我希望索引页接受参数,无论是 mysite.com/search/param_here 或 mysite.com/?
我想知道标准 Scala 解析器组合器是否包含一个解析器,该解析器接受 Scala 语言本身也接受的相同标识符(如 Scala 语言规范第 1.1 节中所指定)。 StdTokenParsers 特征
我暂时使用以下行在我的 Android 手机上创建 ServerSocket: socketl = new ServerSocket(port, 0, InetAddress.getByName("1
今天早上刚刚发布了一个应用程序,我在所有可能的设备和模拟器上测试了它,但是当我从商店下载它时,应用程序在启动时崩溃。我在很多设备和iOS版本上测试过,但结果都是一样的: Incident Identi
我想要 5 个名字中最长的一个。我想我应该使用 compareTo() 方法或 length()? 输出必须是这样的: enter 5 names : Joey Mark Catherine Zach
抱歉,我不熟悉泛型,我可以创建一个如下所示的泛型类: public class InfoField { } 上面的类可以接受如下值: , User> 但是我可以有接受 map 对象作为值的类吗?
我想让一个单元格等于它上面的单元格。 当我写作时 =address(row()-1;column()) 它只是写入上面单元格的名称(例如,对于 B2,它写入 $B$1) 是否有一个函数可以输入地址并放
我正在使用Asm访问java字节码来获取方法信息。看来ClassReader类的accept方法是异步调用的,所以不可能立即获取方法信息。我该怎么办? 最佳答案 实际上,accept 方法是从您自己的
我是一名优秀的程序员,十分优秀!