Topic: Templates in DLLs
Author: ssimpson@world.std.com (Steve Simpson)
Date: Mon, 13 Jun 1994 21:57:07 GMT Raw View
I use the following technique to instantiate templates for classes that I
know are going to be used. Once in a DLL anyone can share them. I add to
the list as needed. When a class template is specifically for a particular
exe or dll then I instantiate it there and not in the common DLL.
For example:
In misc.dll:
static void thisFunctionIsNeverCalled()
{
List<char> temp1;
List<int> temp2;
List<long> temp3;
List<String> temp4;
}
This causes the code for particular template classes to be instantiated and
linked into the DLL.
Steve Simpson | Objects in mirror are closer than they
ssimpson@world.std.com | appear.
Software Engineering Consultant | Jurassic Park
Author: mikey@mcs.com (Mike Young)
Date: Wed, 8 Jun 1994 21:16:12 Raw View
In article <5QUwmsA5vMB@liva.in-berlin.de> benno@liva.in-berlin.de (Benjamin Stein) writes:
>I have a problem using class templates from within my class library.
>For ex. I've written classes for linked, double linked and sorted "lists
>of anything". The template classes work fine. However I have to include
>the implementations for this classes in every program using these lists.
>It's very annoying: longer compile times etc.
>If I don't inlcude the implementations the linker cannot resolve the
>template member functions.
>For a special app I build a class SpecialSomewhat. Then - for all "lists
>of anything" used inside the library the template member functions can be
>resolved. But I get the message:
>"list<SpecialSomewhat>::list<SpecialSomewhat>() unresolved external"
>"list<SpecialSomewhat>::addLast(SpecialSomewhat *elem) unresolved ext."
>etc. pp.
>No matter if I'm using my class lib static linked or as a runtime dll.
>Any hints, what could be wrong?
-----------------------
That just about says it all for templates in DLL's. The DLL can't export more
than you have defined, and templates were intended to generate only the code
referenced in your program. The two concepts don't mesh very well.
Essentially, you can't generate code for "lists of anything" if you don't
know what they are yet.
Here are two work-arounds. They still restrict you to the number of variants
that are known to you when you build your DLL. If you truly need to work
with as yet undefined types, skip the DLL part and simply accept that's how
templates were intended to be used.
1) Wrap each relevant template variant in a class. Export these wrappers but
not the templates. This is reasonable, as the templates are a detail of
implementation that nobody else should know or care about.
2) Declare the exported methods virtual. This causes them to be placed in the
virtual table, thus referencing them and causing code to be generated. Define
a pointer to each variant that you wish to export. This will cause the
compiler to generate code for each of the referenced variants.
Of the two, only (1) seems reasonable. (2) is less labor intensive, but I
think it's an ugly hack. Again, you can only generate code for types that are
known at the time of compile. It doesn't make much sense to export templates.
Mike.
Author: benno@liva.in-berlin.de (Benjamin Stein)
Date: 08 Jun 1994 01:00:00 +0100 Raw View
Hi,
I have a problem using class templates from within my class library.
For ex. I've written classes for linked, double linked and sorted "lists
of anything". The template classes work fine. However I have to include
the implementations for this classes in every program using these lists.
It's very annoying: longer compile times etc.
If I don't inlcude the implementations the linker cannot resolve the
template member functions.
For a special app I build a class SpecialSomewhat. Then - for all "lists
of anything" used inside the library the template member functions can be
resolved. But I get the message:
"list<SpecialSomewhat>::list<SpecialSomewhat>() unresolved external"
"list<SpecialSomewhat>::addLast(SpecialSomewhat *elem) unresolved ext."
etc. pp.
No matter if I'm using my class lib static linked or as a runtime dll.
Any hints, what could be wrong?
Here is the declaration for my list as a sample. All the implementations
are now in list.inl:
***************** snipp ******************
// list.hpp
template <class T>
class listItem
{
public:
listItem<T> *next;
listItem<T> *prev;
T *item;
listItem();
~listItem();
};
template <class T>
class list
{
public:
ULONG items;
listItem<T> *actual;
listItem<T> *first;
listItem<T> *last;
list();
~list();
BOOL isEmpty();
void rewind();
void init();
void reset();
void addFirst(T *elem);
void addLast(T *elem);
void add(T *elem) { addLast(elem); }
T* getFirst();
T* getLast();
T* getNext();
T* getPrev();
T* get() { return(getNext()); }
T* getItem(const ULONG itemNum);
void removeFirst();
void removeLast();
void remove(T *elem);
void remove(const ULONG itemNum);
void delFirst();
void delLast();
void del(T *elem);
void del(const ULONG itemNum);
virtual void output(const T *elem) {}
void print();
};
// end of source
***************** snipp ******************
Thanx in advance.
MfG B.
__________________________________________________________________________
internet: benno@liva.in-berlin.de
Z-Netz: b.stein@tbx.berlinet.in-berlin.de
fido: 2:2410/203.9