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

what is template?

P: n/a
what is it and how to use it?

thanks!
Nov 5 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
nick wrote:
what is it and how to use it?


A template is a parameterized type (a class template) or a parameterized
code (a function template). It is rather hard to explain in one newsgroup
posting how you'd use a template -- there are so many uses -- so I strongly
suggest getting a good book on C++ and studying.

Generic programming (implemented in C++ through templates) is a rather
large area. There is a good book out, "C++ Templates" by Vandevoorde and
Josuttis, you might consider leafing through it to get an idea. Also,
peruse through "Generic Programming and the STL" by Austern. In any case
begin by reading some resources on the web, if that's easier for you:

http://en.wikipedia.org/wiki/Generic_programming

http://www.boost.org/more/generic_programming.html

The rest is here:
http://www.google.com/search?hl=en&q...ming+templates

V
Nov 5 '05 #2

P: n/a
Al
you have functions and class templates. You must write on the line
before the declaration:
template<class/*or typename*/ T>
and you use this class for the function or class
ex.

template<class T>
void sort(T& a, T& b)
{
int temp = a;
a = b;
b = temp;
}

for a class, it's different: you can put other parameters as templates

template<class T [, other parameters: ex: int max]>
class X
{
public:
T val1() {retunr val1_}
T val2() {retunr val2_}
private:
T val1, val2;
};

Nov 5 '05 #3

P: n/a
On 2005-11-05 16:57, Al wrote:
you have functions and class templates. You must write on the line
before the declaration:
template<class/*or typename*/ T>
and you use this class for the function or class
ex.

template<class T>
void sort(T& a, T& b)
{
int temp = a;
a = b;
b = temp;
}


Small error here, correct code would be:

template<class T>
void swap(T& a, T& b) /* It's a swap function, not sort :-) */
{
T temp = a; /* Notice T instead of int */
a = b;
b = temp;
}

Basically what it's all about is to write a function that works with any
data type and not just a fixed on such as int or string. To use the
above swap-function you would write something like this:

int a = 5;
int b = 10;
swap<int>(a, b); /* after a=10 and b=5 */

string first = "World";
string second = "Hello";
swap<string>(first, second); /* after first="Hello", second="World" */

What happens is that when compiling the compiler takes the type entered
between the < and > when calling (which we named T) and replaces all
instances of T in the function body with that type. So in the first
example the T in the body is replaced with 'int' and in the second it's
replaced with 'string'.

--
Erik Wikström
Nov 5 '05 #4

P: n/a
"nick" wrote:
what is it and how to use it?


Templates can be used for either functions or classes but the most likely
conversations on the subject are about their use in classes. A template is
a way, in a sense (weasel words), to defer the declaration of a data type
from compile time to link time. The easiest useful sample usage that occurs
to me is a stack. Instead of writing a stack to handle ints, a stack to
handle doubles, .... you write a single templated class.

This link might help but I suspect there are better examples somewhere on
the Web if this one doesn't please you. It popped up on the first page of
hits on google for the search target of <stack c++ templated>

http://www.cprogramming.com/tutorial/templates.html

An error was indicated when I opened the page, but the results were still
useful.
Nov 5 '05 #5

P: n/a
Al
you should look at the FAQ for more information:
http://www.parashift.com/c++-faq-lite/templates.html

Nov 5 '05 #6

P: n/a
In article <3t************@individual.net>,
osmium <r1********@comcast.net> wrote:
"nick" wrote:
what is it and how to use it?
Templates can be used for either functions or classes but the most likely
conversations on the subject are about their use in classes. A template is
a way, in a sense (weasel words), to defer the declaration of a data type
from compile time to link time.


Describing templates is hard to respond to in a NG post, but:

Using template may involve something pertaining to that, but that's
looking at it from a low-level perspective.

Conceptually, a template is a way to independently model something,
classes, algoritithms, etc, and to provide for it a generic description.
They allow an additional relationship mechanism to be provided
across types, and not just up and down them as in a inheritance
hierarchy.
The easiest useful sample usage that occurs
to me is a stack. Instead of writing a stack to handle ints, a stack to
handle doubles, .... you write a single templated class.


A stack is often a good candidate for a template, since it can
be provided across a broad range of types. It can still be
modelled/described without always needing to be concerned with
all details. And then when you want to use it, plug in the
remaining specifics in effect allowing concept to be indepedently
combined.
--
Greg Comeau / Celebrating 20 years of Comeauity!
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?
Nov 5 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.