Serving the Quantitative Finance Community

 
User avatar
audetto
Topic Author
Posts: 0
Joined: March 12th, 2002, 4:08 pm

Templates, classes and inline

May 21st, 2002, 7:32 am

Hi,I'm new to C++ programming. I come from Delphi and Java and I found that in C++ there exist templates and inline definition.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.In Delphi and Java I tried to organize everything in terms of abstract classes, virtual methods and subclasses and I never felt the luck of templates.So I would like to know what is the good way to use them.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?Now, about inline definition. Is it true that a function should be made inline only when the time of calling it is big respect to the time of execution of the function body? So that only very little functions should be inline?thanks a lotandrea
 
User avatar
Onuk

Templates, classes and inline

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).
 
User avatar
Onuk

Templates, classes and inline

May 21st, 2002, 8:43 am

Audetto >> Now, about inline definition. Is it true that a function should be made inline only when the time of calling it is big respect to the time of execution of the function body? So that only very little functions should be inline?See here for good explanation and advice from Herb Sutter.Personally I love the good old adage:Optimization:1. Don't do it.2. Don't do it soon.
 
User avatar
audetto
Topic Author
Posts: 0
Joined: March 12th, 2002, 4:08 pm

Templates, classes and inline

May 21st, 2002, 8:53 am

I agrre with you, especially in the early stage of development where the most important things are design of the strucure and readability of the code.I've even read about a C++ Compiler from Intel which is optimized and I would like to know is someone has ever used it?byeandrea
 
User avatar
audetto
Topic Author
Posts: 0
Joined: March 12th, 2002, 4:08 pm

Templates, classes and inline

May 21st, 2002, 8:55 am

Thanks for your answer. I'll try to read some article about good c++ programming.I always thought that good programming in languages like Java, Delphi and C++ are almost the same, but maybe I am wrong...byeandrea
 
User avatar
softduck
Posts: 0
Joined: May 21st, 2002, 2:28 am

Templates, classes and inline

May 22nd, 2002, 3:58 am

Typically, templates are used for containers and generic classes/algorithms that can accept different types...thus preventing you from having to "block-copy" code to support multiple types. So you're pretty much on the money there...Generally, when compilers come across C++ templates, they create "instantiations" of them into seperate .o repositories. Therefore, and I may be wrong here, if you have some template_class<int> and another template_class<char>, the compiler will create two instantiations of template_class...generally in seperate .o's (or .obj's...etc).As far as inlining goes, I think the others have answered that pretty nicely so far.Sean.
 
User avatar
RedeR

Templates, classes and inline

May 24th, 2002, 4:02 pm

I've even read about a C++ Compiler from Intel which is optimized and I would like to know is someone has ever used it? >>Intel C++ optimizer is pretty good stuff:- improved compliance with standards (especially regarding templates)- automatic optimization for ranges of (Intel) CPUs (P3,P4)- support for mpi- win32 and linux supportedThe speed (with all optimizations on) is quite good: can be twice that of gcc with -O6 -march=i686 and so on. Usually about 30%.And optimizations always work (unlike vc 6.0 which breaks as soon as -O2 is on).Definitively worth the $ (if speed is a major concern).
 
User avatar
Etuka
Posts: 1
Joined: January 1st, 2002, 7:40 pm

Templates, classes and inline

May 30th, 2002, 9:54 pm

Generally, when compilers come across C++ templates, they create "instantiations" of them into seperate .o repositories. Therefore, and I may be wrong here, if you have some template_class<int> and another template_class<char>, the compiler will create two instantiations of template_class...generally in seperate .o's (or .obj's...etc). >>This is true, Softduck, and is a major source of template bloat. Template frameworks need careful design. A simple example of a template container class that avoids this kind of bloat can be found in Scott Meyers' Effective C++.
 
User avatar
jamesbattle
Posts: 0
Joined: May 12th, 2002, 8:28 pm

Templates, classes and inline

May 30th, 2002, 10:40 pm

Too much of anything will send you bald and the same applies to templates.They're too powerful and can result in hugely complex code for both people and compilers. I used to love them back in 1995 when I used them like amaniac, continually thinking the compiler support would be "next release".I'd be surprised if even now, there's a single compiler that supports everythingpossible with templates.It's not an accident that most new languages avoid them (C#, Java, VB.net). Thebeauty of the STL is not in templates, but in offering a rich set of collections - Libraries with incredibly complex templates usually result in bugs or unmaintainablecode or both.Try compiling Boost on a few platforms and you'll find out ... Try using the SGI/STLon a few platforms and you soon discover that there's a mass of putty and gluein the shape of pre-processor macros holding everything together. And then there are the useful error messages that come from using a bad parameterin a deeply nested template .... Do you really blame MSVC for giving up because thetype name has gone over 512 characters!!
 
User avatar
Onuk

Templates, classes and inline

May 31st, 2002, 6:18 am

JamesBattle >> And then there are the useful error messages that come from using a bad parameter in a deeply nested template .... Do you really blame MSVC for giving up because the type name has gone over 512 characters!!I agree to some extent. I'm afraid templates have taken C++ to the edge of what it can cleanly accomplish. In the same way that you can do OO in C with managing your own vtables etc, you can create cool stuff with templates but it breaks too easily and is not fully integrated with the language. I still think templates and metaprogramming could be very powerful, but it needs a more integrated approach. If C++ had a real compile-time language evaluating to 'standard' C++ compilable code then problems like the super long names would be avoided.