Skip to main content
GameDev.net gamedev.net
🔒 Locked

Template Resources

Started by Chris Hare Oct 31, 2003 at 6:27 PM 10 replies 1.6k views
Original Post
Chris Hare
Chris Hare
Templates are a C++ programmer''s secret weapon, but because of the nature of templates (complex, relatively "new"), I''ve haven''t really found many good online resources or books on template topics, such as normal and partial specialization, metaprogramming etc. Does anyone know of any? Thanks in advance. Wizza Wuzza?
brassfish89
brassfish89
"Modern C++ Design", by Andrei Alexandrescu, is a very good book in templates in my opinion.
EDIT: fixed typo

[edited by - brassfish89 on October 31, 2003 11:04:53 PM]
dot
dot
David Vandevoorde, Nicolai M. Josuttis: C++ Templates: The Complete Guide, Addison-Wesley Pub Co (2002)
Fruny
Fruny
I wish to second brassfish89 and dot''s suggestions.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Chris Hare
Chris Hare
Thanks all.

Wizza Wuzza?
Chris Hare
Chris Hare
In addittion, how can I restrict expected types, in say a templated function, to numeric types as an example.

e.g. restricting a templated abs() function to numeric arguments.

The only way I can think of is to declare the function prototype and specialise for the desired argument types. But then, how does this approach compare to normal operator overloading?

Also, is this allowed in the C++ standard?



struct SomeStruct
{
static int a = 5 ;
};



Thanks in advance.

It doesn't matter where your from but where your at,
it's hard to have rythm in a body bag.

[edited by - Chris Hare on November 2, 2003 12:28:25 AM]

[edited by - Chris Hare on November 2, 2003 12:29:17 AM]
Chris Hare
Chris Hare
Boof. I mean, bump.

It doesn''t matter where your from but where your at,
it''s hard to have rythm in a body bag.
Qw3r7yU10p!
Qw3r7yU10p!
I'll third the suggested texts and links (google is a very useful one)

Also these online articles are from some of the movers and shakers of C++:
Template Metaprograms: Todd Veldhuizen
Mappings between Types and Values: Andrei Alexandrescu
The Boost C++ Metaprogramming Library has a useful introduction explaining some of the ideas.

quote:
Original post by Chris Hare
Also, is this allowed in the C++ standard?

struct SomeStruct
{
static int a = 5 ;
};



Yes but Microsoft VC++ 6.0 doesn't support it.

quote:

It doesn't matter where your from but where your at,
it's hard to have rythm in a body bag.


If that's a signature you might as well get the spelling correct you're and rhythm

[edited by - petewood on November 3, 2003 1:01:53 AM]
Chris Hare
Chris Hare
Thanks petewood, I never was a good speeler.

It doesn''t matter where you''re from but where your at,
it''s hard to have rhythm in a body bag.
Qw3r7yU10p!
Qw3r7yU10p!
I would just note that the basic ideas of templates themselves aren't really complex. templated code is just like normal code but with types specified with a placeholder name rather than int or double or complex

Code which is likely to be repeated exactly for different types can easily be templated. eg. The following code is just waiting to be templated.

double add(double a, double b) {
return a + b;
}

int add(double a, double b) {
return a + b;
}

string add(string a, string b) {
return a + b;
}

If a few modifications are made for efficiency you get:
template<class Type>
Type add(const Type& a, const Type& b) {
return a + b;
}
//This code will also automatically work for complex numbers or

//any other type which has operator+ defined.



This is actually very similar to what you have in the C++ standard library. std:: plus is a function object whose code (from the msvc implementation) is:

//_Ty is shorthand for the Type

template<class _Ty>
struct plus : binary_function<_Ty, _Ty, _Ty> {
_Ty operator()(const _Ty& _X, const _Ty& _Y) const
{return (_X + _Y); }
};
//You can create an object of type plus<int> and call operator() on it

plus<int> plusInt;
int result = plusInt(4, 6);

This might not look very useful by itself or just a bit obscure. However you are able to use function objects with the algorithms provided by the standard library. Then you start to see the rewards. Reading about how to use the standard library will also help you with understanding the power and use of template programming. Check out sgi's stl documentation.

You've missed a 'your'.

[edited by - petewood on November 3, 2003 4:52:18 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.