|
|
Comments and Discussions
|
|
 |

|
Actually you can avoid this.
There are ways to hide your information and still use templates.
There is a "export" key word also coming up in the next C++ version.
|
|
|
|

|
I see templates as one of means to auto generate code. You write instructions for the compiler how to generate the code, and the compiler obeys when it sees your order to generate an instantiation of the template.
More to it, you may force the compiler to do some calculation at compile time (I mean meta-programming.)
It seems that you definitely need templates to write libraries, but not in plain apps.
Recently, works by Andrei Alexandresku changed (or complemented?)the concept and use of the template programming, so it seems (to me, of course) that pro and contra of using templates might be updated a little bit.
geoyar
|
|
|
|

|
Frankly speaking, it is easier to write code using templates, especially when you don't care about code bloat. You also can delegate a big portion of your responsibility with most of safety related concerns to compiler (isn't that nice?).
I think, everyone has something to add to "pros" or "cons" based on his own experience or opinion.
That is why i even didn't try to make any conclusion. Meaning this is up to you as a programmer to decide what to choose.
|
|
|
|

|
I agree on 'pros' and 'contras', but working with templates is not so easy. It is the reason why templates are in 'advanced' chapters of C++ books.
When you are designing a template, you are in parallel designing the classes to be used with the template too (to be precise, the concept of the classes.) And this asks for a lot more of experience and deep thinking. Add to it template template parameters, non-types, default parameters, typedefs - a lot of things. Definitely, it is not for a beginner.
Anyway, thanks for reply.
geoyar
|
|
|
|

|
There is no "easy" part in C++. But we can apply a "simple" analogy to a programming process.
This is like designing and building a house - there are even supposed to be windows.
And coders are like masons, bricklayers. So, this is a simple part or beginner level in your terminology. To design a project of a house is a job of an architect - class designer with code safety in mind, advanced level in programming terminology. This job always was a difficult one. Now, you can build the house from different materials depending on your wallet, talent, time available, etc. That is where templates come handy. They are like panels used to construct a buildings from small to skyscrapers. And of course, there is nothing easy in process of building a skyscraper.
|
|
|
|

|
Unpleasant compilation times, apart from being justified by pushing computation from run time to compilation time (and consequently optimizing much better) can usually be disposed of through code organization: confinement of heavyweight templates into separate source files that are not recompiled without need can be achieved through simple idioms like PIMPL.
|
|
|
|

|
Templates are irreplaceable in computation rich applications and scientific libraries (that was proved by STL and Blitz++). The time C++ committee spent on Concepts for C++11 (and finally removed them) tells that templates are considered a very important part of the language. Now, when processors got to their current limit of power, the parallel computing on multicore platforms and metaprogramming with templates are the techniques experts mostly talk about. But i am afraid that these techniques are so complex that most of us are not ready yet to accept (or understand) them properly. This was one of the reasons i have decided to collect at least some of the commonly known and accepted things about templates in this article. For myself - to organize it somehow, and for the others - if they will need it.
modified 3 Nov '11 - 4:55.
|
|
|
|

|
It's useful to think of templates and classes as being orthogonal rather than competing technologies.
Code bloat is not necessarily a consequence of template explansion. Templates can also be used to generate extremely terse and efficient code (eg uBLAS and Blitz++). Dimensional analysis and other iterative aspects of algorithms can occur at compile time (hence the loooooong builds).
Type safety is a key advantage of generic code, but also a source of many incomprehensible compiler messages. Liberal use of const (a good thing) is often abandoned (a bad thing) in the face of hard to compile generic code.
Poor compiler support for templates (eg, incomprehensible messages) is mainly due to the relative immaturity of the technology..
You say "Templates are sometimes a better solution than C macros and void pointers".
Assuming the language is C++ and not C, I'd say macros and void pointers are not solutions, they are errors!
|
|
|
|

|
Thanks for your comment. You are right in almost every word.
But this small collection of pros and cons is not intended for an expert or
a programmer experienced in templates. Rather this can help to the ones who
have to choose a way but unwilling to decide not knowing what to expect.
I know that no matter of what you advise them, professionals tend to do what they know or do well.
And rarely prefer solutions new or less known to them.
Anyway, it is always the best to know what you can expect in advance.
I have to disagree with immaturity of technology, because the story about poor compiler support for templates is more than 10 years old. And everybody got used to those long error messages somehow.
Concerning quote about C macros and void pointers, this is a classical example given to every C programmer to illustrate how templates are better than macros. It was a part of MSDN text on templates.
Update:To whom it may concern - I forgot to include in References a very good article Techniques for Scientic C++ by Todd Veldhuizen (the author of Blitz++ math library). Though written in 2000, it has a good description of templates related advantages and drawbacks (and not only this). Recommended.
modified 30 Oct '11 - 9:14.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Advantages and drawbacks of using templates in C++ projects
| Type | Article |
| Licence | CPOL |
| First Posted | 29 Oct 2011 |
| Views | 13,955 |
| Bookmarked | 23 times |
|
|