Customize your std :: function / lambda callback carefully

I have something like the following structure. It is a container that calls a callback for all of its elements. fct

can be lambda, functor, or simple function pointer.

Now, in some cases, I would like to optionally configure fct

before I call it. For example, it fct

computes a complex function for each x. But he ContainerX

already knows the intermediate result - I would like to say the fct

intermediate result. If it fct

is a functor, it has state and can store an intermediate rsult. If it is a simple function, it cannot store the intermediate result and must be called without prior configuration.

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:

  • fct

    function<void(X* x, double result)>

  • fct

    , .

callback

, , .

, fct

?

+3




1


callback

, result

. :

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

      



, , .

There are essentially two different callback functions. One that accepts a function using an intermediate value. Someone who doesn't. Sometimes you need different signatures that do roughly the same thing. What are the overloads for :).

+3


source







All Articles