By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

Class Templates

P: n/a
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

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Andre wrote in message <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


Well, first of all you can support other types (float, double, or a class
type that can hold more accurate numbers). Second, you can take advantage of
the nature of templates to optimize operations. One simple example is
unrolling loops. I'm sure your matrix multiplications would benefit from
this. I recommend reading C++ Templates: The Complete Guide. It's a newer
book, and it should help a lot.
Jul 19 '05 #2

P: n/a

"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


Jul 19 '05 #3

P: n/a

"Shane Neph" <eu*********@hotmail.com> wrote in message
news:gi*****************@nwrddc03.gnilink.net...

"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


Using Templates may cause code bloat. Actual amounts will depend on the STL
library you use.
As long u minimize use of virtual stuff and inheritance, there wont be much
performance drawbacks.
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.