473,671 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ template tutorial/review : comments welcomed

Hello All:

I notice that there have been frequent questions being asked about template
here so I guess it is okay to post this message here. I just wrote a c++
tempalte tutorial/review, I would like to hear feedbacks from other users. I
do not have much experience with template myself so I am sure there are many
problems or even mistakes in this material. Comments of all types are
welcomed. and I also hope this tutorial/review is helpful for programmers
who want to learn about template.

Here is the link to the html version:

http://www.cs.indiana.edu/~gluan/cpp...e_tutorial.htm

Thanks,
Guotao
Jul 22 '05 #1
11 3202
Guotao Luan wrote:
I notice that there have been frequent questions being asked about template
here so I guess it is okay to post this message here. I just wrote a c++
tempalte tutorial/review, I would like to hear feedbacks from other users. I
do not have much experience with template myself so I am sure there are many
problems or even mistakes in this material. Comments of all types are
welcomed. and I also hope this tutorial/review is helpful for programmers
who want to learn about template.

Here is the link to the html version:

http://www.cs.indiana.edu/~gluan/cpp...e_tutorial.htm


Couple of things from the very first minute of looking it over:

(a) Introduction of bad programming techniques is not a good way to
justify using other language constructs. Nobody in their right
mind would omit inner parentheses in

#define mySquare(x) (x * x)

They would write

#define mySquare(x) ((x) * (x))

and while it doesn't solve some other problems, it would definitely
prevent mySquare(3+4) from evaluating to 22 (and not 19 as you wrote).

(b) Please replace the _tmain nonsense with the standard main and drop
the TCHAR (unless you include the definition of it in every example)
Teaching compiler-specific things in a generic language course is
A BAD IDEA(tm).

I am not going to comment more on your tutorial. These two things have
already left a bad taste in my mouth.

Programmers who want to learn about templates should read books about
templates. There are several. You mention NONE. Why is that? Have
you heard of "The C++ Templates" by Vandevoorde and Josuttis? What
about "Accelerate d C++", just for kicks? And "The C++ Standard Library"?
You don't even mention "The C++ Programming Language" by Stroustrup.
WHY NOT?

No, your tutorial definitely not the place where I'd advise anybody to
go to learn about templates. Not yet, anyway.

Good luck fixing it!

V
Jul 22 '05 #2
Guotao Luan posted:
Hello All:

I notice that there have been frequent questions being asked about
template here so I guess it is okay to post this message here. I just
wrote a c++ tempalte tutorial/review, I would like to hear feedbacks
from other users. I do not have much experience with template myself so
I am sure there are many problems or even mistakes in this material.
Comments of all types are welcomed. and I also hope this
tutorial/review is helpful for programmers who want to learn about
template.

Here is the link to the html version:

http://www.cs.indiana.edu/~gluan/cpp...e_tutorial.htm

Thanks,
Guotao


Does Bjarne ever post here?

-JKop
Jul 22 '05 #3
Guotao Luan wrote:
Hello All:

I notice that there have been frequent questions being asked about
template here so I guess it is okay to post this message here. I just
wrote a c++ tempalte tutorial/review, I would like to hear feedbacks
from other users. I do not have much experience with template myself
so I am sure there are many problems or even mistakes in this
material. Comments of all types are welcomed. and I also hope this
tutorial/review is helpful for programmers who want to learn about
template.

Here is the link to the html version:

http://www.cs.indiana.edu/~gluan/cpp...e_tutorial.htm


