And which templating library should new beginner user?
Not sure if OS matter,I'm talking about windows if that matters.
And which templating library should new beginner user?
Not sure if OS matter,I'm talking about windows if that matters.
Templates are all about generic programming. The concept is like: you define a function body/class that will work with any kind of data (having some properties, like a specific operator defined). Consider you are writing a function that will return summation of its given parameters:
int sum(int a, int b)
{
return a + b;
}
Now you want that the function should work for strings as well. But you can't do the following:
std::string s1 = "abc", s2 = "def";
std::string s = sum(s1, s2);
For this sum()
calling you need to define another version of sum()
. Templates will save your work. Just write the definition of sum()
in following way:
template<typename T>
T sum(const T& a, const T& b)
{
return a + b;
}
Now the function sum()
will work for any data type for which operator+
is defined.
EDIT
You need to learn STL (Standard Template Library) first, if you want to be a C++ programmer.
principle? I'd have to say it boils down mostly to not 're-inventing the wheel' and rapid application development(RAD), by creating generic code that can be reused in differect situations just by chnaging the template parameters. a good example of this is std::list
or std::vector
As for what to use, well that depends on your goals(ie: what sort or pragram are you making, what will it need to do?), generally though you can use either boost and/or the STL libraries that ship with most compilers these days
For beginner it is better to start from a good book. And the Standard Library (often also called STL) is the template library you should start from.
Templates are a feature of the core C++ language, and implemented by all C++ compilers. You can write a template without using any library. That just means that you have to provide all of the template code; the compiler will turn that code into the appropriate assembly as needed.
The core idea behing templates is the notion of generic code. The code you need to for a linked list of integers looks almsot the same as the code for a linked list of strings; for an array of integers almost the same as an array of strings. Templates allow you to write a linked list of T
objects, wihout specifying T
up front. Then, when you need a linked list of integers, you just tell the compiler to instantiate that linked list with T==int
.
Now, linked lists are quite common. You therefore don't have to write the linked list template; the Standard Library (included with every compiler) contains the std::list<T>
template. To use it, just tell the compiler what kind of list you need. std::list<float>
, std::list<std::string>
, etcetera. In addition to such container classes, there are also algorithms. Those too are templates - std::sort< >
can sort many different containers. Unlike qsort
from C, the C++ compiler knows what it is sorting, and that makes std::sort< >
faster.
The ATL is a microsoft library. It uses templates for the same reason as the Standard Library - not as a goal in itself, but because it allowed Microsoft to write code that you can then tailor for your particular need. For instance, there are thousands of COM interfaces. The ATL doesn't need to provide different code for each and every COM interface; instead it provides a few templates that are instantaited for every COM interafce you want to use.