[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Template member functions of template classes in DLL's

Posted on 1998-02-13
4
Medium Priority
?
476 Views
Last Modified: 2013-12-03
I've learned how to export specific instances of a template class from a DLL, but I now have a problem involving a template class that has a template member function. Here's a simple example: suppose I have a template class Foo, as follows:


// Foo.h
#ifdef BUILDING_FOO
#define EXPORT_TEMPL
#define TEMPL_DECL __declspec(dllexport)
#else
#define EXPORT_TEMPL extern
#define TEMPL_DECL __declspec(dllimport)
#endif

template <class T>
class Foo {
public:
    Foo() { }
    virtual ~Foo() { }
    template <class A>
    static void DoIt( A* pA, T t ) {
        m_datum = t;
        pA->doit(t);
    }
    static T m_datum;
};

EXPORT_TEMPL template class TEMPL_DECL Foo<int>;

// Foo.cpp
int __declspec(dllexport) Foo<int>::m_datum = 123;


Foo has a static data member and a static (template) member function. I export Foo<int> from the DLL, and I have no problem getting clients of the DLL to resolve Foo<int> during linking. I do have a problem accessing Foo<int>::DoIt() however:


// main.cpp
class Bar {
public:
    void doit( int i ) {
        cout << "Bar::doit: i = " << i << endl;
    }
};
void main()
{
    Bar bar;
    Foo<int> foo;
    cout << "foo.m_datum = " << foo.m_datum << endl; // OK
    foo.DoIt( &bar, 456 ); // link err: can't find in DLL
}


The problem is, the linker can't find Foo<int>::DoIt<Bar>() in the DLL. Obviously, this can't be exported from the DLL since Bar is defined by the client and is unknown to the DLL. Is there any way of exporting Foo<int> without exporting the DoIt() template member function so that client code will just instantiate DoIt() during its compilation?
What are my alternatives? (I don't want to turn DoIt() into a non-template member function).
I'm using Visual C++ 5.0 on NT 4.0.
0
Comment
Question by:kfcox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Expert Comment

by:anichini
ID: 1411234
I'm pretty sure that VC++ 5.0 doesn't support member templates anyway, so the question is moot, no?

0
 

Author Comment

by:kfcox
ID: 1411235
I've gotten (simple) member templates (such as the one shown in the example I gave) to work just fine ... when there is no DLL exportation involved. What I really need to do is export the static class member variables without exporting the member functions ... and in a way that allows importing the static member variables into clients. For example, if I define Foo as:

template <class T>
class Foo {
public:
    Foo() { }
    virtual ~Foo() { }

    template <class A>
    static void DoIt( A* pA, T t ) {
        m_datum = t;
        pA->doit(t);
    }
    static TEMPL_DECL T m_datum;  // *** export/import m_datum
};


... I'm able to use Foo in clients fine, but the problem that is still hanging me up is that I'm unable to export some other instance of Foo from a separate DLL. In other words, if Foo is defined as part of Foo.DLL (and Foo<int> exported from it), I'm not able to export Foo<something_else> from a separate DLL, say FooUser.DLL because the TEMPL_DECL macro used in specifying m_datum is defined to be __declspec(dllimport) during compilation of FooUser.DLL. Am I making any sense? What I really need to be able to do is export Foo<something_else> from FooUser.DLL!!

0
 
LVL 2

Accepted Solution

by:
mnguyen021997 earned 900 total points
ID: 1411236
try using a def file.  remove all decl_exports and when you try to link you should get a unresolved error with the mangled name.  use the mangled name in the export section of your def file (this is the way mfc does it).
0
 

Expert Comment

by:bergkamp1
ID: 14577820
You can try putting the export syntax ahead of every non-template member of the template class in the class declaration. Then make the template member inline. Then use the EXPORT_TEMPL for each non-template member
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question