Define C++ function at runtime -
i'm trying adjust mathematical code i've written allow arbitrary functions, seem able pre-defining them @ compile time, seems clunky. i'm using function pointers, far can see same problem arise functors. provide simplistic example, forward-difference differentiation code used is:
double xsquared(double x) { return x*x; } double expx(double x) { return exp(x); } double forward(double x, double h, double (*af)(double)) { double answer = (af(x+h)-af(x))/h; return answer; }
where either of first 2 functions can passed third argument. do, however, pass user input (in valid c++) rather having set functions beforehand. appreciated!
historically kind of functionality you're asking has not been available in c++. usual workaround embed interpreter language other c++ (lua , python example designed being integrated c/c++ apps allow scripting of them), or create new language specific application own parser, compiler, etc. however, that's changing.
clang new open source compiler that's having development apple leverages llvm. clang designed ground usable not compiler c++ library can embed applications. haven't tried myself, should able want clang -- you'd link library , ask compile code users input application.
you might try checking out how clamav team did this, new virus definitions can written in c.
as other compilers, know gcc added support plugins. maybe possible leverage bridge gcc , app, because gcc wasn't designed being used library beginning might more difficult. i'm not aware of other compilers have similar ability.
Comments
Post a Comment