ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It appears the tutorial is very good for a number of reasons:
it is sarcastic ;
it explains well the concepts;
it explains a lot of details;
it explains pitfalls;
it has general tips on C++ safe programming practices;
it explains MSVC++ and g++ quirks (the latter are probably out of date, i.e. g++ appears to be better now).
I am no C++ guru, so my impressions may be wrong. So, constructive comments from C++ gurus are welcome, and even more welcome from them are pointers to good C++ template tutorials.
It seems like a pretty comprehensive intro. It doesn't mention using typedefs with templates, though, which are one of the more useful and widespread uses. For example, iterator_traits and char_traits are used to deduce certain properties of objects passed to template functions by virtue of partial-specialization. There are also some powerful wrapping techniques, like creating a non-template abstract class and deriving a template class from it to encapsulate different types of object while providing a uniform interface. You can also provide default arguments (even depending on other template arguments,) and you can explicitly specify types when calling template functions (much like static_cast, etc.)
I think it also failed to mention using objects as template arguments, e.g. template <int size> class vector;, which is a less-common use. You can do some pretty strange things as a consequence of templates being compile-time constant, especially with static const class members defined inline.
The syntax is fairly simple to learn and a lot of notional examples can be thrown out there, but you can really do a lot with them. I generally only use them to write reusable data structures, but then those structures become the infrastructure of the rest of my code. They're somewhat incompatible with polymorphism because template functions can't be virtual, so you'll often have to make the choice between the two (virtual functions win almost every time, but they can be used in template classes.) It can really bend your mind trying to solve problems that arise from templates and polymorphism.
Kevin Barry
I'm not a C++ expert either :-) but I was wondering about a few slides at the beginning of the presentation. The presentation says that templates are resolved by the preprocessor, but my current understand is/was that templates are pure language constructs handled by the compiler and NOT by the preprocessor. Surely, it's definitely possible to get this done by the preprocessor, but to my opinion, template handling shouldn't be put into the preprocessor. But, I'm willing to get convinced :-)
For "verification" purpose, I checked what GCC/g++ is doing and the g++ preprocessor doesn't resolve the templates.
Yes, templates are not instantiated in the preprocessor. The C++ preprocessor is the C preprocessor, and incapable of knowing whether or not a template needs to be instantiated.
Also, using explicit instantiation to resolve the template definition "problem" is non-idiomatic, inflexible, and introduces a lot of leg-blowing risk, a lot more than is supposedly being avoided.
It's got some stuff right, but the recommendations are bad and most of the "pitfalls" in practice really.. aren't.
The preprocessor doesn't have to be The Preprocessor. Generally templates are parsed in the first pass and the code is actually compiled in the second pass. This allows template resolution and partial-specialization, both of which can be very complicated. The rules for templates make single-pass compilation pretty much impossible.
Kevin Barry
PS This may or may not have been an oral presentation. If it was, remember that slides enhance the presentation, they don't replace it; therefore, some clarifications such as this might have been made in person and not in the slides.
The "preprocessor" thing in that ppt is just wrong. Many more subtle details are also wrong.
The swap example was a nice way to start introducing the idea of templates, but the presentation went bad pretty rapidly after that.
A clear presentation of advanced concepts usually needs to gloss over tricky details to the point that an expert would call it inaccurate. So I don't expect perfect accuracy for a tutorial at this level. But in this case the inaccuracies are just errors, not simplifications. Beyond the first trivial example, the presentation is unclear in addition to being incorrect.
I just browsed for concepts, but could you point out a few of the errors you came across?
When it comes to C++, a tutorial can only get you so far. After that, you really just need to abuse the language to see what you can do and check message boards for the more subtle problems. You could spend days on comp.lang.c++.moderated reading all of the template problems people have, most of which come from not knowing their limitations.
Kevin Barry
The "preprocessor" thing in that ppt is just wrong.
...
The slides do not claim that the preprocessor is the "C" preprocessor. The point is expressed by stating that templates is essentially a sophisticated "find and replace" mechanism, and that parsing of "net" C++ occurs after that "find and replace".
I think the part doing "find and replace" is the preprocessor the author meant. I.e. the templates mechanism is an addition to templateless C++.
Last edited by Sergei Steshenko; 05-25-2010 at 12:06 AM.
I think that's a reasonable way to interpret it, given that different instantiations of the same template are actually different classes or functions. This is a pretty minor point to argue over, though, because there are a lot of rules that determine how templates are resolved, which can change how (more likely, if) your code compiles.
Kevin Barry
PS One point often overlooked is that templates can grossly inflate your code because each instantiation is treated separately, although some compilers are able to merge them.
PS One point often overlooked is that templates can grossly inflate your code because each instantiation is treated separately, although some compilers are able to merge them.
It's the linker, and make that "nearly all". It still has to instantiate it for every translation unit, but if you've got to care, you've got bigger problems.
"Preprocessing" is a completely wrong term for what's going on. The C++ compiler must parse the translation unit first in order to even figure out what instantiations to do. Instantiation is more a part of type-checking than preprocessing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.