3

And which templating library should new beginner user?

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

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
wamp
  • 5,789
  • 17
  • 52
  • 82
  • 1
    Removed "C/" from title - there's no C/C++ language, and the C language doesn't have templates. – MSalters Sep 07 '10 at 07:31
  • So template can only be implemented in OOP languages,is that the case? – wamp Sep 07 '10 at 07:34
  • I didn't like 'c++'. Changed it to 'C++' – Chubsdad Sep 07 '10 at 07:37
  • 3
    no templates have nothing to do with OOP they are to do with generic programming. its just that C++ has them and C doesn't, the nearest C equivalent would be macros – jk. Sep 07 '10 at 07:47
  • 1
    no, generic programming and object-oriented programming are two different paradigms of programming, and as such independent from one another. Object-Oriented programming is about tying the methods to the data they operate on while Generic programming is about not repeating yourself. Haskell supports Generic programming even though it's not considered an OO language (it's a functional language). Go supports Generic programming even though it's Object model may appear kind of strange to a new comer. – Matthieu M. Sep 07 '10 at 07:47

4 Answers4

5

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.

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
Donotalo
  • 12,748
  • 25
  • 83
  • 121
  • I want to know what happens under the hood for the code `template T...` – wamp Sep 07 '10 at 07:02
  • 2
    @wamp: the compiler will generate a function body for all the types you have used with that template. – Daniel Sloof Sep 07 '10 at 07:06
  • @Danial, the compiler will generate functions only for the types which you have used, not for all possible types. – Kirill V. Lyadvinsky Sep 07 '10 at 07:09
  • 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 Sep 07 '10 at 07:10
  • Kirill: I should've clarified on that. :) Will edit for it to make sense. – Daniel Sloof Sep 07 '10 at 07:10
  • @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 Sep 07 '10 at 07:10
  • @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 Sep 07 '10 at 07:11
  • @Donotalo, probably you want to change template to `T sum(const T& a, const T& b)` since it could be used not only for simple types. – Kirill V. Lyadvinsky Sep 07 '10 at 07:12
  • 1
    @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 Sloof Sep 07 '10 at 07:13
  • @Daniel, can you provide an example that STL makes heavy use of templates otherwise will have to go reinvent the wheel? – wamp Sep 07 '10 at 07:15
  • @Kirill V. Lyadvinsky, Thanks for the advice. Edited. – Donotalo Sep 07 '10 at 07:28
  • 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 sum(const T& t, const U& u) { return t + u; } – Tony Delroy Sep 07 '10 at 07:30
  • @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 Sep 07 '10 at 07:30
3

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.

Community
  • 1
  • 1
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • I also heard of ATL, which is better? – wamp Sep 07 '10 at 07:05
  • 1
    They are different. Start from the Standard Library. It is a part of the C++ Standard. – Kirill V. Lyadvinsky Sep 07 '10 at 07:07
  • Can you elaborate a little about the difference?BTW, does the templating library varies on windows and linux? – wamp Sep 07 '10 at 07:10
  • 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 Sep 07 '10 at 07:17
  • Does STL have different versions on windows and linux? – wamp Sep 07 '10 at 07:19
  • 1
    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 Sep 07 '10 at 07:25
  • 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 Sep 07 '10 at 07:28
  • 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 Sep 07 '10 at 07:31
  • 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 Sep 07 '10 at 07:34
  • @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 template that provides an associative mapping in a binary tree. You can just say std::map 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 Delroy Sep 07 '10 at 07:36
  • I can feel the ease of STL now,but for ATL,can you also provide an example on that? – wamp Sep 07 '10 at 07:41
  • @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. – GManNickG Sep 07 '10 at 13:15
2

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
  • 173,980
  • 10
  • 155
  • 350
  • 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` will call `IUnknown::AddRef()` and `IUnknown::Release()` etcetera. – MSalters Sep 07 '10 at 08:23
0

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
  • 25,836
  • 3
  • 63
  • 101