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.
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.
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
fctasfunction<void(X* x, double result)> - I could define
fctas an abstract class which has the actual function and a preconfiguration function.
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.boost::optional.