Template Patterns, part I

I had this idea to write down some of the coding patterns i use in ==C++==. And, well, i use templates a lot so most of this will be concerned with templated C++ patterns. I don’t assert they are somehow useful or that these things can’t be done in a better way, it’s just i couldn’t think of any :-). Hopefully, it will be useful to someone out there (well, if noone else, to me, since my memory is sometimes doing weird things ;)).

Anyhow, this will be a mini-serie, since i don’t want to bore you with too much stuff at once and i am too lazy to type that much in a go, anyway.

So there goes: base class defined by template parameter.

template <typename Base>
class Something : public Base {
// ...
}; 

(i usually forget the semicolon… look ma, i didn’t this time! :)). Anyway, if you would show me this code few months ago, i would be probably surprized this is possible at all. Another thing, you may wonder what is this good for. Well, it can save you some trouble with multiple inheritance, for one. I have used it few times myself, usually giving a sensible default (like template <typename Base = SomethingBase> ...).

Okey, this is a pretty boring pattern i suppose, since i don’t have that many use-cases. Well, for a real-world example of its use, you can look at entity.h (class template Implementation) and its subclasses Package and StablePackage in package.h (both from libapt-front).

The files above show more template fun, like template parameter telling the base the type of its subclass, but i’ll talk about that next time.

This is probably it for today, i’m somewhat tired (probably because i was ill) and all. And missing elenna. Well, d’oh. She’ll be back in about 2 weeks.

Update: sorry the feeds got mangled by hobix, i had to fix the escaping. Hope it shows up right now…