By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

how to start with templates ??

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
Thanks Ivan for giving an example... It was really helpful to me for
understanding the template funda...

Jul 23 '05 #9

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Thanks a lot.. I have learned a new thing...

Jul 23 '05 #13

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.