Tranparently配置std :: function / lambda回调

时间:2021-01-13 18:01:40

I have something like the following structure. It's a container which invokes a callback on all its elements. fct could be a lambda, a functor or a plain function pointer.

我有类似以下结构的东西。它是一个容器,它调用所有元素的回调。 fct可以是lambda,functor或普通函数指针。

Now, in some cases I'd like to optionally configure fct before I call it. For example, fct computes a complicated function on each x. But ContainerX already knows an intermediate result - I would like to tell fct the intermediate result. If fct is a functor, it has a state and it can store the intermediate rsult. If it's a plain function, it can not store the intermediate result and should be called without the pre-configuration.

现在,在某些情况下,我想在调用它之前选择配置fct。例如,fct计算每个x上的复杂函数。但是ContainerX已经知道了一个中间结果 - 我想告诉fct中间结果。如果fct是一个仿函数,它有一个状态,它可以存储中间的rsult。如果它是普通函数,则它不能存储中间结果,应该在没有预配置的情况下调用。

class ContainerX{
    void callback(function<void(X* x)> fct){
        // 1. Tell fct the intermediate result, if it is able to be configured
        // 2. Call fct for all x in the container
    }  
}

There are basically many ways to do this:

基本上有很多方法可以做到这一点:

  • I could define fct as function<void(X* x, double result)>
  • 我可以将fct定义为函数 (x>
  • I could define fct as an abstract class which has the actual function and a preconfiguration function.
  • 我可以将fct定义为具有实际功能和预配置功能的抽象类。

But these solution require me to change the signature of callback in a way that I can no longer call it with plain functions as well as with functors.

但是这些解决方案要求我以一种我不能再使用普通函数和函子调用它的方式来改变回调的签名。

Is there a way to transparently, optinally configure fct?

有没有办法透明地,可选地配置fct?

1 个解决方案

#1


3  

I would create an overload of callback does use the result parameter. It would look something like this:

我会创建一个回调的重载确实使用结果参数。它看起来像这样:

class ContainerX{
    void callback(function<void(X* x)> fct){  
    }  
    void callback(function<void(X* x, double result)>){   
    }  
}

But these solution require me to change the signature of callback in a way that I can no longer call it with plain functions as well as with functors.

但是这些解决方案要求我以一种我不能再使用普通函数和函子调用它的方式来改变回调的签名。

There are in essence two different callback functions. One which takes a function using an intermediate value. One that doesn't. Sometime you want different signatures which do roughly the same thing. That's what overloads are for:).

本质上有两种不同的回调函数。使用中间值获取函数的一个。一个没有。有时您需要不同的签名,这些签名大致相同。这就是重载的原因:)。

#1


3  

I would create an overload of callback does use the result parameter. It would look something like this:

我会创建一个回调的重载确实使用结果参数。它看起来像这样:

class ContainerX{
    void callback(function<void(X* x)> fct){  
    }  
    void callback(function<void(X* x, double result)>){   
    }  
}

But these solution require me to change the signature of callback in a way that I can no longer call it with plain functions as well as with functors.

但是这些解决方案要求我以一种我不能再使用普通函数和函子调用它的方式来改变回调的签名。

There are in essence two different callback functions. One which takes a function using an intermediate value. One that doesn't. Sometime you want different signatures which do roughly the same thing. That's what overloads are for:).

本质上有两种不同的回调函数。使用中间值获取函数的一个。一个没有。有时您需要不同的签名,这些签名大致相同。这就是重载的原因:)。