"Andre" <fo********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
Hi,
I've built a math library that does some matrix multiplications and
other linear algebric functions. I read some place about generics or
class templates and I was wondering if this will help me in any way? I
have structures and classes with the same logic but that which deal with
different types. Moreover, I would like to know if there are any
performance benifits or drawbacks and how templates are or can be
important to numerical code (typically high-performance code). Thanks,
regards
-Andre
C++ generics will not make your code run any faster (assuming your code is
good). One of the main benefits of template programming is source code
reuse (reduced development time and being able to reuse the code in more
general cases).
There are a couple of possible drawbacks if templates are overused (ie;
instantiating (literally) a million different types using the same source
code still means a million different types --> long compile times and code
bloat - but if you need a million different types, however, then by all
means, let the compiler do the work for you). For most applications,
however, the potential drawbacks are insignifcant - you should just be aware
of them.
If used effectively, templates will not have any runtime drawbacks
relative to hand-coded loops, etc. In particular, I'm referring to generic
algorithms at this point (like transform, for_each, merge, sort, etc).
Compile-time mathematics using templates is primitive allowing only for
integers --> no arbitrary precision and usually limited to numbers < 2^32-1,
for example. I would not expect compile-time addition/subtraction to be of
substantial use for your library.
Generic programming is a way of thinking that is very powerful and
versatile if done correctly. But, it does not make your code faster than a
well-written hand-crafted alternative - it makes it about the same. It's
biggest advantages (in my own opinion) is source code reuse and abstracting
ideas in such a way to allow for easy additions/modifications later.
Good Luck.
Shane