tags:

views:

128

answers:

4

And which templating library should new beginner user?

Not sure if OS matter,I'm talking about windows if that matters.

+5  A: 

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.

Donotalo
I want to know what happens under the hood for the code `template<type T> T...`
wamp
@wamp: the compiler will generate a function body for all the types you have used with that template.
Daniel
That's a very nice (and clear) explanation, thanks!
Rob
@Danial, the compiler will generate functions only for the types which you have used, not for all possible types.
Kirill V. Lyadvinsky
So that's done by the compiler,then what's the STL for?It sounds like I don't need to include any libraries to make templating work.
wamp
Kirill: I should've clarified on that. :) Will edit for it to make sense.
Daniel
@wamp: The compiler will generate function bodies (or class bodies, as the case may be) **on demand** when you later use the template by writing `T< int >()` or `T(5)`.
Potatoswatter
@wamp: There are two meanings of the word "library" in C++: there are object code libraries as in C, and template libraries which consist of header files. The STL *can* in theory be implemented entirely using headers, but there is typically a binary component as well that needs to be fed into the linker.
Potatoswatter
Kirill V. Lyadvinsky
@wamp: you don't need the STL to make use of templates, the STL however makes heavy use of templates. It is mostly useful for the various containers (like vectors, maps, sets, strings) it implements so you won't have to go reinvent the wheel (like you pretty much have to when you're writing plain C)
Daniel
@Daniel, can you provide an example that STL makes heavy use of templates otherwise will have to go reinvent the wheel?
wamp
@Kirill V. Lyadvinsky, Thanks for the advice. Edited.
Donotalo
Nice clear explanation, though there's no reason to prevent sum(3, 2.2) or sum(my_std_string, "hello") from working... just need template <typename T, typename U> sum(const T }
Tony
@wamp, Things are no more simple to be discussed in comment. One example could be, std::sort(). It works both on plain C array as well as STL containers (vector, string, ...)
Donotalo
A: 

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

Necrolis
+3  A: 

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.

Kirill V. Lyadvinsky
I also heard of ATL, which is better?
wamp
They are different. Start from the Standard Library. It is a part of the C++ Standard.
Kirill V. Lyadvinsky
Can you elaborate a little about the difference?BTW, does the templating library varies on windows and linux?
wamp
They are different means that ATL and STL are not the implementations of the same library. The are different libraries. ATL is a set of template-based C++ classes intended to simplify the programming of COM objects. Standard Library is a collection of classes and functions, which are written in the core language and part of the C++ ISO Standard itself.
Kirill V. Lyadvinsky
Does STL have different versions on windows and linux?
wamp
Generally each compiler has its own implementation of the Standard Library. So yes, STL implementations on Windows and Linux are different in terms of how are the written. But they should have the same behavior as it is described in the C++ Standard. So from the point of library user they are have no difference in Windows and Linux.
Kirill V. Lyadvinsky
Thanks!My final question,someone mentioned that I don't need to use the Standard Library to use `template` in c++,so can you provide an example that shows the benifit of using STL?
wamp
You could write your own sorting templated function or you could use [std::sort](http://www.cplusplus.com/reference/algorithm/sort/) function from the Standard Library.
Kirill V. Lyadvinsky
It very much depends on the person whether it's best to start with a book...not that I'm any model, but I haven't learned a language from a book since TurboC about 18 years ago.
sje397
@wamp: STL has some quite complex templates that would be too difficult for you to write yourself (anytime soon), but you can use them straight away and it will help you solve more complex problems using C++ without having to do all the hard, error-prone work yourself. For example, the STL has a std::map<Key,Value> template that provides an associative mapping in a binary tree. You can just say std::map<int, std::string> num2name; num2name[1] = "one"; num2name[2] = "two"; std::cout << "x is " << num2name(x) << '\n'; and it will lookup which value to use (if x is 1 or 2).
Tony
I can feel the ease of STL now,but for ATL,can you also provide an example on that?
wamp
@wamp: Why don't you get [a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or something and actually learn these things, instead of scrap for information online? You aren't going to learn a damn thing by an example. And until you actually need ATL, pretend it doesn't exist.
GMan
+2  A: 

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.

MSalters
When you copy a raw COM pointer (e.g. `IUnknown*`), you must remember to call `->AddRef()`; when the pointer goes out of scope you must call `->Release()`. The ATL template `CComPtr<>` will do this for you at the right points, e.g. `CComPtr<IUnknown>` will call `IUnknown::AddRef()` and `IUnknown::Release()` etcetera.
MSalters