[Wylug-discuss] C++ Templates and Design Patterns (was: Re:Speakers for meetings in 2004! )

Paul Grenyer paul at paulgrenyer.co.uk
Fri Nov 28 08:49:57 GMT 2003


Hi

> I've started to use templates, though only in strict moderation so
> far!  Perhaps you could answer a (probably stupid!) question about
> them:

Lol - I'll try. It's good to use templates in moderation even whne you
are more familiar with them as they can increase compile times
significantly.

> When the compiler emits...

I assume you mean Instantiates. :-)

... a template instance for a translation unit,
> AFAICK it /should/ have vague linkage, but I'm only getting weak
> linkage (except for typeinfo and vtables), bloating the code
> considerably because the linker won't remove duplicate weak symbols.
> Do I need to forward delare them, use special compiler options or
> anything?  What is the best way to use them (currently I'm just
> putting them in headers, with inline methods)?

This is a problem I haven't come across as developing on Windows I
don't tend worry too much about code bloat unless it get's completely
out of hand. WHat compiler are you using.

If you post this question to accu-general at Accu.org (after first joining
from www.accu.org), someone there will know the answer.

> Currently, I'm using them like this (this is one layer of my
> database<->object serialisation thingy:
>
> table.h:
>   template<typename _Row, typename _Convert>
>   class Table : public Transaction
>   {
>   };
>
> stock.h:
>   #include "table.h"
>   class StockTable : public Table<StockItem, StockItemConvert>
>   {
>   };
>
> Here I'm inheriting from a template instance (StockItem is a class
> representing a row in a database table, and StockItemConvert is a
> function object containing methods for (de)serialisation).

I see. You may want to take a look at:
http://dtemplatelib.sourceforge.net/index.htm

>
> [nm -C]
> 00000000 V typeinfo for Table<StockItem, StockItemConvert>
> 00000000 V vtable for Table<StaffMember, StaffMemberConvert>
> 00000000 W Table<StockItem, StockItemConvert>::~Table()
> 00000000 W Table<StockItem, StockItemConvert>::insert(StockItem&)
>
> Oddly, I can't see a constructor for Table<StockItem,
> StockItemConver> in the symbol table, but there is one for StockTable,
> StockItemConver> which derives it.

Are you actually creating an instance of Table<StockItem,
StockItemConver>? This compiler won't give you things like constructors
unless you actually create objects.

A book worth getting is another of Nico's, "C++ Templates: The Complete
Guide". Just like The C++ Standard Library, it's worth every penny.

> WRT Design Patterns, I've been recommended to read the "Gang of Four"
> book.  Is this a good book?  Are there any books I should buy in
> preference (or addition) to it?  I only recently learned about the
> existence of DPs, but from the little I've read so far, they seem to
> be fairly straightforward "common sense" ideas, some of which I've
> been using already, although I didn't know them by their formal names.

Gang of Four was the first book of its kind and very out-of-date. In
some places it's misleading, such as describing Singleton as a pattern
rather than an anti-pattern. However, there isn't a good alternitive
and GoF is a good introduction to patterns. My advice would be read it
anyway, but be wary.

Another book you might like to try is Design Patterns Explained (ISBN 7-
5083-1505-7). It covers a lot of the GoF patterns, and is a much easier
read.

I've not really been much, but I hope I've given you something to get
started with.

Regards
Paul

Please note the change of email address!

Paul Grenyer
Email: paul at paulgrenyer.co.uk
Web: www.paulgrenyer.co.uk






More information about the Wylug-discuss mailing list