[Wylug-discuss] C++ Templates and Design Patterns

Roger Leigh roger at whinlatter.uklinux.net
Fri Nov 28 11:22:26 GMT 2003


"Paul Grenyer" <paul at paulgrenyer.co.uk> writes:

>> 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.

Yes.  I've been working on GNU groff (no namespaces, templates etc.),
and it will build several source files per second.  My code builds
about one every 20-30 secs (using the heavily-templated Gtkmm and
libpqxx).

>> When the compiler emits...
>
> I assume you mean Instantiates. :-)

Not in this case--it's the terminology the GCC folks use (?for the
RTL->asm stage?).

> ... 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.

This is with g++ (GCC 3.3.2).  There are options -frepo and
-fno-implicit-templates that might help.  There's also #pragma
interface and #pragma implementation that might be worth
investigating.

>> 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

I had a look at this yesterday, it looks OK, but not quite what I
want.  The main thing it provides (an STL interface to result sets) is
provided by libpqxx.  I'm just adding an object<->row mapping layer on
top, which actually looks very similar to the one it provides as well
(a functor to do the mapping).  The iterator stuff DBView provides
looks interesting--I've currently taken the approach of providing a
separate object per database table (or set of related tables, which
allows you to restrict operations on that table), but this approach
looks less work and more flexible.

>> [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.

There wasn't an instance in the static library above, but it's not in
the final executable either.  I guess it's been inlined, but the other
methods have not been, for some unknown reason.

> 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.
>
> 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.

That's all really helpful, thanks.

--
Roger Leigh

                Printing on GNU/Linux?  http://gimp-print.sourceforge.net/
                GPG Public Key: 0x25BFB848.  Please sign and encrypt your mail.




More information about the Wylug-discuss mailing list