在我的C ++项目中,我决定尝试新的++功能。其中一项功能是通过std::function
将函数与std::bind
绑定。
现在我来到一个用例,我必须重新绑定 std::function
。
考虑以下简化代码:
class Test
{
public:
void first()
{
second(bind(&Test::third, this));
}
void second(function<void()> fun)
{
Test *other = new Test();
fun->rebindLastParameter(other); // How can I do this?
fun();
}
void third()
{
// This is called in context of "other"
}
}
如何执行fun->rebindLastParameter(other);
部分(将this
指针替换为other
)?
(编辑)背景:
在我的应用程序中,有几个类继承自名为BaseModel
的类。这些类是从自制的描述语言自动转换而来的。下面的类代表一个简单的&#34;小行星&#34;其中包括另外两个&#34;小行星&#34;:
#pragma once
#include "BaseModel.h"
#include <functional>
using namespace std;
using namespace std::placeholders;
class Test : public BaseModel
{
public:
Test(const OBB& start_obb) : BaseModel(start_obb) {}
void __init() {
scene();
}
void scene() {
combine(bind(&Test::asteroid, this),bind(&Test::asteroid, this));
}
void asteroid() {
translate(random_float(),random_float(),random_float());
sphere(7);
repeat(400,bind(&Test::impact, this));
}
void impact() {
auto p1 = random_surface_point();
select_sphere(p1,random_float() * 2 + 1,1.0);
normalize(p1);
translate_selection(-p1 * random_float() * 0.4);
}
};
问题在于函数BaseModel::combine
,它结合了(通过构造实体几何体)两个新对象(第一个小行星和第二个小行星):
void BaseModel::combine(function<void()> left, function<void()> right)
{
BaseModel *leftModel = (BaseModel*) ::operator new (sizeof(BaseModel));
BaseModel *rightModel = (BaseModel*) ::operator new (sizeof(BaseModel));
leftModel->initWithStartOBB(*this->obb);
rightModel->initWithStartOBB(*this->obb);
auto newLeft = bind(left, leftModel); // This does not work
auto newRight = bind(right, rightModel); // This does not work
newLeft();
newRight();
// ... CSG stuff
}
由于leftModel
和rightModel
必须是同一个类的新模型,我需要重新绑定我之前在自动编译的类{{1}中提供的第一个参数}}
也许我走错了路。我希望额外的背景可以解释为什么我遇到了这个问题。
答案 0 :(得分:3)
正如@ tobi303和其他人所指出的那样,second
参数的函数签名太受约束,因为它需要一个nullary函数。没有什么可以重新绑定 - 它不需要任何参数。
为了实现类似于你想要做的事情,你需要让second
的论点不那么紧缩。有几种方法(使它成为function
或指向成员函数的指针);下面显示了一个采用一元参数的函数进行模板参数化的设置:
#include <functional>
using namespace std;
class Test
{
public:
void first()
{
second([](Test *p){p->third();});
}
template<class Fn>
void second(Fn fn)
{
Test *const other = new Test();
fn(other);
}
void third()
{
// This is called in context of "other"
}
};
int main()
{
Test t;
t.first();
}
个人观点(一个为我赢得几个赞成票):我认为使用许多库和技术,大多数情况bind
肯定,function
的大多数情况 - 只是在当前的lambda函数之前,并且对它们的需求越来越少。
答案 1 :(得分:2)
如何执行
fun->rebindLastParameter(other);
部分(将this
指针替换为other
)?
一般情况下,你不能。
std::function
依赖于type-erasure,这意味着std::function
中存储的对象的原始类型不属于该类型,并且不易访问。
如果您确定fun
肯定存储bind(&Test::third, this)
返回的对象,则无需重新绑定参数,只需修改fun
即可完全保留具有正确参数的不同函数对象,即
fun = bind(&Test::third, other);
如果您知道它肯定会存储bind( &Test::???, other)
返回的对象,其中&Test::???
是完全与Test::third
相同签名的任何成员函数,那么您可以执行像这样的东西:
using binder_type = decltype(bind(&Test::third, this));
if (auto target = fun.target<binder_type>())
{
// *target is the result of the bind() expression
}
但是仍然无法修改*target
处的对象来替换它所拥有的Test*
指针。
如果您知道可以使用的功能集,那么您可以这样做:
using binder_foo_type = decltype(bind(&Test::foo, this));
using binder_bar_type = decltype(bind(&Test::bar, this));
using binder_baz_type = decltype(bind(&Test::baz, this));
if (fun.target<binder_foo_type>())
{
fun = std::bind(&Test::foo, other);
}
else if (fun.target<binder_bar_type>())
{
fun = std::bind(&Test::bar, other);
}
else if (fun.target<binder_baz_type>())
{
fun = std::bind(&Test::baz, other);
}
但这些不是通用解决方案,也不是很容易维护。
答案 2 :(得分:1)
std::mem_fn
正是您所寻找的,因为它为成员指针生成包装器对象。
std::function<void(Test*)> f = std::mem_fn(&Test::third);
f(this);
Test *other = new Test();
f(other);
使用std::bind
,使用引用Test
对象的乐趣。这样你就可以用一个新对象来调用它
void second(std::function<void(const Test&)> fun)
{
Test *other = new Test();
fun(*other);
}
或使用this
对象
fun(*this);