how to start with templates ??

J

Jigar Mehta

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
 
U

ulrich

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...
 
A

alexmdac

Jigar said:
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.
 
A

alexmdac

ulrich said:
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!
 
I

Ivan Vecerina

Jigar Mehta said:
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/ThinkingInCPP2e.html


hth,
Ivan
 
J

Jigar Mehta

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...
 
I

Ivan Vecerina

Jigar Mehta said:
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
 
V

Vajira

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.
 
J

Jigar Mehta

Thanks Ivan for giving an example... It was really helpful to me for
understanding the template funda...
 
U

ulrich

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)
 
J

Jigar Mehta

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..
 
U

ulrich

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;
}
 
E

Eric Schmidt

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

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

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

Jigar Mehta

But typeinfo.h is also working.. as per the new ISO C++ standards it
would be <typeinfo>...

Thanks for pointing..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top