I don't have the time to read such an extensive piece, but I glanced over
your first chapter and must disagree with the following: you say languages
such as C# and Java don't deal with templates because they have a unified
object model with object that derive from one base class. This isn't true,
per se. I can't speak for Java (my experience with it isn't big enough), but
in .Net at least there are still several drawbacks to using generalised
collection classes with System.Object instead of templates. The big
disadvantages are:
1. Cumbersome code: Casts to System.Object are implicit, but casts back from
System.Object to the type you desire isn't (at least not in C#, and also not
in VB.NET if Option Strict is turned on). If you're putting objects of
multiple types into a container, you need to keep track of what type they
are, using even more cumbersome code. Simple foreach iterations become
incredibly complex if you're not sure of the type of objects in the
container, due to added code for type checking and casting.
2. Performance: Casting takes a performance hit. In VB.NET, you can minimize
this by using DirectCast, but that doesn't always work. With Value Types
(objects that inherit from System.ValueTyp e such as Int32 (int), Boolean
(bool), DateTime and all structs and enums the hit is even bigger, because
these need to be boxed and unboxed to be put in a generalised container.
3. Type safety: there is no way to ensure that if you want a container of
just a certain type, that it will in fact contain just that type.

For this reason, .Net already offers some ready-made specialised collections
in the System.Collecti ons.Specialized namespace. Not only that, but generics
will be added to codename Whidbey (.Net 2.0), albeit in a wildly different
way from C++ (not so much in syntax as in implementation)

Check out these articles:
..NET: Introducing generics in the CLR
http://msdn.microsoft.com/msdnmag/issues/03/09/NET/
..NET: More on generics in the CLR
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

--
Unforgiven

Jul 22 '05 #4
1. I agree that the statement about 'unified object model' and 'template
support' is controversial.
2. I did mention the proposed 'generic addtion' to .Net somewhere in this
tutorial. it is probablly burried deeper somwhere though.
3. Thank you for the insights of the performance and syntax issues with the
container classes in .Net.
I would really like to know the perfermance of .Net containers class v.s.
that of C++ template class, but I have not come seen any papers about it.

Guotao

"Unforgiven " <ja*******@hotm ail.com> wrote in message
news:2i******** ****@uni-berlin.de...

I don't have the time to read such an extensive piece, but I glanced over
your first chapter and must disagree with the following: you say languages
such as C# and Java don't deal with templates because they have a unified
object model with object that derive from one base class. This isn't true,
per se. I can't speak for Java (my experience with it isn't big enough), but in .Net at least there are still several drawbacks to using generalised
collection classes with System.Object instead of templates. The big
disadvantages are:
1. Cumbersome code: Casts to System.Object are implicit, but casts back from System.Object to the type you desire isn't (at least not in C#, and also not in VB.NET if Option Strict is turned on). If you're putting objects of
multiple types into a container, you need to keep track of what type they
are, using even more cumbersome code. Simple foreach iterations become
incredibly complex if you're not sure of the type of objects in the
container, due to added code for type checking and casting.
2. Performance: Casting takes a performance hit. In VB.NET, you can minimize this by using DirectCast, but that doesn't always work. With Value Types
(objects that inherit from System.ValueTyp e such as Int32 (int), Boolean
(bool), DateTime and all structs and enums the hit is even bigger, because
these need to be boxed and unboxed to be put in a generalised container.
3. Type safety: there is no way to ensure that if you want a container of
just a certain type, that it will in fact contain just that type.

For this reason, .Net already offers some ready-made specialised collections in the System.Collecti ons.Specialized namespace. Not only that, but generics will be added to codename Whidbey (.Net 2.0), albeit in a wildly different
way from C++ (not so much in syntax as in implementation)

Check out these articles:
.NET: Introducing generics in the CLR
http://msdn.microsoft.com/msdnmag/issues/03/09/NET/
.NET: More on generics in the CLR
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

--
Unforgiven

Jul 22 '05 #5
Thank you Victor, these are tough comments but they are well said. I will
defintelly try to make some changes according to your comments.

one of the thing I need to point out though is that this is more of a
'research overview' of C++ template, not a pure 'tutorial'. so I was not
very careful when writting the section section on basic template stuffs.

Guotao


"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:NJ******** *********@dfw-read.news.verio .net...
Guotao Luan wrote:
I notice that there have been frequent questions being asked about template here so I guess it is okay to post this message here. I just wrote a c++
tempalte tutorial/review, I would like to hear feedbacks from other users. I do not have much experience with template myself so I am sure there are many problems or even mistakes in this material. Comments of all types are
welcomed. and I also hope this tutorial/review is helpful for programmers who want to learn about template.

Here is the link to the html version:

http://www.cs.indiana.edu/~gluan/cpp...e_tutorial.htm
Couple of things from the very first minute of looking it over:

(a) Introduction of bad programming techniques is not a good way to
justify using other language constructs. Nobody in their right
mind would omit inner parentheses in

#define mySquare(x) (x * x)

They would write

#define mySquare(x) ((x) * (x))

and while it doesn't solve some other problems, it would definitely
prevent mySquare(3+4) from evaluating to 22 (and not 19 as you

wrote).
(b) Please replace the _tmain nonsense with the standard main and drop
the TCHAR (unless you include the definition of it in every example)
Teaching compiler-specific things in a generic language course is
A BAD IDEA(tm).

I am not going to comment more on your tutorial. These two things have
already left a bad taste in my mouth.

Programmers who want to learn about templates should read books about
templates. There are several. You mention NONE. Why is that? Have
you heard of "The C++ Templates" by Vandevoorde and Josuttis? What
about "Accelerate d C++", just for kicks? And "The C++ Standard Library"?
You don't even mention "The C++ Programming Language" by Stroustrup.
WHY NOT?

No, your tutorial definitely not the place where I'd advise anybody to
go to learn about templates. Not yet, anyway.

Good luck fixing it!

V

Jul 22 '05 #6
Unforgiven wrote:
Guotao Luan wrote:
Hello All:

I notice that there have been frequent questions being asked about
template here so I guess it is okay to post this message here. I just
wrote a c++ tempalte tutorial/review, I would like to hear feedbacks
from other users. I do not have much experience with template myself
so I am sure there are many problems or even mistakes in this
material. Comments of all types are welcomed. and I also hope this
tutorial/review is helpful for programmers who want to learn about
template.

Here is the link to the html version:

http://www.cs.indiana.edu/~gluan/cpp...e_tutorial.htm


I don't have the time to read such an extensive piece, but I glanced over
your first chapter and must disagree with the following: you say languages
such as C# and Java don't deal with templates because they have a unified
object model with object that derive from one base class. This isn't true,
per se. I can't speak for Java (my experience with it isn't big enough),
but in .Net at least there are still several drawbacks to using
generalised collection classes with System.Object instead of templates.
The big disadvantages are:

