Closing (with std::function ) - using lambda functions - in C ++ 11 are appropriate and recommended here. So that
std::function<int(int)> translate (int delta) { return [delta](int x) {return x+delta;} }
then you can further code:
auto t3 = translate(3);
Now t3 is a βfunctionβ that adds 3, later:
int y = t3(2), z = t3(5);
and, of course, y be 5 and z be 8.
You can also use some JIT compilation library to generate machine code on the fly, for example. GCCJIT or LLVM or libjit or asmjit ; or you can even generate some C ++ (or C) code in some generated /tmp/mygencod.cc file and fork it (e.g. g++ -Wall -O -fPIC /tmp/mygencod.cc -shared -o /tmp/mygencod.so ) into the /tmp/mygencod.so plugin , then dynamically load this plugin using dlopen on POSIX systems (and later dlsym to get a pointer to a function on behalf of; beware name mangling for C ++). I do such things in GCC MELT
source share