473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Class Templates

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
3 3178

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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
4
by: Gert Van den Eynde | last post by:
Hi all, A beginners question.... I've got a template class template <class T> classA {...} In an other class, I want to pass a pointer to an instance of classA as a function argument....
21
by: Blue Ocean | last post by:
The reason why I ask is because I am unfamiliar with the idea of templates. It seems like it would be easier if all classes that needed something like template<class T> class Stack { ... } ...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
12
by: Ben | last post by:
I'm kind of new to creating templates. I've made some small class and function templates in the past and I have used quite of bit of the STL, but I am having problems tyring to create templates. ...
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.