May 21st, 2002, 8:13 am
Audetto >> So I would like to know what is the good way to use them.Audetto you're right, templates are a very powerful feature but can be confusing; this is partly because the C++ community is still coming to terms with what exactly you can do with them, and even what is a good way to use them?. I give a few links below, really you have to read around and try and see what is possible, then decide where you think they are useful for you. One thing to bear in mind is that with regard to templates the standard is only partially implemented in most compilers, thus many advanced uses of templates will fail to work in many compilers - it's worth checking what your compiler can do before deciding what you want to achieve.Audetto >> I can understand that templates can help you avoiding to rewrite the same code when the only difference is the TYPE of a variable, but now I'm tring to read some come (actually it's QuantLib), where there are a lot (even more) of templates, where it seems that everything is a template.Originally (I think, though Stroustrup often looks prophetic) templates were designed to support generic programming, ie just what you say above. However it was then found that they could be used for more general compile-time computations, and therefore generalized code generation. Because of this they can be used for a variety of purposes, creating very flexible libraries (Loki by Alexandrescu), handy exception safety (Alexandrescu) and making high level optimizations in numerical code and competing with FORTRAN (Veldhuizen). In this way they are basically used as a functinal language at compile time.As I said you have to read around, also it may be best to take it slow if you are just starting with C++; you can write good code without templates. Some pointers:Alexandrescu Many interesting papers, this man is the guru of template meta-programming and definitely on the bleeding edge. His book is also very good, although some things won't work on almost any compilers.Veldhuizen Meta-programming and compile time evaluation.Blitz A numerical library using these techniques.C++ Users Journal always full of goodies, including templates.Herb Sutter I think he has some template articles too.Audetto >> There is another question: what does the compiler do when it finds a template? I think that a template is a sort of "inline definition of a class" and that it cannot compile (I mean: write code) until the template is actually used because it doesn't know what it is made of. Is that right?Certainly these guys cover this and know it better than I. I don't like the word inline here, they supply code to create a class/function based on some other types (and non-types) known at compile time. Actually when finding the template definition the compiler does almost nothing, just a syntax check. Only when finding specific functions used which are known to be templates does the compiler actually expand the template, and then only the functions specifically needed, this is one of the nifty things about templated classes (see Alexandrescu).