LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2010, 12:57 AM   #1
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
"Templates WTF?"


I was looking for C++ templates tutorials and while in the process read a number of them.

On the second page of results from Google I've noticed the one from which I stole the thread name:

http://flatline.cs.washington.edu/or...lates_su01.ppt
.

It appears the tutorial is very good for a number of reasons:
  1. it is sarcastic ;
  2. it explains well the concepts;
  3. it explains a lot of details;
  4. it explains pitfalls;
  5. it has general tips on C++ safe programming practices;
  6. 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.
 
Old 05-23-2010, 08:48 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 05-23-2010 at 08:51 PM.
 
Old 05-24-2010, 01:11 PM   #3
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
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.
 
Old 05-24-2010, 01:31 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.
 
Old 05-24-2010, 04:03 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.

Last edited by ta0kira; 05-24-2010 at 04:05 PM.
 
Old 05-24-2010, 04:27 PM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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.
 
Old 05-24-2010, 09:34 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 05-24-2010, 09:47 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Original Poster
Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
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.
 
Old 05-24-2010, 11:45 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.

Last edited by ta0kira; 05-24-2010 at 11:47 PM.
 
Old 05-25-2010, 12:17 AM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
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.
 
  


Reply

Tags
c++, template



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
Why does "su" reject my password if I can log in as root? wtf? neeyo Linux - General 10 12-21-2006 07:46 PM
"Linux Image" missing in Fedora Core 5 - WTF? Paltrude Fedora - Installation 8 05-30-2006 06:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration