Thursday, August 7, 2008

mixing php and the c++ preprocessor?

here I go again, thinking and complaining about the well known limits of c++ meta programming,
the most known complaint candidate being lack of partial template specialization for functions.
Meta-programming seems to me to be very close to web server side scripting a la php per example. Because in the end, this is what the meta in meta-programming means...
generating source code via 'meta' code. well what about using something like a php processor to meta-program c++ files??

One could do something like #include "Vector.php.hpp?dims=3" this would include a meta programmed header for a 3d vector, one advantage is the the resulting header would be plain c++, it would be possible generate functions that take 3 arguments, like per example
VectorT::VectorT(Type value1, Type value2, Type value3) which is currently impossible with standard meta-programming.
actually the generated class could directly be generated as Vector3 or Vector3D, where ususally
it would be Vector<3>, of course this can be typedef'd and I am a big fan of typedef's they are great for writing fire and forget, self refactoring code. But anyway, it would be possible...
Taking it to the extreme we would probably be able to do something like this:

#include "Vector.php.hpp?class=FunkyVector3D, dims=3"
, and have a resulting class called FunkyVector3D! changing names of classes at will, would that be a maintenance disaster? or simply more responsability?

Planning will definitely be needed so that things don't get out of control and turn into a cryptic mess.

I suspect though, that without some perprocessor features to help with this, issues will arise (name collisions, linker errors, classes not found...).

Time for bed though, I will think about it some more when I have more time, but this could definitely be a cool experiment.

No comments: