I've been hacking some C++ 2D vector and matrix classes in preparation for a vector drawing program I'm planning to do. They are template expression based and therefore totaly unreadable. But they do generate kick ass code using the latest g++ snapshot.
Using the online snapshot compile form at codesourcery this function:
double dotty(Vector &x, Vector &y, Vector &z) { return dot(x, y+z); }compiles to this (x86) code (using -O2 -march=pentiumpro):
dotty__FR6VectorN20: pushl %ebp movl %esp, %ebp subl $24, %esp movl 8(%ebp), %eax movl 12(%ebp), %edx movl 16(%ebp), %ecx movl %ebp, %esp fldl (%eax) fldl (%edx) fldl 8(%eax) fxch %st(1) popl %ebp faddl (%ecx) fmulp %st, %st(2) fldl 8(%edx) faddl 8(%ecx) fmulp %st, %st(1) faddp %st, %st(1) retwhich is pretty damn good for that (very readable) sourcecode. There is none of the normal problems with temporary objects etc.
Unfortunately i had to fight quite a lot with the template syntax, so the code isn't very nice. It would be a lot nicer if convertion operators were actually used when matching arguments to template functions. I wonder why that isn't in the C++ standard?