Linked by Amjith Ramanujam on Wed 20th Aug 2008 19:37 UTC
Thread beginning with comment 327488
To view parent comment, click here.
To read all comments associated with this story, please click here.
To view parent comment, click here.
To read all comments associated with this story, please click here.





Member since:
2006-01-02
For so many reasons:
- because you might want natural mathematical notation:
Vector result = x1 * vector1 + x2 * vector2;
- because even if you do a C function doing this, like
void foo(float x1, const float *v1, float x2, const float *v2, float *result);
then you will have to redo the work all over again when you want another function that takes not 2 but 3 vectors, returning x1*v1+x2*v2+x3*v3
- because C++ template metaprogramming techniques (specifically, expression templates) allow you have this API,
result = x1*v1+x2*v2+x3*v3;
without the introduction of temporaries so that the code will compile to completely optimized assembly (in particular, the arrays traversed only once). I'm not saying that the trivial implementation does this, I'm saying that C++ makes it possible to make a clever implementation.
- because C++ template metaprogramming doesn't stop here, the compile-time metadata on expression types that you gather can be used to make explicit use of SSE instructions where appropriate (it works very well in above example), and to intelligently determine where to introduce temporaries.
To see that in action, see our lib (link i gave above, don't want to give more links as i'd be self-advertising. In my defense this is LGPL'd software).
Here's one of the reasons why C++ is at a turning point. For a long time, c++ template metaprogramming has been known to be possible, but too heavy for the compiler. Recent compilers change that (e.g. GCC >= 4.2) -- current C++ frontends are becoming very clever and robust. C++0x will make template metaprogramming much more convenient from the programmer's point of view (concepts, static asserts, compile-time constants, variadic templates, template typedefs, rvalue references... we're spoiled)