[smip]
What you wrote pretty much describes Java as well. I believe this
discussion from the link above is misrepresenting the history of OOP:

"Look at how template was introduced into C++, one will naturally wonder
about C++?s object model. In my opinion, the lack of a unified, monolithic
object model, where everything else is derived from object, is perhaps one
of the biggest reasons why template was introduced to write container
classes. It?s worthy to notice that once the top level object was
envisioned, languages, including both Java and C#, no longer bother to deal
with templates."
Please see the documentation for the latest version of Java:
http://java.sun.com/j2se/1.5.0/docs/....html#generics

"But given the fact that object oriented software development as a paradigm
only became dominant with the introduction of JAVA, it?s understandable
that Stroutrup did not envision a unified object model when designing C++."

It's been around since the 1970s. Someone correct me if they know better,
but I believe that has always been the design of the SmallTalk object
model. That puts it around 1972?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #7
Thank you for the Java generics link. I will take a look.

As for the OOP, it is true that OO has been around at least since the 70s,
but please note that I was saying that OO only became 'dominant' in the 90s.

Guotao
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:-P************** ******@speakeas y.net...
"Look at how template was introduced into C++, one will naturally wonder
about C++?s object model. In my opinion, the lack of a unified, monolithic
object model, where everything else is derived from object, is perhaps one
of the biggest reasons why template was introduced to write container
classes. It?s worthy to notice that once the top level object was
envisioned, languages, including both Java and C#, no longer bother to deal with templates."
Please see the documentation for the latest version of Java:
http://java.sun.com/j2se/1.5.0/docs/....html#generics

"But given the fact that object oriented software development as a paradigm only became dominant with the introduction of JAVA, it?s understandable
that Stroutrup did not envision a unified object model when designing C++."
It's been around since the 1970s. Someone correct me if they know better,
but I believe that has always been the design of the SmallTalk object
model. That puts it around 1972?
--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org

Jul 22 '05 #8
Steven T. Hatton wrote:
....

[smip]
What you wrote pretty much describes Java as well. I believe this
discussion from the link above is misrepresenting the history of OOP:

"Look at how template was introduced into C++, one will naturally wonder
about C++?s object model. In my opinion, the lack of a unified, monolithic
object model, where everything else is derived from object, is perhaps one
of the biggest reasons why template was introduced to write container
classes. It?s worthy to notice that once the top level object was
envisioned, languages, including both Java and C#, no longer bother to deal
with templates."
Following your lead Steve ...

Actually, there are several reasons templates were introduced - but I
personally don't believe the monolithic object model is a very important
one.

One of the most important reasons templates came into being were to
enforce type safety. Folks were creating containers of void* and then
dangerously down-casting them as they used them. C++ is all about type
safety.

Alexandrescu does things with templates just short of teaching monkeys
to fly ... really unrelated to issues solved (or introduced) with a
monolithic object model like Java or C#.

And any developer is free to create and adhere to his own monolithic
object model in his own application. Standardizing such an ideology
would have weighted the language done with minimal advantages.
"But given the fact that object oriented software development as a paradigm
only became dominant with the introduction of JAVA, it?s understandable
that Stroutrup did not envision a unified object model when designing C++."


http://www.research.att.com/~bs/bs_faq.html#Java

