Suppose we have two sorts of classes
Input
result_type
set(result_type)
Output
result_type
result_type get() const
Input
classes as member variables, on which its output dependsGiven an output class and several input classes (arbitrary number), consider the following procedure:
set()
with an appropriate value (defined beforehand)get()
on the ouput class and collect the result.This procedure can be seen as a call to a function taking the input's values as arguments an returning the output value.
Write the functor that constructs such a variadic function in the general case.
Constraints are: C++ (most likely C++11), arbitrary number of input classes of possibly different Input::result_type
s. Note that Input::result_type
is not necessarily related to Output::result_type
. Aim should first be efficiency, but there's a big bonus if the code is elegant and readable.
Details: For those who wonder how Output
is related to Input
, one could imagine that Input
has a result_type get() const
method as well, which returns whatever you provided via set()
. Output
then has a constructor that takes various Input
s, and stores them (or their reference) as member variables. Output::get()
then does some math by using the return values of its input's get()
methods, and returns some result of type Output::result_type
.
Here's my solution, others welcome
#include <functional>
template <class Output, class... Inputs>
std::function<typename Output::result_type(typename Inputs::result_type...)>
make_function(const Output& output, Inputs&... inputs) {
return[&](typename Inputs::result_type... input_vals) {
int dummy[]{0, (inputs.set(input_vals),0)...};
return output.get();
};
}
the int dummy[]
line is due to @ecatmur (https://stackoverflow.com/a/12515637/958110)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With