Is it possible to transform a nested C++11 bind expression? For example, in the code below, the bind expression associated with f
will first multiply its argument by two, before adding one to the result:
#include <iostream>
#include <functional>
using namespace std::placeholders;
int main(int argc, char *argv[])
{
auto add1 = [](int x) { return x+1; };
auto mul2 = [](int x) { return x*2; };
auto f = std::bind(add1, std::bind(mul2, _1));
std::cout << f(0) << '\n';
return 0;
}
Could we create a transformed version of f
which instead first adds one, then multiplies the result by two; the result would behave as if defined as:
auto f2 = std::bind(mul2, std::bind(add1, _1));
This example is simplified by the fact that it's structure is analogous to a list; whereas a bind expression is more generally a tree.
This is a brutal hack on VS2013, and is totally non-portable, but I was interested to see if I could make something work. It probably doesn't solve your problem, but I thought it was worth sharing. Because the return value of
std::bind
is implementation defined, portability is going to be a big roadblock on this problem. There's also lots handwaving on most of the template deduction.