<quote>
Is Java the language you would have designed if you didn't have to be
compatible with C?
No. Java isn't even close. If people insist on comparing C++ and Java -
as they seem to do - I suggest they read The Design and Evolution of C++
(D&E) to see why C++ is the way it is, and consider both languages in
the light of the design criteria I set for C++. Those criteria will
obviously differ from the criteria of Sun's Java team. Despite the
syntactic similarities, C++ and Java are very different languages. In
many ways, Java seems closer to Smalltalk than to C++.
</quote>

And keep this in mind. It is not an "object oriented paradigm" that begs
for a monolithic object model. Java in no way is more object oriented
then C++. C++ actually has much more modeling versatility in this
regard. Virtual inheritance, multiple inheritance, etc. Java OO
functionality is mostly a subset of C++ in this regard.

No, instead, Java uses a monolithic object model largely due to its
reflection api. Every Java object carries around meta information. Java
casts are always safe casts because an object knows what type it is. You
can't effectively do that unless you have a common base class.

C# has the same *feature*. Both languages compile programs that run in
virtual machines - a common base object helps ensure that information is
present.

Dynamically loading classes and instantiating methods at runtime
requires reflection which requires a common base class. Java and C# are
Virtual Machine languages and the monolithic object model provides a
larger features set for the virtual machines to depend on.

The relation of OO and monolithic object model is an afterthought at best.

It just so happens that as OO came into vogue, so also did Java and now
C# - but to imply that something that happened out of circumstance
(monolithic object model) is actually a core part of a movement (OO) is
a bit overzealous I think ... or just mistaken :)

Ah - back to templates ... now, templates are *hard* to implement in a
language - and not always necessary. That is probably "closer" to the
reason why Java and C# don't have templates _YET_.

-Luther
Jul 22 '05 #9
Guotao Luan wrote:
Thank you for the Java generics link. I will take a look.

As for the OOP, it is true that OO has been around at least since the 70s,
but please note that I was saying that OO only became 'dominant' in the
90s.

Guotao


After I posted I realized I had not really been clear. What I intended is
that the concept of a UBC (universal base class) has existed since the
beginning of SmallTalk. The idea has been considered and rejected by the
C++ designers, and their reasoning seems valid. I know this, because I'm
the person who proposed it.

There is no reason there couldn't be a 'sub-universal' base class for an
entire tool kit. AAMOF, it has been done more than once in some form or
another.

--
STH
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3914
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
6
2027
by: murmur | last post by:
http://www.threetwo.org/sullivan/mockup/ seems to work in mozilla 1.4, netscape 7.1, ie 6. Screenshots from other systems/browsers would be great, particularly mac and aol. Suggestions for better ways to achieve the same results (scrollable textbox and such) would be much appreciated. thank you.
5
2706
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
15
4214
by: binnyva | last post by:
Hello Everyone, I have just compleated a JavaScript tutorial and publishing the draft(or the beta version, as I like to call it) for review. This is not open to public yet. The Tutorial is avaliable at... http://www.geocities.com/binnyva/code/javascript/advanced_tutorial/ If any of you could spare the time, please have a look at my tutorial
53
4571
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document, and at the end there is even pure mindstorming text left in, but already I think it can be very useful. <url: http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01__alpha.doc.pdf>.
156
7563
by: jacob navia | last post by:
There is a C tutorial at http://www.cs.virginia.edu/~lcc-win32 It is written to go with the compiler, available at the same URL. I have added quite a bit of material, and I would be glad if people in this group give it a try and tell me if I am saying nonsense somewhere. Beware that I am not very orthodox, hence my tutorial
11
1996
by: Sathyaish | last post by:
I had recieved an email sometime ago wherein I was asked to write a function that parsed a given string for the longest pallindrome it contained and replaced the pallindrome with another string, given as its second argument. In finding a pallindrome, however, all the characters were to be considered equal. Therefore, a space, a comma, or any punctuation for that matter was to be treated like any other character. In solving that...
11
2925
by: Magnus Lycka | last post by:
While the official Python Tutorial has served its purpose well, keeping it up to date is hardly anyones top priority, and there are others who passionately create really good Python tutorials on the web. I think 'A Byte of Python' by Swaroop C H is a good beginners tutorial, and 'Dive Into Python' by Mark Pilgrim is a good tutorial for more experienced programmers.
4
1763
by: Richard Buckle | last post by:
Hi fellow Pythonistas, I've been using Python in anger for some time, and I must say, as I wrote in <http://macprang.sourceforge.net/>: "It's refreshing beyond words to use a language that so freely combines such a small, clean syntax with such a powerful synthesis of procedural, object-oriented and functional techniques." My (hardcore C++) workplace is now very much converted to Python for
0
8917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8598
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6229
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4407
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.