Archived

This topic is now archived and is closed to further replies.

billybob

static class template functions and DLLs

Recommended Posts

class Foo
{
   ...
   template < class T >
   static bool Load(T *& Bar, const char * Filename)
   {
      ...
   }
   ...
};
 
this is a function to load classes derived from Foo, but is templated so not all the derived classes have to define their own load function. i tried following this: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/vclrfold-stylenamemanglingforexportedtemplateinstantiations.asp, but it gave a whole new slew of compiler errors, instead of just linker errors outside the project. how do you handle this? i found that adding the header with the function to the project trying to use the dll worked, but this seems kind of hackish, and i''d also like to avoid forcing the dll client to do that.

Share this post


Link to post
Share on other sites
Templates don''t exist in compiled code. They are only a means of generating code at compile time. Template instantiations exist in compiled code, but not the entire mechanism used. You cannot ''place'' a template inside a DLL and expect clients to be able to use it through GetProcAddress or the like.

Share this post


Link to post
Share on other sites
i understand that, and its being linked to a lib. i found another way to get it to work, but this is also pretty hackish. for every class that derives from Foo, i just call the function with it. so, somewhere in the code:


class FromFoo1 : public Foo
{
...
};
class FromFoo2 : public Foo
{
...
};
class FromFoo3 : public Foo
{
...
};

// somewhere else that only gets executed once (or at least, a minimum number of times), in the dll with Foo in it

FromFoo1 * Dummy1;
FromFoo2 * Dummy2;
FromFoo3 * Dummy3;

Foo::Load(Dummy1, "DummyFileItsNotGoingToFind");
Foo::Load(Dummy2, "DummyFileItsNotGoingToFind");
Foo::Load(Dummy3, "DummyFileItsNotGoingToFind");

that gets rid of the linker errors, but this is even worse than adding the header of the dll to the project trying to use the load function.

also, i know i can use casts here, but this is just an example, its really a ref counted pointer template class.

Share this post


Link to post
Share on other sites
it is header only. the pointer class is header only, the class with the Load function is not. i was just saying that i can''t simply cast the argument to load, because that is the pointer class.

Share this post


Link to post
Share on other sites
ok, a better way to put it: how do i force the compiler to specialize the function for the classes that i tell it to, other than simply making a dummy call, like in my other reply?

edit: because that DID work on getting rid of the unresolved externals.

[edited by - billybob on January 17, 2004 10:49:25 PM]

Share this post


Link to post
Share on other sites