Alternatives to C++ templates?
c++, metaprogramming
Solution
I am not sure this is what should want, I have used code generators to produce C++ code. In particular, python cheetah. You basically embed straight python code inside your C++ code and run through cheetah preprocessor.it allows to do pretty complex computations much easier than using templates or C++ preprocessor, plus you get all python libraries and extensions. on the other hand it makes debugging harder if something goes wrong.if you are interested I could provide some examples and Emacs mode for editing cheetah C++ programs.
If you need something less powerful and want to stay within C++ C only, take a look at boost preprocessor, here. It takes a bit of time to get used to it, but could make life really easy when the repetitive code is involved
okay, I am pasting cheetah example, give me a few minutes:
#if defined (__INTEL_COMPILER)
#pragma vector aligned
#endif
for(int a = 0; a < $N; ++a) {
/// for functions in block
%for ii, (fi,fj) in enumerate(fb)
%set i = ii + ifb
/// can also use (ix,iy,iz)=fi[0:2], need to clean up when not lazy
%set ix = fi[0]
%set iy = fi[1]
%set iz = fi[2]
%set jx = fj[0]
%set jy = fj[1]
%set jz = fj[2]
q$(i) += Ix(a,$(ix),$(jx))*Iy(a,$(iy),$(jy))*Iz(a,$(iz),$(jz));
%end for
/// end for functions in block
}
produces (after running `cheetah ...`)
#if defined (__INTEL_COMPILER)
#pragma vector aligned
#endif
for(int a = 0; a < 6; ++a) {
q0 += Ix(a,0,1)*Iy(a,0,0)*Iz(a,0,0);
q1 += Ix(a,1,1)*Iy(a,0,0)*Iz(a,0,0);
q2 += Ix(a,0,1)*Iy(a,1,0)*Iz(a,0,0);
q3 += Ix(a,0,1)*Iy(a,0,0)*Iz(a,1,0);
q4 += Ix(a,0,0)*Iy(a,0,1)*Iz(a,0,0);
q5 += Ix(a,1,0)*Iy(a,0,1)*Iz(a,0,0);
q6 += Ix(a,0,0)*Iy(a,1,1)*Iz(a,0,0);
q7 += Ix(a,0,0)*Iy(a,0,1)*Iz(a,1,0);
q8 += Ix(a,0,0)*Iy(a,0,0)*Iz(a,0,1);
q9 += Ix(a,1,0)*Iy(a,0,0)*Iz(a,0,1);
}
which is a regular C++ code
lines starting with % are interpreted as python statements by cheetah preprocessor. /// are cheetah comments. Defaults use # as python statements, but I changed them to avoid collision with C preprocessor directives. `%end` must be used to terminate python blocks. Variables in C++ code which start with $ are replaced by python variables.
Do you want examples using boost preprocessor?
Problem
I think meta programming is very very cool. In particular, I love lisp macros. However, I think C++ template suck because: 1. they slow down compile time (even with precompiled headers that end up being 50MB big if you include any of he STL stuff). 2. they give terrible compiler/syntax errors that are counintuitive 3. they weren't desinged for complicated meta programming in the first place (generating comipler errors for prime numbers / showing templates are turing complete was a big deal back in the day). Having said all that, is there a decent alternative for C++ meta programming? something like *.m -> meta compiler -> *.cpp -> g++ -> executable ? EDIT: I'm thikning along the lines of "custom code generations scripts." I'm just wondering if there's a really good set of them out there.