473,378 Members | 1,478 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,378 software developers and data experts.

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 1451

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********@gatescorp.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********@gatescorp.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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<typename 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<typename T, int Size>
struct my_array // defines an object that contains an array of items
{
T items[Size];
};

Usage example:
my_array<std::string,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********@gatescorp.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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_limits<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 "Collections: 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********@gatescorp.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
Thanks ulrich, but I don't know how to use operator typeid as, I have
never used it.. Can you please do me a favour and tell me how to use
that by giving one line snippet about how to use it ?? Thanks..

Jul 23 '05 #11
On 28 Jan 2005 05:47:00 -0800, Jigar Mehta <ji********@gatescorp.com>
wrote:
Thanks ulrich, but I don't know how to use operator typeid as, I have
never used it.. Can you please do me a favour and tell me how to use
that by giving one line snippet about how to use it ?? Thanks..


here. but usually, you should consult your c++ reference first...

#include <iostream>
#include <typeinfo.h>

class Base {
public:
virtual void vvfunc() {}
};

class Derived : public Base {};

using namespace std;
int main()
{
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid( pb ).name() << endl; //prints "class Base *"
cout << typeid( *pb ).name() << endl; //prints "class Derived"
cout << typeid( pd ).name() << endl; //prints "class Derived *"
cout << typeid( *pd ).name() << endl; //prints "class Derived"
delete pd;
}

--
have a nice day
ulrich
Jul 23 '05 #12
Thanks a lot.. I have learned a new thing...

Jul 23 '05 #13
ulrich wrote:
On 28 Jan 2005 05:47:00 -0800, Jigar Mehta <ji********@gatescorp.com>
wrote:
Thanks ulrich, but I don't know how to use operator typeid as, I have
never used it.. Can you please do me a favour and tell me how to use
that by giving one line snippet about how to use it ?? Thanks..

here. but usually, you should consult your c++ reference first...

#include <iostream>
#include <typeinfo.h>


Er, that should be <typeinfo>, not <typeinfo.h>.

--
Eric Schmidt
Jul 23 '05 #14
But typeinfo.h is also working.. as per the new ISO C++ standards it
would be <typeinfo>...

Thanks for pointing..

Jul 23 '05 #15

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

Similar topics

5
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...
22
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...
16
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
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...
5
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...
1
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...
25
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...
28
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...
104
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.