重新绑定 std::function 的一个参数

rebind one parameter of std::function

本文关键字:一个 参数 std 新绑定 绑定 function      更新时间:2023-10-16

在我的C++项目中,我决定尝试一下new++功能。其中一个功能是通过std::bind将函数与std::function绑定。

现在我来到了一个用例,我必须重新绑定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);部分(将指针替换为other this)?

(编辑)上下文:

在我的应用程序中,有几个类继承自一个名为 BaseModel 的类。这些类是从自制的描述语言自动转译而来的。以下类代表一个简单的"小行星",它由另外两个"小行星"组成:

#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,它结合了(通过构造立体几何)两个新天体(第1颗小行星和第2颗小行星):

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
}

由于leftModelrightModel必须是同一类的新模型,我需要重新绑定之前在自动转译的类Test::scene中给出的第一个参数。

也许我走错了路。我希望额外的上下文可以解释为什么我会遇到这个问题。

正如@tobi303和其他人所指出的,second参数的函数签名过于受限,因为它需要一个空函数。没有什么可以重新绑定的 - 它不需要任何参数。

为了达到你看起来想要做的事情,你需要有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函数之前,并且越来越不需要它们。

如何执行fun->rebindLastParameter(other);部分(将指针替换为other this)?

一般来说,你不能。

std::function依赖于类型擦除,这意味着存储在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);
}

但这些不是通用解决方案,也不是很易于维护。

>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);