问题标签 [std-function]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c++ - 使用 `std::function` 从另一个对象调用一个对象的成员
我正在尝试在对象之间创建一个通信系统,其中控制器对象保留其他对象的成员函数向量,并在需要时调用/调用它们。
我阅读了几篇关于使用的文章std::function
,但我找不到涵盖此用例的文章。
我的代码是:
奇怪的是,当我运行此代码时,我自己的代码中没有任何编译器错误,但我从 STL 文件xmemory0
和vector
. 错误是:
我究竟做错了什么?如何使此代码运行?
(我的 IDE 是 Microsoft Visual Studio 2012。)
c++ - 检查是否将 std::function 分配给 nullptr
我想知道是否有任何方法可以检查您分配给 an 的函数指针是否std::function
为nullptr
. 我期待!
-operator 这样做,但它似乎只在函数被分配了 type 的东西时才起作用nullptr_t
。
我写了这个辅助函数来解决这个问题。
c++ - 在基类中,如何定义一个容器来包含函数 obj,它可以是派生类的任何函数?
我想在基类中定义一个容器,其中包含函数 obj 或任何可以实现我的目的的东西。这些函数 obj 可以调用派生类的函数。它们都采用相同的参数。
c++ - std::function 返回任何可求和类型?
我的并行算法图数据结构具有以下迭代器方法:
我本可以将该handle
函数声明为模板参数,但我认为 C++11 中的首选方式是使用std::function
.
现在我想使用带有这样一个迭代器的 OpenMP 执行并行缩减。每次调用的返回值handle
都减少为一个总和。使用函数模板,这看起来像:
using 的等价物是什么std::function
?我可以定义handle
返回 double 或 int 的类型吗(因为函数体同时适用于两者)。
c++ - 如何将非静态类成员`std::bind` 绑定到 Win32 回调函数`WNDPROC`?
我正在尝试将非静态类成员绑定到标准WNDPROC
函数。我知道我可以通过使类成员静态来简单地做到这一点。但是,作为 C++11 STL 学习者,我对使用<functional>
标题下的工具非常感兴趣。
我的代码如下。
当我按原样运行它时,它会给出错误消息:
function - 如何在 std::function 中使用多态性?
假设我有 2 个课程:
而且我想使用std::function
接收A类型的任何东西,但分配给它接收从A继承的类的方法(如B)。
这显然是行不通的,因为编译器不会隐式地向下转换。
但是我怎么能做这样的事情呢?基本上我想保存一些基本 std::function 类型的映射,并且在每个映射值中它将保存std::function
一个派生类型,如B。
有没有办法将强制转换运算符绑定到占位符?
c++ - std::function 可以序列化吗?
这是一个理论问题。假设有一些对象包含订阅这些对象事件的回调函数列表。现在我们要将这些对象存储在磁盘上。是可std::function
序列化的吗?
c++ - FastDelegate and lambdas - can't get them to work (Don Clugston's fastest possible delegates)
I'm trying to create a C++11 implementation of Don Clugston's Member Function Pointers and the Fastest Possible C++ Delegates, and make it work as a drop-in std::function
replacement.
I construct lambda FastDelegates like this:
Now, some tests:
As you can see, when the lambda is "trivial" (no captures), copying it by value and taking its address works. But when the lambda stores some kind of state (captures), I cannot just copy it by value into the FastFunc
.
I tried getting the lambda by reference, but I cannot do that when it's a temporary like in the example.
I have to somehow store the lambda inside the FastFunc
, but I don't want to use std::shared_ptr
because it's slow (I tried a different fastdelegate implementation that used it, and its performance was comparable to std::function
).
How can I make my implementation of Don Clugston's fastest possible C++ delegates work with lambdas that capture state, preserving the amazing performance of fastdelegates?
c++ - 如何使模板函数成为另一个模板函数的参数?
我有一个包含各种算法的类:
它们可以通过以下方式调用:
现在我想创建一个函数,它将采用任何“算法”函数并在调用该函数之前和之后执行相同的步骤。
这是我所拥有的:
当我尝试像这样调用函数时:
我得到的错误是:
我该如何设置一个通用例程,它将所需的算法作为参数?
编辑:
此解决方案按需要工作。它看起来像这样:
c++ - std::bind 的返回类型可隐式转换为两个不同的显式构造函数
给定两个explicit
构造函数重载(基于不同std::function<...>
的类型),返回值std::bind
可以选择其中一个(从而使调用不明确)
如果我注释掉任何一个,那么代码就会编译!
我会认为让构造函数explicit
要么选择正确的重载,要么阻止两者都被选中?
当然,在我绑定的时候明确地创建一个:std::function
但是,我很困惑为什么类型推导不起作用?
- 如果 from 的返回值
std::bind
都不匹配两个构造函数签名,则它们的事实explicit
应该防止两者都被选中。 - 如果 from 的返回值
std::bind
与两个构造函数签名之一匹配,则它们的事实explicit
应该会导致选择正确的一个。
这里实际发生了什么?
完整的工作代码如下: