473,811 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to start with templates ??

Hye all,
I am a VC++ programmer (have exp. of 2+ years).. But till now, I
have never worked on templates.. I have developed whole products but
never worked on templates or used them in my applications...

But now, I think as a full-fledged programmer, this is the thing I have
not learned.. And want to start with templates, so, can you please
guide me from where to start with ?? What is the general meaning of
template ??

Can anybody present me with any basic example of templates from where I
can start understand template basics...

You please give me small snippet that does something through
templates..
Thanks,

Jigar Mehta
GATES Information Systems Pvt. Ltd.
India

Jul 23 '05 #1
14 1487

Jigar Mehta wrote:
Can anybody present me with any basic example of templates from where I can start understand template basics...

You please give me small snippet that does something through
templates..


There are lots of good books that will help you with this. I've heard
good things about "The C++ Primer" by Lippman and "Thinking in C++" by
Eckel, which is freely available on the web. Of course, you could also
have a look at good old "The C++ Programming Language" by Stroustrup.

Jul 23 '05 #2
On 27 Jan 2005 22:48:33 -0800, Jigar Mehta <ji********@gat escorp.com>
wrote:
But now, I think as a full-fledged programmer, this is the thing I have
not learned.. And want to start with templates, so, can you please
guide me from where to start with ?? What is the general meaning of
template ??
just as variables allow you to parametrize values, templates allow you to
parametrize data types.

take a look at some standard containers in eg. <list>, <vector>, to see
how templates are used.
btw. i cannot believe that you never worked with these classes so far...
Can anybody present me with any basic example of templates from where I
can start understand template basics...

You please give me small snippet that does something through
templates..


std - library is full of "snippets".

for an overview i suggest Stroustrup's "The C++ Programming Language", for
high level stuff i can recommend Alexandrescu's "Modern C++ Design".

be warned: using templates need's twice the care than doing without them,
and (compile time) bugs are much harder to find...

--
have a nice day
ulrich
Jul 23 '05 #3

ulrich wrote:
take a look at some standard containers in eg. <list>, <vector>, to see how templates are used.


If you're not already proficient with the STL, I *highly* recommend you
get a copy of "Effective STL" by Scott Meyers. This book changed my
life!

Jul 23 '05 #4
"Jigar Mehta" <ji********@gat escorp.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Can anybody present me with any basic example of templates from where I
can start understand template basics...
You please give me small snippet that does something through
templates..

Just for the fun of it:

The simplest example of a template function would probably be:

template<typena me T>
T my_abs(T value) // returns the absolute value of its parameter
{
if( value<0 ) return -value;
else return +value;
}

Call it just as a standard function:
int i = my_abs(-5);
float j = my_abs(-5.3f);
As an example of a template class, I would pick:

template<typena me T, int Size>
struct my_array // defines an object that contains an array of items
{
T items[Size];
};

Usage example:
my_array<std::s tring,5> obj;
obj.items[3] = "hello";
Online, I agree that "Thinking in C++" might be a good place to start.
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html
hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 23 '05 #5
Hye,
How can we examine for the type name that has been passed as a
parameter...

Whether the argument has been passed is integer or float or string etc
??

Please help the novice...

Jul 23 '05 #6
"Jigar Mehta" <ji********@gat escorp.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Hye,
How can we examine for the type name that has been passed as a
parameter...

Whether the argument has been passed is integer or float or string etc
??
You could query for some of the type's properties using
std::numeric_li mits<T> (#include <limits>), for example.
You can also specialize a template for a specific
type, IIRC:
template<> my_abs<MyBigInt > { ... special implementation }
Or delegate work to another template that has specializations .
You can also consider using a traits class when needed...
Please help the novice...


This quickly goes beyond 'novice' level.
First learn to use the templates in the standard C++ library.

Read relevant chapters of Thinking in C++.

Inspecting the source code of your standard C++ library
implementation can also be instructive.
A good book to start with could be "C++ Templates, the
complete guide" by Josuttis and Vandevoorde.
For greater prowesses, see Alexandrescu's "Modern C++ Design".

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #7
MFC applications also use STL data types. MFC provides STL container
classes as well, even though it is not much popular. You can find
about STL in MSDN under "Collection s: Template-Based Classes" section.

Jul 23 '05 #8
Thanks Ivan for giving an example... It was really helpful to me for
understanding the template funda...

Jul 23 '05 #9
On 28 Jan 2005 02:42:12 -0800, Jigar Mehta <ji********@gat escorp.com>
wrote:
Hye,
How can we examine for the type name that has been passed as a
parameter...

Whether the argument has been passed is integer or float or string etc
??


this should not be necessary, because there exactly lies one of the powers
of supplying a template data type:
that it is NOT necessary to write specific code for each data type.
you should express your algorithm (within a template function or class) so
that it is independent of the template parameters data type.

however, if you need to get type information: use the c++ operator typeid,
and the returned structure of type type_info.
you may need to switch your compiler to enable RTTI (run time type
information)

--
have a nice day
ulrich
Jul 23 '05 #10

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

Similar topics

5
2538
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element of the currently matched element in the template, so that XPath expressions inside would be relative to that node? Specifically, I want to do this, in order to be able to issue xsl:call-template to apply some templates to some
22
2201
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding solutions which are overly specific),
16
16298
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
1957
by: David | last post by:
SUMMARY: If you don't want to read all of this, what I'm looking for more or less is ideas on how to implement websites using ASP.NET that have a templated look and feel and many pages that make use of a template design. Hi- I'm new to ASP.NET and have been working for about 2 weeks on learning ASP.NET but it all still seems very murky to me. I've programmed sites in PHP and MySQL and also done programming in C++ and Java but I
5
4678
by: DraguVaso | last post by:
Hi, I want my appliation to Run a Shortcut on my Desktop. This should be done regardless the fact if the Shortcut is in the All Users\Desktop or MyProfile\Desktop and regardless the version of Windows (NT, 2000, XP, 2003, ....) (97 also Cor ;-) ). Is there a way to Access the Desktop-object with VB.NET? How should I do this?
1
1016
by: VB Programmer | last post by:
I installed 3 web starter kits for VS2005. All of them reside in this dir: C:\Documents and Settings\Robert\My Documents\Visual Studio 2005\Templates\ProjectTemplates\Visual Web Developer\VisualBasic When I create a new website the only template that appears is: Personal Web Site Starter Kit How come the other 2 templates don't appear under the "installed templates"?
25
3339
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the same subject in clc++m to get the more of the context. Ted
28
2644
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the executable size. This should not cause any performance issue. So, is it safe to say that if I can offered to have bigger image size I can go ahead and use Templates with out worrying about any other issues?
104
4624
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
0
9730
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10392
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
10403
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,...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9208
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3020
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.