473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Help understand Templates

Can someone explain or give me a link on how to create templates?
specially on how to use the template< thingy. For example I have
seen this:

template <class T>
template<typename T>
template <>

each on almost identical situations... so whats the difference?

for example:

#include <iostream>

template <class T>
class Test
{
public:
T i;
};

template <typename T>
class Test2
{
public:
T i;
};

using std::cout;

int
main(void)
{
Test<inti;
Test2<intj;

i.i=1;
j.i=1;

cout << i.i << '\t' << j.i << '\n';
return 0;
}

whats the difference between Test and Test2 templates??

Aug 18 '06 #1
5 1321
RoLo wrote:
Can someone explain or give me a link on how to create templates?
http://www.vandevoorde.com/C++Templates/

Get the book. Well worth it.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 18 '06 #2
RoLo wrote:
Can someone explain or give me a link on how to create templates?
specially on how to use the template< thingy. For example I have
seen this:

template <class T>
template<typename T>
template <>
[redacted]
The first two are semantically identical. The third is the way to
introduce a specialization.

When I write a template, I use the typename/class equivalence to
document the code. If a template is intended to be used with any type
as a template parameter, then I use "typename". If I intend it to be
used with a class parameter, then I use "class". Note that the language
doesn't enforce such restrictions, it's just a convention to document
the intended usage.
Aug 18 '06 #3
The first two are semantically identical. The third is the way to
introduce a specialization.

When I write a template, I use the typename/class equivalence to
document the code. If a template is intended to be used with any type
as a template parameter, then I use "typename". If I intend it to be
used with a class parameter, then I use "class". Note that the language
doesn't enforce such restrictions, it's just a convention to document
the intended usage.
Thanks for your help :-D

Aug 18 '06 #4
In article <11*********************@m73g2000cwd.googlegroups. com>,
RoLo <ro********@gmail.comwrote:
>Can someone explain or give me a link on how to create templates?
specially on how to use the template< thingy. For example I have
seen this:

template <class T>
template<typename T>
See http://www.comeaucomputing.com/techt...late/#typename
>template <>
The <allows us to establish a specialization. This is used
when we know something about a special instance that can
be handled better than the generalized description.

But we really can't/shouldn't do this justice on a NG, so I would
suggest getting a good C++ text (see http://wwww.comeaucomputing.com/booklist
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #5
wow, awesome reply Greg :-D
Thanks!

You have cleared my doubts. I will surely buy a C++ Templates book
anyway, I really need one as reference.

Tanks again everyone for your replies.

Aug 19 '06 #6

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

Similar topics

4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
10
by: E.T. Grey | last post by:
Hi, I have a C++ DLL that I want to use from a C# project. I am actually usng a lot of advanced C++ features like templates, partial/specialized templates, functors and callbacks. I am also...
8
by: Larry Coon | last post by:
I'm trying to use xsl for the first time, and as with any first-time attempt, I'm running into problems. I'm trying to transform an XML document into another XML document with a slightly...
1
by: oldgent | last post by:
I am having a problem installing the starter kits. I have reinstalled VS 2005, think that might be the problem. I then installed both 'Personal Website" and the "Club Website" starter kits. I...
3
by: Simon Brooke | last post by:
As various people will have noticed, I've been having a lot of trouble with XSL lately. Brief history: I wrote myself an XML toolkit back in 2000, and it worked well enough for me, so it's been...
4
by: Debbiedo | last post by:
My software program outputs an XML Driving Directions file that I need to input into an Access table (although if need be I can import a dbf or xls) so that I can relate one of the fields...
6
Gaiason
by: Gaiason | last post by:
Hi XML/XSLT masters and gurus, I am a newbie in XML/XSLT and have been reading up XML/XSLT in order to convert a XML generated from a OCR engine to another format of XML. I am truly stuck at this...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.